diff -r 000000000000 -r 81e7900b06a7 src/p4l/forms.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/p4l/forms.py Tue Aug 27 11:01:32 2013 +0200 @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +''' +Created on Aug 4, 2013 + +@author: ymh +''' +from django.conf import settings +from django.contrib.auth import get_user_model +from django.contrib.auth.forms import (UserChangeForm as AuthUserChangeForm, + UserCreationForm as AuthUserCreationForm) +from django.core.exceptions import ValidationError +from django.forms.fields import ChoiceField +from django.utils.translation import ugettext as _ + + +User = get_user_model() + +class UserCreationform(AuthUserCreationForm): + class Meta: + model = User + + def clean_username(self): + # Since User.username is unique, this check is redundant, + # but it sets a nicer error message than the ORM. See #13147. + username = self.cleaned_data["username"] + try: + User.objects.get(username=username) + except User.DoesNotExist: + return username + raise ValidationError(self.error_messages['duplicate_username']) + + +class UserChangeForm(AuthUserChangeForm): + language = ChoiceField(label=_("language"), choices=[(k,_(v)) for k,v in settings.LANGUAGES], initial=settings.LANGUAGE_CODE[:2]) + class Meta: + model = User