Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. from django.contrib.auth.admin import UserAdmin
  2. from django.contrib.auth.models import User, Group, Permission
  3. from django import forms
  4. #from django.contrib.auth import models
  5. from django.core import validators
  6.  
  7. class GolferProfileForm(forms.ModelForm):
  8.     def __init__(self, *args, **kwargs):
  9.         super(GolferProfileForm, self).__init__(*args, **kwargs)
  10.         try:
  11.             self.fields['username'].initial = self.instance.username
  12.             self.fields['password'].initial = self.instance.password
  13.             self.fields['first_name'].initial = self.instance.first_name
  14.             self.fields['last_name'].initial = self.instance.last_name
  15.             self.fields['email'].initial = self.instance.email
  16.             self.fields['address'].initial = self.instance.profile.address
  17.  
  18.             self.fields['user_permissions'].initial = self.instance.user_permissions
  19.             self.fields['groups'].initial = self.instance.groups
  20.         except User.DoesNotExist:
  21.             pass
  22.  
  23.     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).')
  24.     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>.")
  25.  
  26.     first_name = forms.CharField(label = 'First Name', max_length = 30, required = False)
  27.     last_name = forms.CharField(label = 'Last Name', max_length = 30, required = False)
  28.  
  29.     email = forms.EmailField(label = 'E-Mail Address', required = True)
  30.     address = forms.CharField(label = 'Address', required = False)
  31.  
  32.     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.')
  33.     user_permissions = forms.ModelMultipleChoiceField(label = 'User Permissions', queryset = Permission.objects.all(), required = False)
  34.  
  35.     class Meta:
  36.         model = Golfer
  37.         exclude = ('user',)
  38.  
  39.     def save(self, *args, **kwargs):
  40.         u = self.instance
  41.         profile = u.profile
  42.         u.username = self.cleaned_data['username']
  43.         u.password = self.cleaned_data['password']
  44.         u.first_name = self.cleaned_data['first_name']
  45.         u.last_name = self.cleaned_data['last_name']
  46.         u.email = self.cleaned_data['email']
  47.         u.groups = self.cleaned_data['groups']
  48.         u.user_permissions = self.cleaned_data['user_permissions']
  49.  
  50.         profile.address = self.cleaned_data['address']
  51.         profile.save()
  52.         u.save()
  53.         profile = super(GolferProfileForm, self).save(*args, **kwargs)
  54.         return profile
  55.  
  56. class MyUserAdmin(UserAdmin):
  57.     list_display = ('email', 'username', 'first_name', 'last_name', 'date_joined', 'is_staff')
  58.  
  59.     form = GolferProfileForm
  60.     fieldsets = (
  61.         (None, {'fields': ['username', 'password']}),
  62.         ('Personal Info', {'fields': ['email', 'first_name', 'last_name', 'address']}),
  63. #            ('Permissions', {'fields': ['is_staff', 'is_active', 'is_superuser', 'user_permissions'], 'classes': ['collapse']}),
  64. #            ('Important Dates', {'fields': ['last_login', 'date_joined'], 'classes': ['collapse']}),
  65.         ('Groups',{'fields': ['groups', 'user_permissions'], 'classes': ['collapse']}),
  66.     )
  67.  
  68. admin.site.unregister(User)
  69. admin.site.register(User, MyUserAdmin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement