Guest User

Untitled

a guest
Jan 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. from django.contrib import admin
  2. from django.contrib.auth.models import Group
  3. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  4.  
  5.  
  6. from .forms import UserAdminPostForm, UserAdminUpdateForm
  7. from .models import User
  8.  
  9.  
  10. class UserAdmin(BaseUserAdmin):
  11.  
  12. form = UserAdminUpdateForm
  13. add_form = UserAdminPostForm
  14.  
  15. list_display = ('email', 'created', 'admin')
  16. list_filter = ('admin',)
  17. fieldsets = (
  18. ('Account', {'fields': ('email', 'password')}),
  19. ('Profile', {'fields': ()}),
  20. ('Permissions', {'fields': ('active', 'staff', 'admin',)}),
  21. )
  22.  
  23. add_fieldsets = (
  24. (None, {
  25. 'classes': ('wide',),
  26. 'fields': ('email', 'password1', 'password2', 'staff', 'admin',)}
  27. ),
  28. )
  29. search_fields = ('email',)
  30. ordering = ('email',)
  31. filter_horizontal = ()
  32.  
  33.  
  34. admin.site.register(User, UserAdmin)
  35.  
  36. from django import forms
  37. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  38.  
  39. from .models import User
  40.  
  41. class UserAdminPostForm(forms.ModelForm):
  42. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  43. password2 = forms.CharField(label='Password Confirmation', widget=forms.PasswordInput)
  44.  
  45. class Meta:
  46. model = User
  47. fields = ('email', )
  48.  
  49. def confirm_password(self):
  50. password1 = self.cleaned_data.get("password1")
  51. password2 = self.cleaned_data.get("password2")
  52. if password1 and password2 and password1 != password2:
  53. raise forms.ValidationError("Passwords don't match")
  54. return password2
  55.  
  56. def save(self, commit=True):
  57. user = super(UserAdminPostForm, self).save(commit=False)
  58. user.set_password(self.cleaned_data["password2"])
  59. if commit:
  60. user.save()
  61. return user
  62.  
  63.  
  64. class UserAdminUpdateForm(forms.ModelForm):
  65.  
  66. password = ReadOnlyPasswordHashField()
  67.  
  68. class Meta:
  69. model = User
  70. fields = ('email', 'password', 'active', 'staff', 'admin', )
  71.  
  72. def confirm_password(self):
  73. return self.initial["password"]
  74.  
  75. import uuid
  76. from django.db import models
  77. from django.contrib.auth.models import (BaseUserManager, AbstractBaseUser)
  78.  
  79.  
  80. def hex_uuid():
  81. return uuid.uuid4().hex
  82.  
  83.  
  84. class Model(models.Model):
  85.  
  86. class Meta:
  87. abstract = True
  88.  
  89. id = models.CharField(
  90. verbose_name='id',
  91. max_length=32,
  92. primary_key=True,
  93. default=hex_uuid,
  94. editable=False,
  95. unique=True, )
  96.  
  97. created = models.DateTimeField(
  98. verbose_name='created',
  99. auto_now_add=True, )
  100.  
  101. modified = models.DateTimeField(
  102. verbose_name='updated',
  103. auto_now=True, )
  104.  
  105.  
  106. class UserManager(BaseUserManager):
  107.  
  108. def create_user(self, email, password):
  109. if not email:
  110. raise ValueError('Users must have an email')
  111. if not password:
  112. raise ValueError('Users must have a password')
  113.  
  114. user = self.model(email=self.normalize_email(email), )
  115. user.set_password(password)
  116. user.save(using=self._db)
  117. return user
  118.  
  119. def create_staffuser(self, email, password):
  120. user = self.create_user(email=email, password=password, )
  121. user.staff = True
  122. user.save(using=self._db)
  123. return user
  124.  
  125. def create_superuser(self, email, password):
  126. user = self.create_user(email=email, password=password, )
  127. user.staff = True
  128. user.admin = True
  129. user.save(using=self._db)
  130. return user
  131.  
  132.  
  133. class User(AbstractBaseUser, Model):
  134.  
  135. objects = UserManager()
  136.  
  137. email = models.EmailField(max_length=255, unique=True, )
  138. active = models.BooleanField(default=True, )
  139. staff = models.BooleanField(default=False, )
  140. admin = models.BooleanField(default=False, )
  141.  
  142. USERNAME_FIELD = 'email'
  143. REQUIRED_FIELDS = []
  144.  
  145. def __str__(self):
  146. return self.email
  147.  
  148. def has_perm(self, perm, obj=None):
  149. print(self)
  150. print(perm)
  151. print(obj)
  152. return True
  153.  
  154. def has_module_perms(self, app_label):
  155. print(self)
  156. print(app_label)
  157. return True
  158.  
  159. @property
  160. def is_staff(self):
  161. return self.staff
  162.  
  163. @property
  164. def is_admin(self):
  165. return self.admin
  166.  
  167. @property
  168. def is_active(self):
  169. return self.active
Add Comment
Please, Sign In to add comment