Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## ADMIN.PY ##
- from django.contrib import admin
- from django import forms
- from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
- from django.contrib.auth.forms import ReadOnlyPasswordHashField
- from django.contrib.auth.models import Group
- from myApp.models import MyUser, RegularUser, AdminUser
- # Register your models here.
- class UserCreationForm(forms.ModelForm):
- """A form for creating new users. Includes all the required fields,
- plus a repeated password"""
- password1 = forms.CharField(label='Contraseña', widget=forms.PasswordInput)
- password2 = forms.CharField(label='Repita Contraseña',
- widget=forms.PasswordInput)
- class Meta:
- model = MyUser
- fields = ('email',
- 'first_name',
- 'last_name',
- 'telefono',
- 'avatar',
- 'groups',)
- def clean_password2(self):
- # Check that the two password entries match
- password1 = self.cleaned_data.get("password1")
- password2 = self.cleaned_data.get("password2")
- if password1 and password2 and password1 != password2:
- raise forms.ValidationError("Las contraseñas no coinciden")
- return password2
- def save(self, commit=True):
- # Save the provided password in hashed format
- user = super().save(commit=False)
- user.set_password(self.cleaned_data["password1"])
- if commit:
- user.save()
- return user
- class UserChangeForm(forms.ModelForm):
- """A form for updating users. Includes all the fields on the user
- , but replaces the password field with admin's password
- hash display field"""
- password = ReadOnlyPasswordHashField()
- class Meta:
- model = MyUser
- fields = ('username',
- 'email',
- 'password',
- 'first_name',
- 'last_name',
- 'descripcion',
- 'telefono',
- 'avatar',
- )
- def clean_password(self):
- # Regardless of what the user provides, return the initial value.
- # This is done here, rather than on the field, because the
- # field does not have access to the initial value
- return self.initial["password"]
- class AdminCreationForm(forms.ModelForm):
- """A form for creating new Admin users. Including all required fields,
- plus a repeated password"""
- password1 = forms.CharField(label='Contraseña', widget=forms.PasswordInput)
- password2 = forms.CharField(label='Repita Contraseña', widget=forms.PasswordInput)
- # usuarios = forms.CharField(label= 'Usuarios', widget=forms.SelectMultiple(choices=RegularUser.objects.all()))
- class Meta:
- model = AdminUser
- fields = ('username',
- 'email',
- 'password',
- 'telefono',
- 'avatar',
- 'usuarios',)
- def clean_password2(self):
- # Check that the 2 password entries match
- password1 = self.cleaned_data.get('password1')
- password2 = self.cleaned_data.get('password2')
- if password1 and password2 and password1 != password2:
- raise ValueError("Las contraseñas no coinciden")
- return password2
- def save(self, commit=True):
- # Save the commit password in hashed form
- user = super().save(commit=False)
- user.set_password(self.cleaned_data['password1'])
- # Set the current User as admin user
- user.is_staff = True
- if commit:
- user.save()
- group = Group.objects.get(name="Administrators")
- group.user_set.add(user)
- group.save()
- return user
- class AdminChangeForm(forms.ModelForm):
- """ A form for updating Administrators. Includes all the fields on the user
- , but replaces the password field with admin's password hash display field"""
- password = ReadOnlyPasswordHashField()
- class Meta:
- model = AdminUser
- fields = ('username',
- 'email',
- 'password',
- 'first_name',
- 'last_name',
- 'descripcion',
- 'telefono',
- 'avatar',
- 'usuarios',
- 'groups',
- )
- def clean_password(self):
- # Regardless of what the admin provides, return the initial value.
- # This is done here, rather than on the field, because the
- # field does not have access to the initial value
- return self.initial["password"]
- class AdminUserAdmin(BaseUserAdmin):
- # The forms to add and change admin instances
- form = AdminChangeForm
- add_form = AdminCreationForm
- # The fields to be used in displaying the Admin model.
- # These overrides the definitions on the base AdminUserAdmin
- # that reference specific fields on auth.User
- list_display = ('username', 'email',)
- list_filter = ('last_login',)
- fieldsets = (
- (None, {'fields': ('email', 'password')}),
- ('Información Personal', {'fields': ('first_name', 'last_name', 'descripcion', 'avatar', 'telefono',)}),
- ('Administración', {'fields': ('is_staff', 'usuarios',)}),
- )
- # add_fieldsets is not a standard Modeladmin attribute. UserAdmin
- # overrides get_fieldsets to use this attribute when creating a user.
- add_fieldsets = (
- (None, {
- 'classes': ('wide',),
- 'fields': ('username', 'email', 'telefono', 'password1', 'password2', 'usuarios',)}
- ),
- )
- search_fields = ('username',)
- ordering = ('username',)
- filter_horizontal = ()
- class UserAdmin(BaseUserAdmin):
- # The forms to add and change user instances
- form = UserChangeForm
- add_form = UserCreationForm
- # The fields to be used in displaying the User model.
- # These override the definitions on the base UserAdmin
- # that reference specific fields on auth.User.
- list_display = ('username', 'email', 'is_staff')
- list_filter = ('is_staff',)
- fieldsets = (
- (None, {'fields': ('email', 'password')}),
- ('Personal info', {'fields': ('first_name', 'last_name', 'descripcion', 'avatar', 'telefono',)}),
- ('Permissions', {'fields': ('is_staff', 'is_superuser')}),
- )
- # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
- # overrides get_fieldsets to use this attribute when creating a user.
- add_fieldsets = (
- (None, {
- 'classes': ('wide',),
- 'fields': ('username', 'email', 'telefono', 'password1', 'password2',)}
- ),
- )
- search_fields = ('username',)
- ordering = ('username',)
- filter_horizontal = ()
- # Now register the new UserAdmin...
- admin.site.register(MyUser, UserAdmin)
- admin.site.register(AdminUser, AdminUserAdmin)
- # @admin.register(MyUser)
- # class MyUserAdmin(admin.ModelAdmin):
- # pass
- # @admin.register(AdminUser)
- # class AdminUserAdmin(admin.ModelAdmin):
- # # The forms to add and change Admin instances:
- # form = AdminChangeForm
- @admin.register(RegularUser)
- class RegularUserAdmin(admin.ModelAdmin):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement