Last active
August 29, 2015 14:03
-
-
Save jkatz/2ce3347526384b283d87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/pgweb/account/admin.py b/pgweb/account/admin.py | |
index 09b51b4..beb1a01 100644 | |
--- a/pgweb/account/admin.py | |
+++ b/pgweb/account/admin.py | |
@@ -1,4 +1,7 @@ | |
from django.contrib import admin | |
+from django.contrib.auth.admin import UserAdmin | |
+from django.contrib.auth.forms import UserChangeForm | |
+from django.contrib.auth.models import User | |
from django import forms | |
import base64 | |
@@ -23,5 +26,27 @@ class CommunityAuthSiteAdminForm(forms.ModelForm): | |
class CommunityAuthSiteAdmin(admin.ModelAdmin): | |
form = CommunityAuthSiteAdminForm | |
- | |
+class PGUserChangeForm(UserChangeForm): | |
+ """just like UserChangeForm, butremoves "username" requirement""" | |
+ def __init__(self, *args, **kwargs): | |
+ super(PGUserChangeForm, self).__init__(*args, **kwargs) | |
+ # because the auth.User model is set to "blank=False" and the Django | |
+ # auth.UserChangeForm is setup as a ModelForm, it will always validate | |
+ # the "username" even though it is not present. Thus the best way to | |
+ # avoid the validation is to remove the "username" field, if it exists | |
+ if self.fields.get('username'): | |
+ del self.fields['username'] | |
+ | |
+class PGUserAdmin(UserAdmin): | |
+ """overrides default Django user admin""" | |
+ form = PGUserChangeForm | |
+ | |
+ def get_readonly_fields(self, request, obj=None): | |
+ """this prevents users from changing a username once created""" | |
+ if obj: | |
+ return self.readonly_fields + ('username',) | |
+ return self.readonly_fields | |
+ | |
admin.site.register(CommunityAuthSite, CommunityAuthSiteAdmin) | |
+admin.site.unregister(User) # have to unregister default User Admin... | |
+admin.site.register(User, PGUserAdmin) # ...in order to add overrides | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment