Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.contrib.auth.models import AbstractUser
- from django.db import models
- from django.contrib.auth.base_user import BaseUserManager
- from django.utils.translation import gettext as _
- class CustomUserManager(BaseUserManager):
- def create_user(self, email, username, first_name, last_name, password, **extra_fields):
- if not email:
- raise ValueError('The Email must be set')
- email = self.normalize_email(email)
- user = self.model(email=email, username=username, first_name=first_name, last_name=last_name, **extra_fields)
- user.set_password(password)
- user.save()
- return user
- def create_superuser(self, email, username, first_name, last_name, password, **extra_fields):
- extra_fields.setdefault('is_staff', True)
- extra_fields.setdefault('is_superuser', True)
- extra_fields.setdefault('is_active', True)
- if extra_fields.get('is_staff') is not True:
- raise ValueError('Superuser must have is_staff=True.')
- if extra_fields.get('is_superuser') is not True:
- raise ValueError('Superuser must have is_superuser=True.')
- return self.create_user(email, username, first_name, last_name, password, **extra_fields)
- class User(AbstractUser):
- class DisplayNameChoices(models.TextChoices):
- FIRST_LAST = 'FL', _('First name + Last name')
- LAST_FIRST = 'LF', _('Last name + First name')
- USERNAME = 'UN', _('Username')
- class SexChoices(models.TextChoices):
- MALE = 'M', _('Male')
- FEMALE = 'F', _('Female')
- OTHER = 'O', _('Other')
- email = models.EmailField(unique=True, verbose_name=_('Email'))
- first_name = models.CharField(verbose_name=_('First name'), max_length=30, blank=True)
- last_name = models.CharField(verbose_name=_('Last name'),max_length=30, blank=True)
- display_name_preference = models.CharField(
- _('display name preference'),
- max_length=2,
- choices=DisplayNameChoices.choices,
- default=DisplayNameChoices.FIRST_LAST,
- blank=True, null=True
- )
- date_of_birth = models.DateField(verbose_name=_('Date of birth'), blank=True, null=True)
- sex = models.CharField(
- _('sex'),
- max_length=1,
- choices=SexChoices.choices,
- default=SexChoices.OTHER,
- blank=True, null=True
- )
- telephone = models.CharField(verbose_name=_('Telephone'), max_length=255, blank=True, null=True)
- address = models.CharField(verbose_name=_('Address'), max_length=255, blank=True, null=True)
- city = models.CharField(verbose_name=_('City'), max_length=255, blank=True, null=True)
- province = models.CharField(verbose_name=_('Province'), max_length=255, blank=True, null=True)
- country = models.CharField(verbose_name=_('Country'), max_length=255, null=True, blank=True)
- postal_code = models.CharField(verbose_name=_('Postal code'), max_length=50, blank=True, null=True)
- note = models.TextField(verbose_name=_('Note'), blank=True, null=True)
- objects = CustomUserManager()
- USERNAME_FIELD = 'username'
- EMAIL_FIELD = 'email'
- REQUIRED_FIELDS = ['first_name', 'last_name', 'email']
- def get_display_name(self):
- if self.display_name_preference == self.DisplayNameChoices.FIRST_LAST:
- return f"{self.first_name} {self.last_name}"
- elif self.display_name_preference == self.DisplayNameChoices.LAST_FIRST:
- return f"{self.last_name} {self.first_name}"
- else:
- return self.username
- def __str__(self):
- return f'{self.username}'
- class Meta:
- verbose_name = _("User")
- verbose_name_plural = _("Users")
- ordering = ['email']
- -----
- # AllAuth settings
- ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
- ACCOUNT_EMAIL_REQUIRED = True
- ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
- ACCOUNT_UNIQUE_EMAIL = True
- ACCOUNT_USERNAME_REQUIRED = True
- ACCOUNT_UNIQUE_USERNAME = True
- LOGIN_REDIRECT_URL = "/"
- EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
- EMAIL_HOST = 'smtp.gmail.com'
- EMAIL_HOST_USER = 'amorfiniluigi82'
- EMAIL_HOST_PASSWORD = ''
- EMAIL_PORT = 587
- EMAIL_USE_TLS = True
- DEFAULT_FROM_EMAIL = 'amorfiniluigi82@'
- AUTH_USER_MODEL = 'accounts.User'
- SITE_ID = 1
- ACCOUNT_FORMS = {
- 'add_email': 'accounts.forms.AddEmailForm',
- 'change_password': 'accounts.forms.ChangePasswordForm',
- 'confirm_login_code': 'accounts.forms.ConfirmLoginCodeForm',
- 'login': 'accounts.forms.LoginForm',
- 'request_login_code': 'accounts.forms.RequestLoginCodeForm',
- 'reset_password': 'accounts.forms.ResetPasswordForm',
- 'reset_password_from_key': 'accounts.forms.ResetPasswordKeyForm',
- 'set_password': 'accounts.forms.SetPasswordForm',
- 'signup': 'accounts.forms.SignupForm',
- }
- -----
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement