Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. class CUserManager(BaseUserManager):
  2.  
  3. def _create_user(self, email, first_name, password,
  4. is_staff, is_superuser, **extra_fields):
  5. """
  6. Creates and saves a User with the given email and password.
  7. """
  8. now = timezone.now()
  9. if not email:
  10. raise ValueError('The given email must be set')
  11. email = self.normalize_email(email)
  12. user = self.model(email=email,
  13. first_name = first_name,
  14. is_staff=is_staff, is_active=False,
  15. is_superuser=is_superuser, last_login=now,
  16. date_joined=now, **extra_fields)
  17. user.set_password(password)
  18. user.save(using=self._db)
  19. return user
  20.  
  21. def create_user(self, email, first_name, password=None, **extra_fields):
  22. return self._create_user(email, first_name, password, False, False,
  23. **extra_fields)
  24.  
  25. def create_superuser(self, email, first_name, password, **extra_fields):
  26. return self._create_user(email, first_name, password, True, True,
  27. **extra_fields)
  28.  
  29.  
  30. class CUser(AbstractBaseUser, PermissionsMixin):
  31. email = models.EmailField(_('email address'), max_length=254, unique=True)
  32. first_name = models.CharField(_('first name'), max_length=30)
  33. last_name = models.CharField(_('last name'), max_length=30, blank=True)
  34. is_staff = models.BooleanField(_('staff status'), default=False,
  35. help_text=_('Designates whether the user can log into this admin '
  36. 'site.'))
  37. is_active = models.BooleanField(_('active'), default=False,
  38. help_text=_('Designates whether this user should be treated as '
  39. 'active. Unselect this instead of deleting accounts.'))
  40. date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
  41. last_updated = models.DateTimeField(_('last updated'), default=timezone.now)
  42.  
  43. objects = CUserManager()
  44.  
  45. USERNAME_FIELD = 'email'
  46. REQUIRED_FIELDS = ['first_name', 'last_name']
  47.  
  48. >>> from django.contrib.auth import get_user_model, auhtenticate
  49. >>> u = get_user_model()
  50. >>> authenticate(username='abc@gmail.com', password='abc)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement