forked from CEOS-Developers/django-vote-14th
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
68 lines (50 loc) · 1.78 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
from django.db import models
from django.contrib.auth.models import (BaseUserManager, AbstractBaseUser)
# Create your models here.
class Base(models.Model):
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class Candidate(Base):
name = models.CharField(max_length=50, null=False)
votes = models.IntegerField(default=0)
class UserManager(BaseUserManager):
use_in_migrations = True
def create_user(self, email, userid, password=None):
if not "email":
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
userid=userid,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, userid, password):
superuser = self.create_user(
email=self.normalize_email(email),
userid=userid,
password=password)
superuser.is_admin = True
superuser.is_superuser = True
superuser.save(using=self._db)
return superuser
class User(AbstractBaseUser, Base):
email = models.EmailField(
verbose_name='email',
max_length=255,
unique=True,
)
userid = models.CharField(max_length=50, unique=True, null=False)
voteDone = models.BooleanField(default=False)
voting_for = models.ForeignKey(Candidate, on_delete=models.CASCADE, related_name='voters', null=True)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True