/usermgr/forms.py
https://github.com/dmkm2011/vinylmgr · Python · 95 lines · 84 code · 10 blank · 1 comment · 8 complexity · ae3f6e4743ca9850e0ed616e47c760d5 MD5 · raw file
- from django.contrib.sites.models import Site
- from django.contrib.auth.models import User
- from usermgr.models import UserProfile
- from django.contrib.auth.tokens import default_token_generator
- from django.utils.http import int_to_base36
- from django.template import Context, loader
- from django import forms
- from django.core.mail import send_mail
- class UserCreationForm(forms.ModelForm):
- username = forms.RegexField(label="Username", max_length=30, regex=r'^[\w.@+-]+$',
- help_text="Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.",
- error_messages = {'invalid': "This value may contain only letters, numbers and @/./+/-/_ characters."})
- password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
- password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput,
- help_text = "Enter the same password as above, for verification.")
- email1 = forms.EmailField(label="Email", max_length=75)
- email2 = forms.EmailField(label="Email confirmation", max_length=75,
- help_text = "Enter your email address again. A confirmation email will be sent to this address.")
- firstname = forms.CharField(label="First name", widget=forms.TextInput)
- lastname = forms.CharField(label="Last name", widget=forms.TextInput)
-
- class Meta:
- model = UserProfile
- fields = ("username",)
- def clean_password2(self):
- password1 = self.cleaned_data.get("password1", "")
- password2 = self.cleaned_data["password2"]
- if password1 != password2:
- raise forms.ValidationError("The two password fields didn't match.")
- return password2
-
- def clean_email1(self):
- email1 = self.cleaned_data["email1"]
- users_found = User.objects.filter(email__iexact=email1)
- if len(users_found) >= 1:
- raise forms.ValidationError("A user with that email already exist.")
- return email1
- def clean_email2(self):
- email1 = self.cleaned_data.get("email1", "")
- email2 = self.cleaned_data["email2"]
- if email1 != email2:
- raise forms.ValidationError("The two email fields didn't match.")
- return email2
- def save(self, commit=True, domain_override=None,
- email_template_name='usermgr/password_signup_email.html',
- use_https=False, token_generator=default_token_generator):
- user = super(UserCreationForm, self).save(commit=False)
- user.first_name = self.cleaned_data["firstname"]
- user.last_name = self.cleaned_data["lastname"]
- user.set_password(self.cleaned_data["password1"])
- user.email = self.cleaned_data["email1"]
- user.is_active = False
- if commit:
- user.save()
- if not domain_override:
- current_site = Site.objects.get_current()
- site_name = current_site.name
- domain = current_site.domain
- else:
- site_name = domain = domain_override
- t = loader.get_template(email_template_name)
- c = {
- 'email': user.email,
- 'domain': domain,
- 'site_name': site_name,
- 'uid': int_to_base36(user.id),
- 'user': user,
- 'token': token_generator.make_token(user),
- 'protocol': use_https and 'https' or 'http',
- }
- send_mail("Confirmation link sent on %s" % site_name,
- t.render(Context(c)), 'DMKMvinylmgr@gmail.com', [user.email])
- return user
-
- class ProfileEditForm(forms.ModelForm):
- firstname = forms.CharField(label="First name", widget=forms.TextInput)
- lastname = forms.CharField(label="Last name", widget=forms.TextInput)
-
- class Meta:
- model = UserProfile
- fields = ('firstname', 'lastname', 'personal_page', 'avatar',
- 'published_tracklist', 'published_ownedlist', 'biography')
- def save(self):
- u = super(ProfileEditForm, self).save(commit=False)
- u.user.first_name = self.cleaned_data['firstname']
- u.user.last_name = self.cleaned_data['lastname']
- #u.user.is_active = False
- u.user.save()
- u.save()