src/p4l/forms.py
author ymh <ymh.work@gmail.com>
Sat, 28 Sep 2013 02:55:26 +0200
changeset 120 6ec0300b626e
parent 113 c05567404888
child 126 a345f1a67bf1
permissions -rw-r--r--
- update django - clear search input - footer

# -*- 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