-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
116 lines (97 loc) · 3.03 KB
/
models.py
File metadata and controls
116 lines (97 loc) · 3.03 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
import uuid
import django.contrib.auth.models
import django.db.models
import django.utils.timezone
import business.models
import user.constants
class UserManager(django.contrib.auth.models.BaseUserManager):
def create_user(self, email, name, surname, password=None, **extra_fields):
if not email:
raise ValueError('The Email must be set')
email = self.normalize_email(email)
user = self.model(
email=email,
name=name,
surname=surname,
**extra_fields,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(
self,
email,
name,
surname,
password=None,
**extra_fields,
):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self.create_user(email, name, surname, password, **extra_fields)
class User(
django.contrib.auth.models.AbstractBaseUser,
django.contrib.auth.models.PermissionsMixin,
):
id = django.db.models.UUIDField(
'UUID',
primary_key=True,
default=uuid.uuid4,
editable=False,
)
email = django.db.models.EmailField(
unique=True,
max_length=user.constants.EMAIL_MAX_LENGTH,
)
name = django.db.models.CharField(
max_length=user.constants.NAME_MAX_LENGTH,
)
surname = django.db.models.CharField(
max_length=user.constants.SURNAME_MAX_LENGTH,
)
avatar_url = django.db.models.URLField(
blank=True,
null=True,
max_length=user.constants.AVATAR_URL_MAX_LENGTH,
)
other = django.db.models.JSONField(default=dict)
token_version = django.db.models.IntegerField(default=0)
is_active = django.db.models.BooleanField(default=True)
is_staff = django.db.models.BooleanField(default=False)
last_login = django.db.models.DateTimeField(null=True, blank=True)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name', 'surname']
def __str__(self):
return self.email
def save(self, *args, **kwargs):
if not self.pk:
self.last_login = django.utils.timezone.now()
super().save(*args, **kwargs)
class PromoLike(django.db.models.Model):
id = django.db.models.UUIDField(
'UUID',
primary_key=True,
default=uuid.uuid4,
editable=False,
)
user = django.db.models.ForeignKey(
User,
on_delete=django.db.models.CASCADE,
related_name='promo_likes',
)
promo = django.db.models.ForeignKey(
business.models.Promo,
on_delete=django.db.models.CASCADE,
related_name='likes',
)
created_at = django.db.models.DateTimeField(auto_now_add=True)
class Meta:
constraints = [
django.db.models.UniqueConstraint(
fields=['user', 'promo'],
name='unique_like',
),
]
def __str__(self):
return f'{self.user} likes {self.promo}'