Guest User

Untitled

a guest
Mar 1st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. from django.db import models
  2. from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
  3.  
  4. class UserManager(BaseUserManager):
  5. def create_user(self, email, full_name, address, number, password=None):
  6. """
  7. Creates and saves a User with the given email and password.
  8. """
  9. if not email:
  10. raise ValueError('Users must have an email address')
  11. if not full_name:
  12. raise ValueError('Users must have an email address')
  13. if not address:
  14. raise ValueError('Users must have an email address')
  15. if not number:
  16. raise ValueError('Users must have an email address')
  17. if not password:
  18. raise ValueError('Users must have an email address')
  19.  
  20. user = self.model(
  21. email=self.normalize_email(email.lower()),
  22. full_name=full_name,
  23. address = address,
  24. number=number,
  25. )
  26. user.set_password(password)
  27. user.save(using=self._db)
  28. return user
  29.  
  30. def create_staffuser(self, email, full_name, address, number, password):
  31. """
  32. Creates and saves a staff user with the given email and password.
  33. """
  34. user = self.create_user(
  35. email,
  36. full_name,
  37. address,
  38. numbe,
  39. password = password,
  40. )
  41. user.staff = True
  42. user.save(using=self._db)
  43. return user
  44.  
  45. def create_superuser(self, email, full_name, address, number, password):
  46. """
  47. Creates and saves a superuser with the given email and password.
  48. """
  49. user = self.create_user(
  50. email,
  51. full_name,
  52. address,
  53. number,
  54. password = password,
  55. )
  56. user.staff = True
  57. user.admin = True
  58. user.save(using=self._db)
  59. return user
  60.  
  61. class User(AbstractBaseUser):
  62. email = models.EmailField(max_length=255, unique=True)
  63. full_name = models.CharField(max_length=255, blank = False, null = False)
  64. address = models.CharField(max_length=255, blank = False, null = False)
  65. number = models.CharField(max_length=255, blank = False, null = False)
  66. active = models.BooleanField(default=True)
  67. staff = models.BooleanField(default=False) # a admin user; non super-user
  68. admin = models.BooleanField(default=False) # a superuser
  69. # notice the absence of a "Password field", that's built in.
  70.  
  71. USERNAME_FIELD = 'email'
  72. REQUIRED_FIELDS = ['full_name', 'address', 'number'] # Email & Password are required by default.
  73.  
  74. objects = UserManager()
  75.  
  76. def get_full_name(self):
  77. # The user is identified by their email address
  78. return self.email
  79.  
  80. def get_short_name(self):
  81. # The user is identified by their email address
  82. return self.email
  83.  
  84. def __str__(self): # __unicode__ on Python 2
  85. return self.email
  86.  
  87. def has_perm(self, perm, obj=None):
  88. "Does the user have a specific permission?"
  89. # Simplest possible answer: Yes, always
  90. return True
  91.  
  92. def has_module_perms(self, app_label):
  93. "Does the user have permissions to view the app `app_label`?"
  94. # Simplest possible answer: Yes, always
  95. return True
  96.  
  97. @property
  98. def is_staff(self):
  99. "Is the user a member of staff?"
  100. return self.staff
  101.  
  102. @property
  103. def is_admin(self):
  104. "Is the user a admin member?"
  105. return self.admin
  106.  
  107. @property
  108. def is_active(self):
  109. "Is the user active?"
  110. return self.active
  111.  
  112. from django.contrib import admin
  113. from django.contrib.auth.models import Group
  114. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  115.  
  116. from .forms import UserAdminChangeForm, UserAdminCreationForm
  117. from .models import User
  118.  
  119. class UserAdmin(BaseUserAdmin):
  120. # The forms to add and change user instances
  121. form = UserAdminChangeForm
  122. add_form = UserAdminCreationForm
  123.  
  124. # The fields to be used in displaying the User model.
  125. # These override the definitions on the base UserAdmin
  126. # that reference specific fields on auth.User.
  127. list_display = ('email', 'admin')
  128. list_filter = ('admin',)
  129. fieldsets = (
  130. (None, {'fields': ('email', 'password')}),
  131. ('Personal info', {'fields': ('full_name', 'address', 'number')}),
  132. ('Permissions', {'fields': ('admin', 'active', 'staff')}),
  133. )
  134. # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
  135. # overrides get_fieldsets to use this attribute when creating a user.
  136. add_fieldsets = (
  137. (None, {
  138. 'classes': ('wide',),
  139. 'fields': ('email', 'full_name', 'address', 'number', 'password1', 'password2')}
  140. ),
  141. )
  142. search_fields = ('email',)
  143. ordering = ('email',)
  144. filter_horizontal = ()
  145.  
  146.  
  147. admin.site.register(User, UserAdmin)
  148. admin.site.unregister(Group)
  149.  
  150. from django import forms
  151. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  152.  
  153. from .models import User
  154.  
  155. class UserAdminCreationForm(forms.ModelForm):
  156. """A form for creating new users. Includes all the required
  157. fields, plus a repeated password."""
  158. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  159. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  160.  
  161. class Meta:
  162. model = User
  163. fields = ('email', 'full_name', 'address', 'number')
  164.  
  165. def clean_password2(self):
  166. # Check that the two password entries match
  167. password1 = self.cleaned_data.get("password1")
  168. password2 = self.cleaned_data.get("password2")
  169. if password1 and password2 and password1 != password2:
  170. raise forms.ValidationError("Passwords don't match")
  171. return password2
  172.  
  173. def save(self, commit=True):
  174. # Save the provided password in hashed format
  175. user = super(UserAdminCreationForm, self).save(commit=False)
  176. user.set_password(self.cleaned_data["password1"])
  177. if commit:
  178. user.save()
  179. return user
  180.  
  181.  
  182. class UserAdminChangeForm(forms.ModelForm):
  183. """A form for updating users. Includes all the fields on
  184. the user, but replaces the password field with admin's
  185. password hash display field.
  186. """
  187. password = ReadOnlyPasswordHashField()
  188.  
  189. class Meta:
  190. model = User
  191. fields = ('email', 'full_name', 'address', 'number', 'password', 'active', 'admin')
  192.  
  193. def clean_password(self):
  194. # Regardless of what the user provides, return the initial value.
  195. # This is done here, rather than on the field, because the
  196. # field does not have access to the initial value
  197. return self.initial["password"]
Add Comment
Please, Sign In to add comment