SHARE
TWEET

Untitled

a guest Aug 13th, 2019 141 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
  2. from django.db import models
  3. from django.utils import timezone
  4.  
  5.  
  6. class UserManager(BaseUserManager):
  7.  
  8.     def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
  9.         if not email:
  10.             raise ValueError('Users must have an email address')
  11.         now = timezone.now()
  12.         email = self.normalize_email(email)
  13.         user = self.model(
  14.             email=email,
  15.             is_staff=is_staff,
  16.             is_active=True,
  17.             is_superuser=is_superuser,
  18.             last_login=now,
  19.             date_joined=now,
  20.             **extra_fields
  21.         )
  22.         user.set_password(password)
  23.         user.save(using=self._db)
  24.         return user
  25.  
  26.     def create_user(self, email=None, password=None, **extra_fields):
  27.         return self._create_user(email, password, False, False, **extra_fields)
  28.  
  29.     def create_superuser(self, email, password, **extra_fields):
  30.         user = self._create_user(email, password, True, True, **extra_fields)
  31.         user.save(using=self._db)
  32.         return user
  33.  
  34.  
  35. class User(AbstractBaseUser, PermissionsMixin):
  36.     email = models.EmailField(max_length=254, unique=True)
  37.     name = models.CharField(max_length=254, null=True, blank=True)
  38.     is_staff = models.BooleanField(default=False)
  39.     is_superuser = models.BooleanField(default=False)
  40.     is_active = models.BooleanField(default=True)
  41.     last_login = models.DateTimeField(null=True, blank=True)
  42.     date_joined = models.DateTimeField(auto_now_add=True)
  43.  
  44.     USERNAME_FIELD = 'email'
  45.     EMAIL_FIELD = 'email'
  46.     REQUIRED_FIELDS = []
  47.  
  48.     objects = UserManager()
  49.  
  50.     def get_absolute_url(self):
  51.         return "/users/%i/" % (self.pk)
  52.     def get_email(self):
  53.         return self.email
  54.  
  55. # -----I will explain this part later. So let's keep it commented for now-------
  56.  
  57. # class user_type(models.Model):
  58. #     is_teach = models.BooleanField(default=False)
  59. #     is_student = models.BooleanField(default=False)
  60. #     user = models.OneToOneField(User, on_delete=models.CASCADE)
  61.  
  62. #     def __str__(self):
  63. #         if self.is_student == True:
  64. #             return User.get_email(self.user) + " - is_student"
  65. #         else:
  66. #             return User.get_email(self.user) + " - is_teacher"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top