· 4 years ago · Dec 02, 2020, 01:30 PM
1from django.db import models
2from django.contrib.auth.models import (
3 AbstractBaseUser,
4 BaseUserManager
5)
6# Create your models here.
7
8class AccountManager(BaseUserManager):
9
10 def create_user(
11 self,
12 email,
13 username,
14 password=None,
15 is_active=True,
16 is_staff=False,
17 is_admin=False,
18 is_superuser=False
19 ):
20 if not email:
21 raise ValueError("Users must have a Email.")
22 if not username:
23 raise ValueError("Users must have a Username.")
24 if not password:
25 raise ValueError("Users must have a Password.")
26
27 user = self.model(
28 email = self.normalize_email(email),
29 username = username,
30 is_active = is_active,
31 is_staff = is_staff,
32 is_admin = is_admin,
33 is_superuser = is_superuser
34 )
35 user.set_password(password)
36 user.save(using=self._db)
37 return user
38
39 def create_staffuser(self, email, username, password):
40 user = self.create_user(
41 email,
42 username,
43 password=password,
44 is_active=True,
45 is_staff=True,
46 is_admin=True,
47 )
48 return user
49
50 def create_superuser(self, email, username, password):
51 user = self.create_user(
52 email,
53 username,
54 password=password,
55 is_active=True,
56 is_staff=True,
57 is_admin=True,
58 is_superuser=True
59 )
60 return user
61
62
63
64class Account(AbstractBaseUser):
65
66 username = models.CharField(max_length=255, unique=True)
67 email = models.CharField(max_length=255, unique=True)
68 is_active = models.BooleanField(default=True)
69 is_staff = models.BooleanField(default=False)
70 is_admin = models.BooleanField(default=False)
71 is_superuser = models.BooleanField(default=False)
72
73 USERNAME_FIELD = "email"
74 REQUIRED_FIELDS = ["username"]
75
76 objects = AccountManager()
77
78 def __str__(self):
79 return self.email
80
81 def has_perm(self, perm, obj=None):
82 return self.is_admin
83
84 def has_module_perms(self, app_label):
85 return True
86
87
88
89
90
91
92 ######### Settings.py #########
93
94
95
96
97from pathlib import Path
98
99# Build paths inside the project like this: BASE_DIR / 'subdir'.
100BASE_DIR = Path(__file__).resolve().parent.parent
101
102
103# Quick-start development settings - unsuitable for production
104# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
105
106# SECURITY WARNING: keep the secret key used in production secret!
107SECRET_KEY = '4jr5v_oqt_8y_qe)bij_wf4(duu-1eab+$8y7-2uns62jsyg)#'
108
109# SECURITY WARNING: don't run with debug turned on in production!
110DEBUG = True
111
112ALLOWED_HOSTS = []
113
114
115# Application definition
116
117INSTALLED_APPS = [
118 'django.contrib.admin',
119 'django.contrib.auth',
120 'django.contrib.contenttypes',
121 'django.contrib.sessions',
122 'django.contrib.messages',
123 'django.contrib.staticfiles',
124 # my application
125 'Backend'
126]
127
128MIDDLEWARE = [
129 'django.middleware.security.SecurityMiddleware',
130 'django.contrib.sessions.middleware.SessionMiddleware',
131 'django.middleware.common.CommonMiddleware',
132 'django.middleware.csrf.CsrfViewMiddleware',
133 'django.contrib.auth.middleware.AuthenticationMiddleware',
134 'django.contrib.messages.middleware.MessageMiddleware',
135 'django.middleware.clickjacking.XFrameOptionsMiddleware',
136]
137
138ROOT_URLCONF = 'Easybcs.urls'
139
140TEMPLATES = [
141 {
142 'BACKEND': 'django.template.backends.django.DjangoTemplates',
143 'DIRS': [],
144 'APP_DIRS': True,
145 'OPTIONS': {
146 'context_processors': [
147 'django.template.context_processors.debug',
148 'django.template.context_processors.request',
149 'django.contrib.auth.context_processors.auth',
150 'django.contrib.messages.context_processors.messages',
151 ],
152 },
153 },
154]
155
156# Added line
157AUTH_USER_MODEL = "Backend.Account"
158
159WSGI_APPLICATION = 'Easybcs.wsgi.application'
160