custom_user.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from django.contrib.auth.models import (
  2. AbstractBaseUser, AbstractUser, BaseUserManager, Group, Permission,
  3. PermissionsMixin, UserManager,
  4. )
  5. from django.db import models
  6. from django.utils.encoding import python_2_unicode_compatible
  7. # The custom User uses email as the unique identifier, and requires
  8. # that every user provide a date of birth. This lets us test
  9. # changes in username datatype, and non-text required fields.
  10. class CustomUserManager(BaseUserManager):
  11. def create_user(self, email, date_of_birth, password=None):
  12. """
  13. Creates and saves a User with the given email and password.
  14. """
  15. if not email:
  16. raise ValueError('Users must have an email address')
  17. user = self.model(
  18. email=self.normalize_email(email),
  19. date_of_birth=date_of_birth,
  20. )
  21. user.set_password(password)
  22. user.save(using=self._db)
  23. return user
  24. def create_superuser(self, email, password, date_of_birth):
  25. u = self.create_user(email, password=password, date_of_birth=date_of_birth)
  26. u.is_admin = True
  27. u.save(using=self._db)
  28. return u
  29. @python_2_unicode_compatible
  30. class CustomUser(AbstractBaseUser):
  31. email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
  32. is_active = models.BooleanField(default=True)
  33. is_admin = models.BooleanField(default=False)
  34. date_of_birth = models.DateField()
  35. custom_objects = CustomUserManager()
  36. USERNAME_FIELD = 'email'
  37. REQUIRED_FIELDS = ['date_of_birth']
  38. def get_full_name(self):
  39. return self.email
  40. def get_short_name(self):
  41. return self.email
  42. def __str__(self):
  43. return self.email
  44. # Maybe required?
  45. def get_group_permissions(self, obj=None):
  46. return set()
  47. def get_all_permissions(self, obj=None):
  48. return set()
  49. def has_perm(self, perm, obj=None):
  50. return True
  51. def has_perms(self, perm_list, obj=None):
  52. return True
  53. def has_module_perms(self, app_label):
  54. return True
  55. # Admin required fields
  56. @property
  57. def is_staff(self):
  58. return self.is_admin
  59. class RemoveGroupsAndPermissions(object):
  60. """
  61. A context manager to temporarily remove the groups and user_permissions M2M
  62. fields from the AbstractUser class, so they don't clash with the
  63. related_name sets.
  64. """
  65. def __enter__(self):
  66. self._old_au_local_m2m = AbstractUser._meta.local_many_to_many
  67. self._old_pm_local_m2m = PermissionsMixin._meta.local_many_to_many
  68. groups = models.ManyToManyField(Group, blank=True)
  69. groups.contribute_to_class(PermissionsMixin, "groups")
  70. user_permissions = models.ManyToManyField(Permission, blank=True)
  71. user_permissions.contribute_to_class(PermissionsMixin, "user_permissions")
  72. PermissionsMixin._meta.local_many_to_many = [groups, user_permissions]
  73. AbstractUser._meta.local_many_to_many = [groups, user_permissions]
  74. def __exit__(self, exc_type, exc_value, traceback):
  75. AbstractUser._meta.local_many_to_many = self._old_au_local_m2m
  76. PermissionsMixin._meta.local_many_to_many = self._old_pm_local_m2m
  77. class CustomUserWithoutIsActiveField(AbstractBaseUser):
  78. username = models.CharField(max_length=150, unique=True)
  79. email = models.EmailField(unique=True)
  80. objects = UserManager()
  81. USERNAME_FIELD = 'username'
  82. # The extension user is a simple extension of the built-in user class,
  83. # adding a required date_of_birth field. This allows us to check for
  84. # any hard references to the name "User" in forms/handlers etc.
  85. with RemoveGroupsAndPermissions():
  86. class ExtensionUser(AbstractUser):
  87. date_of_birth = models.DateField()
  88. custom_objects = UserManager()
  89. REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ['date_of_birth']