Mercurial > public > sg101
comparison gpp/accounts/forms.py @ 74:df56795771a6
#13 Added logging to registration.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 01 Jul 2009 20:02:14 +0000 |
parents | 53b7c681d80b |
children | ef93dc9f1992 |
comparison
equal
deleted
inserted
replaced
73:8d391fe0ad64 | 74:df56795771a6 |
---|---|
1 """forms for the accounts application""" | 1 """forms for the accounts application""" |
2 | |
3 import logging | |
2 | 4 |
3 from django import forms | 5 from django import forms |
4 from django.contrib.auth.models import User | 6 from django.contrib.auth.models import User |
5 from django.core.urlresolvers import reverse | 7 from django.core.urlresolvers import reverse |
6 from django.template.loader import render_to_string | 8 from django.template.loader import render_to_string |
12 from accounts.models import IllegalUsername | 14 from accounts.models import IllegalUsername |
13 from accounts.models import IllegalEmail | 15 from accounts.models import IllegalEmail |
14 | 16 |
15 | 17 |
16 class RegisterForm(forms.Form): | 18 class RegisterForm(forms.Form): |
17 """Form used to register with the website""" | 19 """Form used to register with the website""" |
18 username = forms.RegexField(max_length = 30, regex = r'^\w+$', | 20 username = forms.RegexField(max_length = 30, regex = r'^\w+$', |
19 error_messages = {'invalid' : 'Your username must be 30 characters or less and contain only letters, numbers and underscores.'}) | 21 error_messages = {'invalid' : 'Your username must be 30 characters or less and contain only letters, numbers and underscores.'}) |
20 email = forms.EmailField() | 22 email = forms.EmailField() |
21 password1 = forms.CharField(label = "Password", widget = forms.PasswordInput) | 23 password1 = forms.CharField(label = "Password", widget = forms.PasswordInput) |
22 password2 = forms.CharField(label = "Password confirmation", widget = forms.PasswordInput) | 24 password2 = forms.CharField(label = "Password confirmation", widget = forms.PasswordInput) |
23 agree_tos = forms.BooleanField(required = True, label = 'I agree to the Terms of Service', | 25 agree_tos = forms.BooleanField(required = True, label = 'I agree to the Terms of Service', |
24 error_messages = {'required' : 'You have not agreed to our Terms of Service'}) | 26 error_messages = {'required' : 'You have not agreed to our Terms of Service'}) |
25 agree_privacy = forms.BooleanField(required = True, label = 'I agree to the Privacy Policy', | 27 agree_privacy = forms.BooleanField(required = True, label = 'I agree to the Privacy Policy', |
26 error_messages = {'required' : 'You have not agreed to our Privacy Policy'}) | 28 error_messages = {'required' : 'You have not agreed to our Privacy Policy'}) |
27 | 29 |
28 def clean_username(self): | 30 def __init__(self, *args, **kwargs): |
29 username = self.cleaned_data['username'] | 31 self.ip = kwargs.pop('ip', '?') |
30 try: | 32 super(RegisterForm, self).__init__(*args, **kwargs) |
31 User.objects.get(username = username) | 33 |
32 except User.DoesNotExist: | 34 def clean_username(self): |
33 try: | 35 username = self.cleaned_data['username'] |
34 PendingUser.objects.get(username = username) | 36 try: |
35 except PendingUser.DoesNotExist: | 37 User.objects.get(username = username) |
38 except User.DoesNotExist: | |
36 try: | 39 try: |
37 IllegalUsername.objects.get(username = username) | 40 PendingUser.objects.get(username = username) |
38 except IllegalUsername.DoesNotExist: | 41 except PendingUser.DoesNotExist: |
39 return username | 42 try: |
40 raise forms.ValidationError("That username is not allowed.") | 43 IllegalUsername.objects.get(username = username) |
41 raise forms.ValidationError("A pending user with that username already exists.") | 44 except IllegalUsername.DoesNotExist: |
42 raise forms.ValidationError("A user with that username already exists.") | 45 return username |
46 self._validation_error("That username is not allowed.", username) | |
47 self._validation_error("A pending user with that username already exists.", username) | |
48 self._validation_error("A user with that username already exists.", username) | |
43 | 49 |
44 def clean_email(self): | 50 def clean_email(self): |
45 email = self.cleaned_data['email'] | 51 email = self.cleaned_data['email'] |
46 try: | 52 try: |
47 User.objects.get(email = email) | 53 User.objects.get(email = email) |
48 except User.DoesNotExist: | 54 except User.DoesNotExist: |
49 try: | |
50 PendingUser.objects.get(email = email) | |
51 except PendingUser.DoesNotExist: | |
52 try: | 55 try: |
53 IllegalEmail.objects.get(email = email) | 56 PendingUser.objects.get(email = email) |
54 except IllegalEmail.DoesNotExist: | 57 except PendingUser.DoesNotExist: |
55 return email | 58 try: |
56 raise forms.ValidationError("That email address is not allowed.") | 59 IllegalEmail.objects.get(email = email) |
57 raise forms.ValidationError("A pending user with that email already exists.") | 60 except IllegalEmail.DoesNotExist: |
58 raise forms.ValidationError("A user with that email already exists.") | 61 return email |
62 self._validation_error("That email address is not allowed.", email) | |
63 self._validation_error("A pending user with that email address already exists.", email) | |
64 self._validation_error("A user with that email address already exists.", email) | |
59 | 65 |
60 def clean_password2(self): | 66 def clean_password2(self): |
61 password1 = self.cleaned_data.get("password1", "") | 67 password1 = self.cleaned_data.get("password1", "") |
62 password2 = self.cleaned_data["password2"] | 68 password2 = self.cleaned_data["password2"] |
63 if password1 != password2: | 69 if password1 != password2: |
64 raise forms.ValidationError("The two password fields didn't match.") | 70 self._validation_error("The two password fields didn't match.") |
65 return password2 | 71 return password2 |
66 | 72 |
67 def save(self): | 73 def save(self): |
68 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'], | 74 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'], |
69 self.cleaned_data['email'], | 75 self.cleaned_data['email'], |
70 self.cleaned_data['password1']) | 76 self.cleaned_data['password1']) |
71 | 77 |
72 # Send the confirmation email | 78 # Send the confirmation email |
73 | 79 |
74 site = Site.objects.get_current() | 80 site = Site.objects.get_current() |
75 admin_email = settings.ADMINS[0][1] | 81 admin_email = settings.ADMINS[0][1] |
76 | 82 |
77 activation_link = 'http://%s%s' % (site.domain, reverse('accounts.views.register_confirm', | 83 activation_link = 'http://%s%s' % (site.domain, reverse('accounts.views.register_confirm', |
78 kwargs = {'username' : pending_user.username, 'key' : pending_user.key})) | 84 kwargs = {'username' : pending_user.username, 'key' : pending_user.key})) |
79 | 85 |
80 msg = render_to_string('accounts/registration_email.txt', | 86 msg = render_to_string('accounts/registration_email.txt', |
81 { | 87 { |
82 'site_name' : site.name, | 88 'site_name' : site.name, |
83 'site_domain' : site.domain, | 89 'site_domain' : site.domain, |
84 'user_email' : pending_user.email, | 90 'user_email' : pending_user.email, |
85 'activation_link' : activation_link, | 91 'activation_link' : activation_link, |
86 'username' : pending_user.username, | 92 'username' : pending_user.username, |
87 'admin_email' : admin_email, | 93 'admin_email' : admin_email, |
88 }) | 94 }) |
89 | 95 |
90 subject = 'Registration Confirmation for ' + site.name | 96 subject = 'Registration Confirmation for ' + site.name |
91 send_mail(subject, msg, admin_email, [self.cleaned_data['email']]) | 97 send_mail(subject, msg, admin_email, [self.cleaned_data['email']]) |
98 logging.info('Accounts/registration conf. email sent to %s for user %s; IP = %s' % \ | |
99 (self.cleaned_data['email'], pending_user.username, self.ip)) | |
92 | 100 |
93 return pending_user | 101 return pending_user |
94 | 102 |
103 def _validation_error(self, msg, param=None): | |
104 logging.error('Accounts/registration [%s]: %s (%s)' % (self.ip, msg, param)) | |
105 raise forms.ValidationError(msg) |