Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User(AbstractBaseUser):
- email = models.EmailField(
- verbose_name = 'email address',
- max_length=255,
- unique=True
- )
- real_name = models.CharField(max_length=20, blank=True)
- username = models.CharField(max_length=40, unique=True, verbose_name='username')
- active = models.BooleanField(default=True)
- staff = models.BooleanField(default=False)
- admin = models.BooleanField(default=False)
- grade = models.ForeignKey(GradeUser, on_delete=models.CASCADE, blank=True, null=True)
- country = models.ForeignKey(Country, on_delete=models.CASCADE, blank=True, null=True)
- reputation = models.IntegerField(default=0)
- image = models.ImageField(upload_to='accounts/media', blank=True)
- about_me = models.TextField(blank=True)
- USERNAME_FIELD= 'email'
- REQUIRED_FIELDS = ['username']
- objects = UserManager()
- def get_email(self):
- return self.email
- def get_username(self):
- return self.username
- def __str__(self):
- return self.email
- def has_perm(self, perm, obj=None):
- return True
- def has_module_perms(self, app_label):
- return True
- @property
- def is_staff(self):
- return self.staff
- @property
- def is_admin(self):
- return self.admin
- @property
- def is_active(self):
- return self.active
- class UserAdminCreationForm(forms.ModelForm):
- password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
- password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
- class Meta:
- model = User
- fields = ('email', 'username')
- def clean_password2(self):
- password1 = self.cleaned_data.get("password1")
- password2 = self.cleaned_data.get("password2")
- if password1 and password2 and password1 != password2:
- raise forms.ValidationError("Passwords don't match")
- return password2
- def save(self, commit=True):
- user = super(UserAdminCreationForm, self).save(commit=False)
- user.set_password(self.cleaned_data["password1"])
- user.active = False
- if commit:
- user.save()
- return user
- class UserAdminChangeForm(forms.ModelForm):
- password = ReadOnlyPasswordHashField()
- class Meta:
- model = User
- fields = ('email', 'username', 'password', 'active', 'admin')
- def clean_password(self):
- return self.initial["password"]
Add Comment
Please, Sign In to add comment