class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(unique=True) username = models.CharField(max_length=30, unique=True) fullname = models.CharField(max_length=50, blank=True, null=True) profile_picture = models.ImageField(upload_to="profiles/", default='user.png', validators=[validate_image]) bio = models.CharField(max_length=300, blank=True, null=True) phone_number = models.CharField(max_length=30, unique=True, blank=True, null=True) register_date = models.DateField(auto_now=True) is_staff = models.BooleanField(default=False) is_active = models.BooleanField(default=True) is_private = models.BooleanField(default=False) USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['username'] objects = UserManager() def __str__(self): return "Username: {}, Email: {}".format(self.username, self.email) def get_full_name(self): return self.fullname def get_bio(self): return self.bio
class User(AbstractUser): # changes email to unique and blank to false email = models.EmailField(_('email address'), unique=True) first_name = models.CharField(_('First Name'), max_length=20) last_name = models.CharField(_('Last Name'), max_length=100) username = None objects = UserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = []
class User(AbstractUser): class Meta: ordering = ["-id"] username = None email = models.EmailField("email address", unique=True) USERNAME_FIELD = "email" REQUIRED_FIELDS = [] objects = UserManager() def __str__(self): return f"{self.email} : {self.username}" if self.username else self.email
class User(AbstractUser): """Main User model""" MARITAL_STATUS_CHOICES = (("M", "Married"), ("S", "Single")) # id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) username = None email = models.EmailField(_("Email Address"), unique=True) dob = models.DateField(_("Date of Birth"), blank=True, null=True) marital_status = models.CharField(_("Marital Status"), max_length=1, choices=MARITAL_STATUS_CHOICES, blank=True, null=True) profile_photo = models.ImageField(_("Profile Photo"), upload_to=upload_profile_photo, blank=True, null=True, default=os.path.join( settings.MEDIA_ROOT, "default", "persion.svg")) phoneNumber = models.CharField(_("Phone Number"), max_length=20, blank=True, null=True) phoneNumberVerified = models.BooleanField(_("Phone Number Verified"), default=False) is_expert = models.BooleanField(_("Expert Status"), default=False) is_customer = models.BooleanField(_("Customer Status"), default=False) is_influencer = models.BooleanField(_("Influencer Status"), default=False) USERNAME_FIELD = "email" REQUIRED_FIELDS = [] objects = UserManager() def __str__(self): return str(self.email)
class User(AbstractBaseUser, PermissionsMixin): """ Custom User Model. """ USERNAME_FIELD = 'email' email = models.EmailField( _('email address'), null=False, max_length=255, unique=True, error_messages={ 'unique': _("A user with that username already exists."), } ) password = models.CharField( _('password'), max_length=128, help_text=_('Please enter your password.') ) first_name = models.CharField(_('first name'), max_length=30, blank=True) last_name = models.CharField(_('last name'), max_length=150, blank=True) is_staff = models.BooleanField( _('staff status'), default=False, help_text=_( 'Designates whether the user can log into this admin site.'), ) is_active = models.BooleanField( _('active'), default=True, help_text=_( 'Designates whether this user should be treated as active. ' 'Unselect this instead of deleting accounts.' ), ) date_joined = models.DateTimeField(_('date joined'), default=timezone.now) objects = UserManager() @property def get_full_name(self): if not self.first_name or not self.last_name: return self.email return '{} {}'.format(self.first_name, self.last_name) def __str__(self): return self.get_full_name @classmethod def create(cls, **kwargs): """ Utility function to create users. :return: user instance or None """ user = None password = kwargs.get('password') email = kwargs.get('email') if not email or not password: return None # Check if user exists. try: user = User.objects.get(email=email) except User.DoesNotExist: pass # If exists then return None. if user: return None # Else go on and create the user. user = User.objects.create(**kwargs) user.set_password(password) user.save() return user
class User(AbstractBaseUser, PermissionsMixin, Auth0UserModelMixin): """ Custom User Model Major Changes Include: - uuid as primary key - phone number field - phone number verification field - email verification field All else works as expected. """ uuid = models.UUIDField(primary_key=True, db_index=True, default=uuid4, editable=False) date_joined = models.DateTimeField(_("date joined"), auto_now_add=True) is_active = models.BooleanField(_("active"), default=True) is_staff = models.BooleanField(_("is staff"), default=False) is_superuser = models.BooleanField(_("is superuser"), default=False) first_name = models.CharField(_("first name"), max_length=30, blank=True) last_name = models.CharField(_("last name"), max_length=30, blank=True) email = models.EmailField(_("email address"), max_length=128, unique=True, blank=True, null=True) email_verified_on = models.DateTimeField(_("email verified on"), blank=True, null=True) phone_number = models.CharField(_("phone number"), max_length=16, unique=True) phone_number_verified_on = models.DateTimeField( _("phone number verified on"), blank=True, null=True) # image url DEFAULT_IMAGE_URL = ( "https://www.gravatar.com/avatar/85c0bae3b2a1e04908a3c916fbb000f7") image_url = models.URLField(default=DEFAULT_IMAGE_URL) auth0_user_id = models.CharField(max_length=35, blank=True, null=True) # object manager objects = UserManager() # django user model field enhancments USERNAME_FIELD = "phone_number" REQUIRED_FIELDS = ["first_name", "last_name", "email"] class Meta: verbose_name = _("user") verbose_name_plural = _("users") def __str__(self): return self.get_full_name() or self.email @classmethod def load_set_password_token(cls, signed): try: value = signing.loads(signed) return value except signing.BadSignature: logger.warning(f"Bad set password token for user:{self.uuid}") return @property def name(self): name = self.get_full_name() name = " ".join([s.strip() for s in name.split(" ") if s.strip()]).strip() return name.strip() @property def phone_number__prettified(self): return pretty_print_phone_number(self.phone_number) @property def phone_number_verified(self): return self.phone_number and self.phone_number_verified_on is not None @property def email_verified(self): return self.email and self.email_verified_on is not None @property def has_password(self): return self.password not in [None, ""] def get_full_name(self): """ Returns the first_name plus the last_name, with a space in between. """ return f"{self.first_name} {self.last_name}".strip() def get_short_name(self): """ Returns the short name for the user. """ return self.first_name def email_user(self, subject, message, from_email=None, **kwargs): """ Sends an email to this User. """ send_mail(subject, message, from_email, [self.email], **kwargs) def verify_phone_number(self): self.phone_number_verified_on = timezone.now() self.save() def gen_set_password_token(self): # TODO: consider making the timedelta a setting expires = timezone.now() + timedelta(hours=4) value = f"{self.phone_number}::{int(expires.timestamp())}" signed = signing.dumps(value) return signed def is_hotline_admin(self): return self.groups.filter(name="hotline_admin").exists()
def user_manager(self): manager = UserManager() manager.model = User return manager