-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
149 lines (124 loc) · 4.26 KB
/
models.py
File metadata and controls
149 lines (124 loc) · 4.26 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
import uuid
import django.contrib.auth.models
import django.db.models
import django.utils.timezone
import business.constants
import business.managers
class Company(django.contrib.auth.models.AbstractBaseUser):
id = django.db.models.UUIDField(
'UUID',
primary_key=True,
default=uuid.uuid4,
editable=False,
)
email = django.db.models.EmailField(
unique=True,
max_length=business.constants.COMPANY_EMAIL_MAX_LENGTH,
)
name = django.db.models.CharField(
max_length=business.constants.COMPANY_NAME_MAX_LENGTH,
)
token_version = django.db.models.PositiveIntegerField(default=0)
created_at = django.db.models.DateTimeField(auto_now_add=True)
is_active = django.db.models.BooleanField(default=True)
objects = business.managers.CompanyManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def __str__(self):
return self.name
class Promo(django.db.models.Model):
id = django.db.models.UUIDField(
'UUID',
primary_key=True,
default=uuid.uuid4,
editable=False,
)
company = django.db.models.ForeignKey(
Company,
on_delete=django.db.models.CASCADE,
null=True,
blank=True,
)
description = django.db.models.CharField(
max_length=business.constants.PROMO_DESC_MAX_LENGTH,
)
image_url = django.db.models.URLField(
max_length=business.constants.PROMO_IMAGE_URL_MAX_LENGTH,
blank=True,
null=True,
)
target = django.db.models.JSONField(default=dict)
max_count = django.db.models.PositiveIntegerField()
used_count = django.db.models.PositiveIntegerField(
default=0,
editable=False,
)
like_count = django.db.models.PositiveIntegerField(
default=0,
editable=False,
)
comment_count = django.db.models.PositiveIntegerField(
default=0,
editable=False,
)
active_from = django.db.models.DateField(null=True, blank=True)
active_until = django.db.models.DateField(null=True, blank=True)
mode = django.db.models.CharField(
max_length=10,
choices=business.constants.PROMO_MODE_CHOICES,
)
promo_common = django.db.models.CharField(
max_length=business.constants.PROMO_COMMON_CODE_MAX_LENGTH,
blank=True,
null=True,
)
active = django.db.models.BooleanField(default=True)
created_at = django.db.models.DateTimeField(auto_now_add=True)
objects = business.managers.PromoManager()
def __str__(self):
return f'Promo {self.id} ({self.mode})'
@property
def is_active(self) -> bool:
today = django.utils.timezone.now().date()
if self.active_from and self.active_from > today:
return False
if self.active_until and self.active_until < today:
return False
if self.mode == business.constants.PROMO_MODE_UNIQUE:
return self.unique_codes.filter(is_used=False).exists()
if self.mode == business.constants.PROMO_MODE_COMMON:
return self.used_count < self.max_count
return True
@property
def get_like_count(self) -> int:
return self.like_count
@property
def get_comment_count(self) -> int:
return self.comment_count
@property
def get_used_codes_count(self) -> int:
if self.mode == business.constants.PROMO_MODE_UNIQUE:
return self.unique_codes.filter(is_used=True).count()
# TODO: COMMON Promo
return 0
@property
def get_available_unique_codes(self) -> list[str] | None:
if self.mode == business.constants.PROMO_MODE_UNIQUE:
return [c.code for c in self.unique_codes.filter(is_used=False)]
return None
class PromoCode(django.db.models.Model):
promo = django.db.models.ForeignKey(
Promo,
on_delete=django.db.models.CASCADE,
related_name='unique_codes',
to_field='id',
)
code = django.db.models.CharField(
max_length=business.constants.PROMO_UNIQUE_CODE_MAX_LENGTH,
)
is_used = django.db.models.BooleanField(default=False)
used_at = django.db.models.DateTimeField(null=True, blank=True)
class Meta:
unique_together = ('promo', 'code')
def __str__(self):
return self.code