gremmie@1: """forms for the accounts application""" gremmie@1: gremmie@1: from django import forms gremmie@1: from django.contrib.auth.models import User gremmie@1: from django.core.urlresolvers import reverse gremmie@1: from django.template.loader import render_to_string gremmie@1: from django.contrib.sites.models import Site bgneal@6: from django.conf import settings gremmie@1: gremmie@1: from core.functions import send_mail gremmie@1: from accounts.models import PendingUser gremmie@1: from accounts.models import IllegalUsername gremmie@1: from accounts.models import IllegalEmail gremmie@1: gremmie@1: gremmie@1: class RegisterForm(forms.Form): gremmie@1: """Form used to register with the website""" gremmie@1: username = forms.RegexField(max_length = 30, regex = r'^\w+$', gremmie@1: error_messages = {'invalid' : 'Your username must be 30 characters or less and contain only letters, numbers and underscores.'}) gremmie@1: email = forms.EmailField() gremmie@1: password1 = forms.CharField(label = "Password", widget = forms.PasswordInput) gremmie@1: password2 = forms.CharField(label = "Password confirmation", widget = forms.PasswordInput) gremmie@1: agree_tos = forms.BooleanField(required = True, label = 'I agree to the Terms of Service', gremmie@1: error_messages = {'required' : 'You have not agreed to our Terms of Service'}) gremmie@1: agree_privacy = forms.BooleanField(required = True, label = 'I agree to the Privacy Policy', gremmie@1: error_messages = {'required' : 'You have not agreed to our Privacy Policy'}) gremmie@1: gremmie@1: def clean_username(self): gremmie@1: username = self.cleaned_data['username'] gremmie@1: try: gremmie@1: User.objects.get(username = username) gremmie@1: except User.DoesNotExist: gremmie@1: try: gremmie@1: PendingUser.objects.get(username = username) gremmie@1: except PendingUser.DoesNotExist: gremmie@1: try: gremmie@1: IllegalUsername.objects.get(username = username) gremmie@1: except IllegalUsername.DoesNotExist: gremmie@1: return username gremmie@1: raise forms.ValidationError("That username is not allowed.") gremmie@1: raise forms.ValidationError("A pending user with that username already exists.") gremmie@1: raise forms.ValidationError("A user with that username already exists.") gremmie@1: gremmie@1: def clean_email(self): gremmie@1: email = self.cleaned_data['email'] gremmie@1: try: gremmie@1: User.objects.get(email = email) gremmie@1: except User.DoesNotExist: gremmie@1: try: gremmie@1: PendingUser.objects.get(email = email) gremmie@1: except PendingUser.DoesNotExist: gremmie@1: try: gremmie@1: IllegalEmail.objects.get(email = email) gremmie@1: except IllegalEmail.DoesNotExist: gremmie@1: return email gremmie@1: raise forms.ValidationError("That email address is not allowed.") gremmie@1: raise forms.ValidationError("A pending user with that email already exists.") gremmie@1: raise forms.ValidationError("A user with that email already exists.") gremmie@1: gremmie@1: def clean_password2(self): gremmie@1: password1 = self.cleaned_data.get("password1", "") gremmie@1: password2 = self.cleaned_data["password2"] gremmie@1: if password1 != password2: gremmie@1: raise forms.ValidationError("The two password fields didn't match.") gremmie@1: return password2 gremmie@1: gremmie@1: def save(self): gremmie@1: pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'], gremmie@1: self.cleaned_data['email'], gremmie@1: self.cleaned_data['password1']) gremmie@1: gremmie@1: # Send the confirmation email gremmie@1: gremmie@1: site = Site.objects.get_current() bgneal@2: admin_email = settings.ADMINS[0][1] gremmie@1: gremmie@1: activation_link = 'http://%s%s' % (site.domain, reverse('accounts.views.register_confirm', gremmie@1: kwargs = {'username' : pending_user.username, 'key' : pending_user.key})) gremmie@1: gremmie@1: msg = render_to_string('accounts/registration_email.txt', gremmie@1: { gremmie@1: 'site_name' : site.name, gremmie@1: 'site_domain' : site.domain, gremmie@1: 'user_email' : pending_user.email, gremmie@1: 'activation_link' : activation_link, gremmie@1: 'username' : pending_user.username, bgneal@2: 'admin_email' : admin_email, gremmie@1: }) gremmie@1: gremmie@1: subject = 'Registration Confirmation for ' + site.name bgneal@2: send_mail(subject, msg, admin_email, [self.cleaned_data['email']]) gremmie@1: gremmie@1: return pending_user gremmie@1: