Guest User

Untitled

a guest
Aug 17th, 2018
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. class User(AbstractBaseUser):
  2. email = models.EmailField(
  3. verbose_name = 'email address',
  4. max_length=255,
  5. unique=True
  6. )
  7. real_name = models.CharField(max_length=20, blank=True)
  8. username = models.CharField(max_length=40, unique=True, verbose_name='username')
  9. active = models.BooleanField(default=True)
  10. staff = models.BooleanField(default=False)
  11. admin = models.BooleanField(default=False)
  12. grade = models.ForeignKey(GradeUser, on_delete=models.CASCADE, blank=True, null=True)
  13. country = models.ForeignKey(Country, on_delete=models.CASCADE, blank=True, null=True)
  14. reputation = models.IntegerField(default=0)
  15. image = models.ImageField(upload_to='accounts/media', blank=True)
  16. about_me = models.TextField(blank=True)
  17.  
  18.  
  19. USERNAME_FIELD= 'email'
  20. REQUIRED_FIELDS = ['username']
  21. objects = UserManager()
  22.  
  23. def get_email(self):
  24. return self.email
  25.  
  26. def get_username(self):
  27. return self.username
  28.  
  29. def __str__(self):
  30. return self.email
  31.  
  32. def has_perm(self, perm, obj=None):
  33. return True
  34.  
  35. def has_module_perms(self, app_label):
  36. return True
  37.  
  38. @property
  39. def is_staff(self):
  40. return self.staff
  41.  
  42. @property
  43. def is_admin(self):
  44. return self.admin
  45.  
  46. @property
  47. def is_active(self):
  48. return self.active
  49.  
  50. class UserAdminCreationForm(forms.ModelForm):
  51. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  52. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  53.  
  54. class Meta:
  55. model = User
  56. fields = ('email', 'username')
  57.  
  58. def clean_password2(self):
  59. password1 = self.cleaned_data.get("password1")
  60. password2 = self.cleaned_data.get("password2")
  61. if password1 and password2 and password1 != password2:
  62. raise forms.ValidationError("Passwords don't match")
  63. return password2
  64.  
  65. def save(self, commit=True):
  66. user = super(UserAdminCreationForm, self).save(commit=False)
  67. user.set_password(self.cleaned_data["password1"])
  68. user.active = False
  69. if commit:
  70. user.save()
  71. return user
  72.  
  73. class UserAdminChangeForm(forms.ModelForm):
  74. password = ReadOnlyPasswordHashField()
  75. class Meta:
  76. model = User
  77. fields = ('email', 'username', 'password', 'active', 'admin')
  78.  
  79. def clean_password(self):
  80. return self.initial["password"]
Add Comment
Please, Sign In to add comment