custom_permissions.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. """
  2. The CustomPermissionsUser users email as the identifier, but uses the normal
  3. Django permissions model. This allows us to check that the PermissionsMixin
  4. includes everything that is needed to interact with the ModelBackend.
  5. """
  6. from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
  7. from django.db import models
  8. from .custom_user import CustomUserManager, RemoveGroupsAndPermissions
  9. class CustomPermissionsUserManager(CustomUserManager):
  10. def create_superuser(self, email, password, date_of_birth):
  11. u = self.create_user(email, password=password, date_of_birth=date_of_birth)
  12. u.is_superuser = True
  13. u.save(using=self._db)
  14. return u
  15. with RemoveGroupsAndPermissions():
  16. class CustomPermissionsUser(AbstractBaseUser, PermissionsMixin):
  17. email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
  18. date_of_birth = models.DateField()
  19. custom_objects = CustomPermissionsUserManager()
  20. USERNAME_FIELD = 'email'
  21. REQUIRED_FIELDS = ['date_of_birth']
  22. def __str__(self):
  23. return self.email