-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagers.py
More file actions
182 lines (154 loc) · 5.13 KB
/
managers.py
File metadata and controls
182 lines (154 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import django.contrib.auth.models
import django.db.models
import django.utils.timezone
import business.constants
import business.models
class CompanyManager(django.contrib.auth.models.BaseUserManager):
def create_company(self, email, name, password=None, **extra_fields):
if not email:
raise ValueError('The Email must be set')
email = self.normalize_email(email)
company = self.model(
email=email,
name=name,
**extra_fields,
)
company.set_password(password)
company.save(using=self._db)
return company
class PromoManager(django.db.models.Manager):
with_related_fields = (
'id',
'company__id',
'company__name',
'description',
'image_url',
'target',
'max_count',
'active_from',
'active_until',
'mode',
'promo_common',
'created_at',
)
def get_queryset(self):
return super().get_queryset()
def with_related(self):
return (
self.select_related('company')
.prefetch_related('unique_codes')
.only(
*self.with_related_fields,
)
)
def for_company(self, user):
return self.with_related().filter(company=user)
def get_feed_for_user(
self,
user,
active_filter=None,
user_country=None,
user_age=None,
):
"""
Retrieve a queryset of Promo objects for a given user, filtered
and ordered according to specified criteria.
"""
today = django.utils.timezone.now().date()
qs = (
self.get_queryset()
.select_related('company')
.annotate(_has_unique_codes=self._q_has_unique_codes())
.filter(self._q_is_targeted(user_country, user_age))
)
if active_filter is not None:
is_active = active_filter.lower() == 'true'
active_q = self._q_is_active(today)
qs = qs.filter(active_q) if is_active else qs.exclude(active_q)
return qs.order_by('-created_at')
def _q_is_active(self, today):
"""
Build a Q expression that checks whether a promo
is active on the given date.
"""
qt = django.db.models.Q(active_from__lte=today) | django.db.models.Q(
active_from__isnull=True,
)
tu = django.db.models.Q(active_until__gte=today) | django.db.models.Q(
active_until__isnull=True,
)
common = django.db.models.Q(
mode=business.constants.PROMO_MODE_COMMON,
used_count__lt=django.db.models.F('max_count'),
)
unique = django.db.models.Q(
mode=business.constants.PROMO_MODE_UNIQUE,
_has_unique_codes=True,
)
return qt & tu & (common | unique)
def _q_has_unique_codes(self):
"""
Annotate whether there are unused unique codes remaining
for each promo.
"""
subq = business.models.PromoCode.objects.filter(
promo=django.db.models.OuterRef('pk'),
is_used=False,
)
return django.db.models.Exists(subq)
def _q_is_targeted(self, country, age):
"""
Build a Q expression that checks whether a promo targets the given
country and age, or is not targeted.
"""
empty = django.db.models.Q(target={})
match_country = (
django.db.models.Q(target__country__iexact=country)
if country
else django.db.models.Q()
)
no_country = ~django.db.models.Q(
target__has_key='country',
) | django.db.models.Q(target__country__isnull=True)
country_ok = match_country | no_country
from_ok = (
~django.db.models.Q(target__has_key='age_from')
| django.db.models.Q(target__age_from__isnull=True)
| django.db.models.Q(target__age_from__lte=age)
)
until_ok = (
~django.db.models.Q(target__has_key='age_until')
| django.db.models.Q(target__age_until__isnull=True)
| django.db.models.Q(target__age_until__gte=age)
)
age_ok = from_ok & until_ok
return empty | (country_ok & age_ok)
@django.db.transaction.atomic
def create_promo(
self,
user,
target_data,
promo_common,
promo_unique,
**kwargs,
):
promo = self.create(
company=user,
target=target_data,
**kwargs,
)
if promo.mode == business.constants.PROMO_MODE_COMMON:
promo.promo_common = promo_common
promo.save(update_fields=['promo_common'])
elif (
promo.mode == business.constants.PROMO_MODE_UNIQUE and promo_unique
):
self._create_unique_codes(promo, promo_unique)
return promo
def _create_unique_codes(self, promo, codes):
business.models.PromoCode.objects.bulk_create(
[
business.models.PromoCode(promo=promo, code=code)
for code in codes
],
)