Advertisement
Guest User

Untitled

a guest
Apr 20th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. # coding=utf-8
  2. from django.db import models
  3. from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
  4.  
  5.  
  6. class CustomUserManager(BaseUserManager):
  7. def create_user(self, email, username, date_of_birth, password=None):
  8. """
  9. Creates and saves a User with the given email, date of
  10. birth and password.
  11. :param username:
  12. :param password:
  13. :param date_of_birth:
  14. :param email:
  15. """
  16. if not email:
  17. raise ValueError('Users must have an email address')
  18.  
  19. user = self.model(
  20. email=self.normalize_email(email),
  21. username=username,
  22. date_of_birth=date_of_birth
  23. )
  24.  
  25. user.set_password(password)
  26. user.save(using=self._db)
  27. return user
  28.  
  29. def create_superuser(self, email, username, date_of_birth, password):
  30. """
  31. Creates and saves a superuser with the given email, date of
  32. birth and password.
  33. :param username:
  34. :param email:
  35. :param password:
  36. :param date_of_birth:
  37. """
  38. user = self.create_user(email,
  39. username=username,
  40. password=password,
  41. date_of_birth=date_of_birth
  42. )
  43. user.is_admin = True
  44. user.save(using=self._db)
  45. return user
  46.  
  47.  
  48. class CustomUser(AbstractBaseUser):
  49. # Fields
  50. email = models.EmailField(verbose_name='email address', max_length=96, unique=True)
  51.  
  52. username = models.TextField(max_length=32, unique=True)
  53. first_name = models.TextField(max_length=24, db_index=True, blank=True, null=True)
  54. last_name = models.TextField(max_length=24, db_index=True, blank=True, null=True)
  55. date_of_birth = models.DateField()
  56.  
  57. is_active = models.BooleanField(default=True)
  58. is_admin = models.BooleanField(default=False)
  59. subscribed_on = models.DateTimeField(auto_now_add=True, db_index=True)
  60. # End fields
  61.  
  62. objects = CustomUserManager()
  63.  
  64. USERNAME_FIELD = 'email'
  65. REQUIRED_FIELDS = ['date_of_birth', 'username']
  66.  
  67. def get_full_name(self):
  68. # The user is identified by their email address
  69. return self.email
  70.  
  71. def get_short_name(self):
  72. # The user is identified by their email address
  73. return self.email
  74.  
  75. def __str__(self): # __unicode__ on Python 2
  76. return self.email
  77.  
  78. def has_perm(self, perm, obj=None):
  79. "Does the user have a specific permission?"
  80. # Simplest possible answer: Yes, always
  81. return True
  82.  
  83. def has_module_perms(self, app_label):
  84. "Does the user have permissions to view the app `app_label`?"
  85. # Simplest possible answer: Yes, always
  86. return True
  87.  
  88. @property
  89. def is_staff(self):
  90. "Is the user a member of staff?"
  91. # Simplest possible answer: All admins are staff
  92. return self.is_admin
  93.  
  94.  
  95. class CustomGroup(models.Model):
  96. """
  97. Custom user group
  98. """
  99. name = models.CharField(max_length=48, unique=True)
  100. boss = models.ForeignKey(CustomUser, related_name='groups_owned')
  101. members = models.ManyToManyField(CustomUser, through='CustomUserInGroup')
  102.  
  103. def __unicode__(self):
  104. return self.name
  105.  
  106.  
  107. class CustomUserInGroup(models.Model):
  108. """
  109. Link between CustomUser and CustomGroup
  110. """
  111. group = models.ForeignKey(CustomGroup, related_name='customs')
  112. custom = models.ForeignKey(CustomUser, related_name='groups')
  113. joined_on = models.DateTimeField(auto_now_add=True, blank=True, null=True)
  114.  
  115. class Meta:
  116. unique_together = ('group','custom')
  117.  
  118. def __unicode__(self):
  119. return "{} in {}".format(self.custom, self.group)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement