Skip to content

Commit

Permalink
Changed text fields on User and Group selection forms in the admin to…
Browse files Browse the repository at this point in the history
… be ModelChoiceField
  • Loading branch information
boardman committed Nov 21, 2011
1 parent 603049d commit b22e805
Showing 1 changed file with 2 additions and 33 deletions.
35 changes: 2 additions & 33 deletions guardian/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,39 +345,8 @@ def get_obj_perms_manage_group_form(self):


class UserManage(forms.Form):
user = forms.RegexField(label=_("Username"), max_length=30,
regex=r'^[\w.@+-]+$',
error_messages = {
'invalid': _("This value may contain only letters, numbers and "
"@/./+/-/_ characters."),
'does_not_exist': _("This user does not exist")})

def clean_user(self):
"""
Returns ``User`` instance based on the given username.
"""
username = self.cleaned_data['user']
try:
user = User.objects.get(username=username)
return user
except User.DoesNotExist:
raise forms.ValidationError(
self.fields['user'].error_messages['does_not_exist'])
user = forms.ModelChoiceField(queryset=User.objects.filter(is_active=True))


class GroupManage(forms.Form):
group = forms.CharField(max_length=80, error_messages={'does_not_exist':
_("This group does not exist")})

def clean_group(self):
"""
Returns ``Group`` instance based on the given group name.
"""
name = self.cleaned_data['group']
try:
group = Group.objects.get(name=name)
return group
except Group.DoesNotExist:
raise forms.ValidationError(
self.fields['group'].error_messages['does_not_exist'])

group = forms.ModelChoiceField(queryset=Group.objects.all())

0 comments on commit b22e805

Please sign in to comment.