-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagers.py
More file actions
83 lines (71 loc) · 2.18 KB
/
managers.py
File metadata and controls
83 lines (71 loc) · 2.18 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
import django.contrib.auth.models
import django.db.models
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):
def get_queryset(self):
return super().get_queryset()
def with_related(self):
return (
self.select_related('company')
.prefetch_related('unique_codes')
.only(
'id',
'company',
'description',
'image_url',
'target',
'max_count',
'active_from',
'active_until',
'mode',
'promo_common',
'created_at',
'company__id',
'company__name',
)
)
def for_company(self, user):
return self.with_related().filter(company=user)
@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
],
)