|
0
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
''' |
|
|
3 |
Created on Aug 4, 2013 |
|
|
4 |
|
|
|
5 |
@author: ymh |
|
|
6 |
''' |
|
|
7 |
from django.conf import settings |
|
|
8 |
from django.contrib.auth import get_user_model |
|
|
9 |
from django.contrib.auth.forms import (UserChangeForm as AuthUserChangeForm, |
|
|
10 |
UserCreationForm as AuthUserCreationForm) |
|
|
11 |
from django.core.exceptions import ValidationError |
|
|
12 |
from django.forms.fields import ChoiceField |
|
|
13 |
from django.utils.translation import ugettext as _ |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
User = get_user_model() |
|
|
17 |
|
|
|
18 |
class UserCreationform(AuthUserCreationForm): |
|
|
19 |
class Meta: |
|
|
20 |
model = User |
|
|
21 |
|
|
|
22 |
def clean_username(self): |
|
|
23 |
# Since User.username is unique, this check is redundant, |
|
|
24 |
# but it sets a nicer error message than the ORM. See #13147. |
|
|
25 |
username = self.cleaned_data["username"] |
|
|
26 |
try: |
|
|
27 |
User.objects.get(username=username) |
|
|
28 |
except User.DoesNotExist: |
|
|
29 |
return username |
|
|
30 |
raise ValidationError(self.error_messages['duplicate_username']) |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
class UserChangeForm(AuthUserChangeForm): |
|
|
34 |
language = ChoiceField(label=_("language"), choices=[(k,_(v)) for k,v in settings.LANGUAGES], initial=settings.LANGUAGE_CODE[:2]) |
|
|
35 |
class Meta: |
|
|
36 |
model = User |