[#29809805] made username change form more pythonic

pull/1/head
eric 2012-05-26 17:30:12 -04:00
parent 61f45a5716
commit 365976933e
2 changed files with 10 additions and 14 deletions

View File

@ -151,18 +151,17 @@ class UserData(forms.Form):
'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")
}
)
oldusername = None
def clean_username(self):
username = self.data["username"]
oldusername = self.data["oldusername"]
if username != oldusername:
try:
User.objects.get(username__iexact=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(_("Another user with that username already exists."))
raise forms.ValidationError(_("Your username is already "+oldusername))
if username != self.oldusername:
users = User.objects.filter(username__iexact=username)
for user in users:
raise forms.ValidationError(_("Another user with that username already exists."))
return username
raise forms.ValidationError(_("Your username is already "+username))
class OpenCampaignForm(forms.ModelForm):
managers = AutoCompleteSelectMultipleField(

View File

@ -1382,14 +1382,11 @@ def edit_user(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('auth_login'))
form=UserData()
emailform = UserEmail({'email':request.user.email})
oldusername=request.user.username
emailform = UserEmail()
if request.method == 'POST':
if 'change_username' in request.POST.keys():
# surely there's a better way to add data to the POST data?
postcopy=request.POST.copy()
postcopy['oldusername']=oldusername
form = UserData(postcopy)
form = UserData(request.POST)
form.oldusername = request.user.username
if form.is_valid(): # All validation rules pass, go and change the username
request.user.username=form.cleaned_data['username']
request.user.save()