Guest User

Untitled

a guest
Jul 8th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. from django.db import models
  2. from django.contrib.auth.models import (
  3. BaseUserManager, AbstractBaseUser
  4. )
  5.  
  6.  
  7.  
  8. #User Model Manager
  9. class UserManager(BaseUserManager):
  10. def create_user(self, username, password=None):
  11. """
  12. Creates and saves a User with the given username and password.
  13. """
  14. if not username:
  15. raise ValueError('Error: The User you want to create must have an username, try again')
  16.  
  17. user = self.model(
  18. user=self.normalize_username(username),
  19. )
  20.  
  21. user.set_password(password)
  22. user.save(using=self._db)
  23. return user
  24.  
  25. def create_staffuser(self, username, password):
  26. """
  27. Creates and saves a staff user with the given username and password.
  28. """
  29. user = self.create_user(
  30. username,
  31. password=password,
  32. )
  33. user.staff = True
  34. user.save(using=self._db)
  35. return user
  36.  
  37. def create_superuser(self, username, password):
  38. """
  39. Creates and saves a superuser with the given username and password.
  40. """
  41. user = self.create_user(
  42. username,
  43. password=password,
  44. )
  45. user.staff = True
  46. user.admin = True
  47. user.save(using=self._db)
  48. return user
  49.  
  50.  
  51. class User(AbstractBaseUser):
  52.  
  53. #User fields
  54. user = models.CharField(verbose_name='username',max_length=30,unique=True)
  55. bio = models.TextField(max_length=5000, blank=True, null=True)
  56. pubpgp = models.TextField(blank=True, null=True)
  57.  
  58. #Account typs
  59. active = models.BooleanField(default=True)
  60. staff = models.BooleanField(default=False) # a admin user; non super-user
  61. admin = models.BooleanField(default=False) # a superuser
  62. # notice the absence of a "Password field", that's built in.
  63.  
  64. USERNAME_FIELD = 'user'
  65. REQUIRED_FIELDS = [] # Username & Password are required by default.
  66.  
  67. def get_full_name(self):
  68. # The user is identified by their Username ;)
  69. return self.user
  70.  
  71. def get_short_name(self):
  72. # The user is identified by their Username address
  73. return self.user
  74. def __str__(self):
  75. return self.user
  76.  
  77. def has_perm(self, perm, obj=None):
  78. """Does the user have a specific permission?"""
  79. # Simplest possible answer: Yes, always
  80. return True
  81.  
  82. def has_module_perms(self, app_label):
  83. """Does the user have permissions to view the app `app_label`?"""
  84. # Simplest possible answer: Yes, always
  85. return True
  86.  
  87. @property
  88. def is_staff(self):
  89. """Is the user a member of staff?"""
  90. return self.staff
  91.  
  92. @property
  93. def is_admin(self):
  94. """Is the user a admin member?"""
  95. return self.admin
  96.  
  97. @property
  98. def is_active(self):
  99. """Is the user active?"""
  100. return self.active
  101.  
  102. objects = UserManager()
  103.  
  104. AUTH_USER_MODEL = 'accounts.User'
  105.  
  106. INSTALLED_APPS = [
  107. ...
  108. 'accounts',
  109. ...
  110. ]
Add Comment
Please, Sign In to add comment