Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.contrib.auth.admin import UserAdmin
- from django.contrib.auth.models import User, Group, Permission
- from django import forms
- #from django.contrib.auth import models
- from django.core import validators
- class GolferProfileForm(forms.ModelForm):
- def __init__(self, *args, **kwargs):
- super(GolferProfileForm, self).__init__(*args, **kwargs)
- try:
- self.fields['username'].initial = self.instance.username
- self.fields['password'].initial = self.instance.password
- self.fields['first_name'].initial = self.instance.first_name
- self.fields['last_name'].initial = self.instance.last_name
- self.fields['email'].initial = self.instance.email
- self.fields['address'].initial = self.instance.profile.address
- self.fields['user_permissions'].initial = self.instance.user_permissions
- self.fields['groups'].initial = self.instance.groups
- except User.DoesNotExist:
- pass
- username = forms.CharField(label = 'Username', required = True, max_length = 30, help_text = 'Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and under scores).')
- password = forms.CharField(label = 'Password', required = True, max_length = 128, help_text = "Use '[algo]$[salt]$[hexdigest]' or user the <a href=\"password/\">change password form</a>.")
- first_name = forms.CharField(label = 'First Name', max_length = 30, required = False)
- last_name = forms.CharField(label = 'Last Name', max_length = 30, required = False)
- email = forms.EmailField(label = 'E-Mail Address', required = True)
- address = forms.CharField(label = 'Address', required = False)
- groups = forms.ModelMultipleChoiceField(label = 'Groups', queryset = Group.objects.all(), required = False, help_text = '<a href="../../../auth/group/add/" class="add-another" id="add_id_groups" onclick="return showAddAnotherPopup(this);"><img src="/admin_media/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"></a> - Add Another Group<br />In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in. Hold down "Control", or "Command" on a Mac, to select more than one.')
- user_permissions = forms.ModelMultipleChoiceField(label = 'User Permissions', queryset = Permission.objects.all(), required = False)
- class Meta:
- model = Golfer
- exclude = ('user',)
- def save(self, *args, **kwargs):
- u = self.instance
- profile = u.profile
- u.username = self.cleaned_data['username']
- u.password = self.cleaned_data['password']
- u.first_name = self.cleaned_data['first_name']
- u.last_name = self.cleaned_data['last_name']
- u.email = self.cleaned_data['email']
- u.groups = self.cleaned_data['groups']
- u.user_permissions = self.cleaned_data['user_permissions']
- profile.address = self.cleaned_data['address']
- profile.save()
- u.save()
- profile = super(GolferProfileForm, self).save(*args, **kwargs)
- return profile
- class MyUserAdmin(UserAdmin):
- list_display = ('email', 'username', 'first_name', 'last_name', 'date_joined', 'is_staff')
- form = GolferProfileForm
- fieldsets = (
- (None, {'fields': ['username', 'password']}),
- ('Personal Info', {'fields': ['email', 'first_name', 'last_name', 'address']}),
- # ('Permissions', {'fields': ['is_staff', 'is_active', 'is_superuser', 'user_permissions'], 'classes': ['collapse']}),
- # ('Important Dates', {'fields': ['last_login', 'date_joined'], 'classes': ['collapse']}),
- ('Groups',{'fields': ['groups', 'user_permissions'], 'classes': ['collapse']}),
- )
- admin.site.unregister(User)
- admin.site.register(User, MyUserAdmin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement