forked from flashingpumpkin/django-socialregistration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
forms.py
30 lines (25 loc) · 1.02 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
class UserForm(forms.Form):
username = forms.RegexField(r'^\w+$', max_length=32, label=_("Username"))
email = forms.EmailField(required=False, label=_("Email"))
def __init__(self, user, profile, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
self.user = user
self.profile = profile
def clean_username(self):
username = self.cleaned_data.get('username')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return username
else:
raise forms.ValidationError(_('This username is already in use.'))
def save(self, request=None):
self.user.username = self.cleaned_data.get('username')
self.user.email = self.cleaned_data.get('email')
self.user.save()
self.profile.user = self.user
self.profile.save()
return self.user