-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmodels.py
145 lines (110 loc) · 4.05 KB
/
models.py
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
from django.conf import settings
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
from django.core.validators import RegexValidator
from django.db import models
from django.db.models.signals import post_save
# Create your models here.
from .utils import code_generator
USERNAME_REGEX = '^[a-zA-Z0-9.+-]*$'
class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
username = username,
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(
username,
email,
password=password,
)
user.is_admin = True
user.is_staff = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
username = models.CharField(
max_length=255,
validators=[
RegexValidator(
regex = USERNAME_REGEX,
message = 'Username must be Alpahnumeric or contain any of the following: ". @ + -" ',
code='invalid_username'
)],
unique=True,
)
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
zipcode = models.CharField(max_length=120, default="92660")
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
# @property
# def is_staff(self):
# "Is the user a member of staff?"
# # Simplest possible answer: All admins are staff
# return self.is_admin
class ActivationProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
key = models.CharField(max_length=120)
expired = models.BooleanField(default=False)
def save(self, *args, **kwargs):
self.key = code_generator()
super(ActivationProfile, self).save(*args, **kwargs)
def post_save_activation_receiver(sender, instance, created, *args, **kwargs):
if created:
#send email
print('activation created')
post_save.connect(post_save_activation_receiver, sender=ActivationProfile)
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
city = models.CharField(max_length=120, null=True, blank=True)
def __str__(self):
return str(self.user.username)
def __unicode__(self):
return str(self.user.username)
def post_save_user_model_receiver(sender, instance, created, *args, **kwargs):
if created:
try:
Profile.objects.create(user=instance)
ActivationProfile.objects.create(user=instance)
except:
pass
post_save.connect(post_save_user_model_receiver, sender=settings.AUTH_USER_MODEL)