Advertisement
lamorfini

Untitled

Sep 30th, 2024
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.82 KB | None | 0 0
  1. from django.contrib.auth.models import AbstractUser
  2. from django.db import models
  3. from django.contrib.auth.base_user import BaseUserManager
  4. from django.utils.translation import gettext as _
  5.  
  6.  
  7. class CustomUserManager(BaseUserManager):
  8.     def create_user(self, email, username, first_name, last_name, password, **extra_fields):
  9.         if not email:
  10.             raise ValueError('The Email must be set')
  11.         email = self.normalize_email(email)
  12.         user = self.model(email=email, username=username, first_name=first_name, last_name=last_name, **extra_fields)
  13.         user.set_password(password)
  14.         user.save()
  15.         return user
  16.  
  17.     def create_superuser(self, email, username, first_name, last_name, password, **extra_fields):
  18.         extra_fields.setdefault('is_staff', True)
  19.         extra_fields.setdefault('is_superuser', True)
  20.         extra_fields.setdefault('is_active', True)
  21.  
  22.         if extra_fields.get('is_staff') is not True:
  23.             raise ValueError('Superuser must have is_staff=True.')
  24.         if extra_fields.get('is_superuser') is not True:
  25.             raise ValueError('Superuser must have is_superuser=True.')
  26.         return self.create_user(email, username, first_name, last_name, password, **extra_fields)
  27.  
  28.  
  29. class User(AbstractUser):
  30.     class DisplayNameChoices(models.TextChoices):
  31.         FIRST_LAST = 'FL', _('First name + Last name')
  32.         LAST_FIRST = 'LF', _('Last name + First name')
  33.         USERNAME = 'UN', _('Username')
  34.  
  35.     class SexChoices(models.TextChoices):
  36.         MALE = 'M', _('Male')
  37.         FEMALE = 'F', _('Female')
  38.         OTHER = 'O', _('Other')        
  39.  
  40.     email = models.EmailField(unique=True, verbose_name=_('Email'))
  41.     first_name = models.CharField(verbose_name=_('First name'), max_length=30, blank=True)
  42.     last_name = models.CharField(verbose_name=_('Last name'),max_length=30, blank=True)
  43.     display_name_preference = models.CharField(
  44.         _('display name preference'),
  45.         max_length=2,
  46.         choices=DisplayNameChoices.choices,
  47.         default=DisplayNameChoices.FIRST_LAST,
  48.         blank=True, null=True
  49.     )
  50.     date_of_birth = models.DateField(verbose_name=_('Date of birth'), blank=True, null=True)
  51.     sex = models.CharField(
  52.         _('sex'),
  53.         max_length=1,
  54.         choices=SexChoices.choices,
  55.         default=SexChoices.OTHER,
  56.         blank=True, null=True
  57.        
  58.     )
  59.     telephone = models.CharField(verbose_name=_('Telephone'), max_length=255, blank=True, null=True)
  60.     address = models.CharField(verbose_name=_('Address'), max_length=255, blank=True, null=True)
  61.     city = models.CharField(verbose_name=_('City'), max_length=255, blank=True, null=True)
  62.     province = models.CharField(verbose_name=_('Province'), max_length=255, blank=True, null=True)
  63.     country = models.CharField(verbose_name=_('Country'), max_length=255, null=True, blank=True)
  64.     postal_code = models.CharField(verbose_name=_('Postal code'), max_length=50, blank=True, null=True)
  65.     note = models.TextField(verbose_name=_('Note'), blank=True, null=True)
  66.    
  67.     objects = CustomUserManager()
  68.  
  69.     USERNAME_FIELD  = 'username'
  70.     EMAIL_FIELD     = 'email'
  71.     REQUIRED_FIELDS = ['first_name', 'last_name', 'email']
  72.  
  73.     def get_display_name(self):
  74.         if self.display_name_preference == self.DisplayNameChoices.FIRST_LAST:
  75.             return f"{self.first_name} {self.last_name}"
  76.         elif self.display_name_preference == self.DisplayNameChoices.LAST_FIRST:
  77.             return f"{self.last_name} {self.first_name}"
  78.         else:
  79.             return self.username
  80.  
  81.     def __str__(self):
  82.         return f'{self.username}'
  83.  
  84.     class Meta:
  85.         verbose_name = _("User")
  86.         verbose_name_plural = _("Users")
  87.         ordering = ['email']
  88.  
  89.  
  90.  
  91.  
  92. -----
  93.  
  94. # AllAuth settings
  95. ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
  96. ACCOUNT_EMAIL_REQUIRED = True
  97. ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
  98. ACCOUNT_UNIQUE_EMAIL = True
  99. ACCOUNT_USERNAME_REQUIRED = True
  100. ACCOUNT_UNIQUE_USERNAME = True
  101.  
  102. LOGIN_REDIRECT_URL = "/"
  103.  
  104. EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
  105. EMAIL_HOST = 'smtp.gmail.com'
  106. EMAIL_HOST_USER = 'amorfiniluigi82'
  107. EMAIL_HOST_PASSWORD = ''
  108. EMAIL_PORT = 587
  109. EMAIL_USE_TLS = True
  110. DEFAULT_FROM_EMAIL = 'amorfiniluigi82@'
  111.  
  112. AUTH_USER_MODEL = 'accounts.User'
  113.  
  114.  
  115. SITE_ID = 1
  116.  
  117. ACCOUNT_FORMS = {
  118.     'add_email': 'accounts.forms.AddEmailForm',
  119.     'change_password': 'accounts.forms.ChangePasswordForm',
  120.     'confirm_login_code': 'accounts.forms.ConfirmLoginCodeForm',
  121.     'login': 'accounts.forms.LoginForm',
  122.     'request_login_code': 'accounts.forms.RequestLoginCodeForm',
  123.     'reset_password': 'accounts.forms.ResetPasswordForm',
  124.     'reset_password_from_key': 'accounts.forms.ResetPasswordKeyForm',
  125.     'set_password': 'accounts.forms.SetPasswordForm',
  126.     'signup': 'accounts.forms.SignupForm',
  127. }
  128.  
  129. -----
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement