Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django import forms
- from django.contrib import admin
- from django.contrib.auth.forms import ReadOnlyPasswordHashField
- from django.utils.crypto import get_random_string
- from django.utils.encoding import smart_str
- from django.utils.translation import ugettext_lazy as _
- from django.utils import timezone
- from myauth.models import Company, MyGroup, MyUser
- class MyUserCreationForm(forms.ModelForm):
- """
- A form that creates a myuser, with no privileges, from the given username and
- password.
- """
- error_messages = {
- 'duplicate_username': _("A user with that username already exists."),
- 'password_mismatch': _("The two password fields didn't match."),
- }
- company = forms.ModelChoiceField(queryset=Company.objects.all())
- username = forms.RegexField(label=_("Username"), max_length=30,
- regex=r'^[\w.@+-]+$',
- help_text = _("Required. 30 characters or fewer. Letters, digits and "
- "@/./+/-/_ only."),
- error_messages = {
- 'invalid': _("This value may contain only letters, numbers and "
- "@/./+/-/_ characters.")})
- password1 = forms.CharField(label=_("Password"),
- widget=forms.PasswordInput)
- password2 = forms.CharField(label=_("Password confirmation"),
- widget=forms.PasswordInput,
- help_text = _("Enter the same password as above, for verification."))
- class Meta:
- model = MyUser
- fields = ("username",)
- def clean_username(self):
- # Since MyUser.username is unique, this check is redundant,
- # but it sets a nicer error message than the ORM. See #13147.
- username = self.cleaned_data["username"]
- try:
- MyUser.objects.get(username=username, company=company)
- except MyUser.DoesNotExist:
- return username
- raise forms.ValidationError(self.error_messages['duplicate_username'])
- def clean_password2(self):
- password1 = self.cleaned_data.get("password1", "")
- password2 = self.cleaned_data["password2"]
- if password1 != password2:
- raise forms.ValidationError(
- self.error_messages['password_mismatch'])
- return password2
- def save(self, commit=True):
- myuser = super(MyUserCreationForm, self).save(commit=False)
- myuser.set_password(self.cleaned_data["password1"])
- if commit:
- myuser.save()
- return user
- class MyUserChangeForm(forms.ModelForm):
- username = forms.RegexField(
- label=_("Username"), max_length=30, regex=r"^[\w.@+-]+$",
- help_text = _("Required. 30 characters or fewer. Letters, digits and "
- "@/./+/-/_ only."),
- error_messages = {
- 'invalid': _("This value may contain only letters, numbers and "
- "@/./+/-/_ characters.")})
- password = ReadOnlyPasswordHashField(label=_("Password"),
- help_text=_("Raw passwords are not stored, so there is no way to see "
- "this user's password, but you can change the password "
- "using <a href=\"password/\">this form</a>."))
- is_active= forms.BooleanField(label=_("Is Admin?"))
- is_staff= forms.BooleanField(label=_("Is Staff?"))
- is_admin= forms.BooleanField(label=_("Is Admin?"))
- def clean_password(self):
- return self.initial["password"]
- class Meta:
- model = MyUser
- def __init__(self, *args, **kwargs):
- super(MyUserChangeForm, self).__init__(*args, **kwargs)
- f = self.fields.get('user_permissions', None)
- if f is not None:
- f.queryset = f.queryset.select_related('content_type')
- class MyUserAdmin(admin.ModelAdmin):
- # The forms to add and change user instances
- form = MyUserChangeForm
- add_form = MyUserCreationForm
- list_display = ('username',)
- #list_filter = ('is_admin',)
- fieldsets = (
- (None, {'fields': ('username', 'password')}),
- ('Permissions', {'fields': ('is_admin','is_staff','is_active')}),
- )
- add_fieldsets = (
- (None, {
- 'classes': ('wide',),
- 'fields': ('company', 'username', 'password1', 'password2')}
- ),
- )
- search_fields = ('username',)
- ordering = ('username',)
- filter_horizontal = ()
- admin.site.register(Company)
- admin.site.register(MyGroup)
- admin.site.register(MyUser, MyUserAdmin)
Advertisement
Add Comment
Please, Sign In to add comment