Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """ User models."""
- from django.contrib.auth.models import (
- AbstractBaseUser, BaseUserManager, PermissionsMixin)
- from django.core.mail import send_mail
- from django.db import models
- from django.utils import timezone
- from django.utils.translation import ugettext_lazy as _
- from django.conf import settings
- from custom_user.utils import hide_email
- class EmailUserManager(BaseUserManager):
- """ Custom manager for EmailUser."""
- def _create_user(self, email, password,
- is_staff, is_superuser, **extra_fields):
- """ Create and save an EmailUser with the given email and password.
- :param str email: user email
- :param str password: user password
- :param bool is_staff: whether user staff or not
- :param bool is_superuser: whether user admin or not
- :return custom_user.models.EmailUser user: user
- :raise ValueError: email is not set
- """
- now = timezone.now()
- # if not email:
- # raise ValueError('The given email must be set')
- # For socials have to equal email(if emply seted) None
- if email:
- email = self.normalize_email(email)
- else:
- email = None
- is_active = extra_fields.pop("is_active", True)
- user = self.model(email=email, is_staff=is_staff, is_active=is_active,
- is_superuser=is_superuser, last_login=now,
- date_joined=now, **extra_fields)
- if password:
- user.set_password(password)
- user.save(using=self._db)
- return user
- def create_user(self, email, password=None, **extra_fields):
- """ Create and save an EmailUser with the given email and password.
- :param str email: user email
- :param str password: user password
- :return custom_user.models.EmailUser user: regular user
- """
- is_staff = extra_fields.pop("is_staff", False)
- return self._create_user(email, password, is_staff, False,
- **extra_fields)
- def create_superuser(self, email, password, **extra_fields):
- """ Create and save an EmailUser with the given email and password.
- :param str email: user email
- :param str password: user password
- :return custom_user.models.EmailUser user: admin user
- """
- return self._create_user(email, password, True, True,
- **extra_fields)
- class AbstractEmailUser(AbstractBaseUser, PermissionsMixin):
- """ Abstract User with the same behaviour as Django's default User.
- AbstractEmailUser does not have username field. Uses email as the
- USERNAME_FIELD for authentication.
- Use this if you need to extend EmailUser.
- Inherits from both the AbstractBaseUser and PermissionMixin.
- The following attributes are inherited from the superclasses:
- * password
- * last_login
- * is_superuser
- """
- email = models.EmailField(_('email address'), max_length=255,
- unique=True, db_index=True, null=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 = EmailUserManager()
- USERNAME_FIELD = 'email'
- REQUIRED_FIELDS = []
- class Meta:
- verbose_name = _('user')
- verbose_name_plural = _('users')
- abstract = True
- def get_full_name(self):
- """ Return the email."""
- return self.email
- def get_short_name(self):
- """ Return the email."""
- return self.email
- def email_user(self, subject, message, **kwargs):
- """ Send an email to this User."""
- send_mail(subject, message, getattr(settings, 'EMAIL_HOST_USER'), [self.email], **kwargs)
- class EmailUser(AbstractEmailUser):
- """
- Expand Custom Email user abstraction
- """
- GENDER_LIST = (
- ('male', _('Male')),
- ('female', _('Female')),
- )
- username = models.CharField(_('User name from socials'), max_length=256, blank=True, null=True)
- first_name = models.CharField(_('First Name'), max_length=256, blank=True, null=True)
- last_name = models.CharField(_('Last Name'), max_length=256, blank=True, null=True)
- gender = models.CharField(_('Gender'), max_length=10, choices=GENDER_LIST, null=True, blank=True)
- birthdate = models.DateField(_('Birthdate'), null=True, blank=True)
- # joined = models.DateTimeField(_('Last Name'), default=timezone.now)
- avatar = models.ImageField(_('Avatar'), upload_to='profiles', blank=True, null=True)
- def get_full_name(self):
- name_list = []
- if self.first_name:
- name_list.append(self.first_name)
- if self.last_name:
- name_list.append(self.last_name)
- if not name_list:
- return self.email or self.username
- else:
- return ' '.join(name_list)
- def get_hidden_name(self):
- return hide_email(self.get_full_name())
- def in_group(self, group_name):
- return group_name in self.groups.all().values_list('name', flat=True)
- def in_copyright_group(self):
- return self.in_group('copyright')
- def __unicode__(self):
- return self.get_full_name()
- class Meta(AbstractEmailUser.Meta):
- swappable = 'AUTH_USER_MODEL'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement