comparison accounts/forms.py @ 659:8e6b8ffe5f34

For issue #31, implement a forgot username feature.
author Brian Neal <bgneal@gmail.com>
date Sat, 11 May 2013 23:39:46 -0500
parents ee87ea74d46b
children 988782c6ce6c
comparison
equal deleted inserted replaced
658:2adf01661ac5 659:8e6b8ffe5f34
78 self._validation_error("A user with that email address already exists.", email) 78 self._validation_error("A user with that email address already exists.", email)
79 elif PendingUser.objects.filter(email=email).count(): 79 elif PendingUser.objects.filter(email=email).count():
80 self._validation_error("A pending user with that email address already exists.", email) 80 self._validation_error("A pending user with that email address already exists.", email)
81 elif IllegalEmail.objects.filter(email=email).count(): 81 elif IllegalEmail.objects.filter(email=email).count():
82 self._validation_error("That email address is not allowed.", email) 82 self._validation_error("That email address is not allowed.", email)
83 83
84 # email is ok 84 # email is ok
85 return email 85 return email
86 86
87 def clean_password2(self): 87 def clean_password2(self):
88 password1 = self.cleaned_data.get("password1", "") 88 password1 = self.cleaned_data.get("password1", "")
139 'username' : pending_user.username, 139 'username' : pending_user.username,
140 'admin_email' : admin_email, 140 'admin_email' : admin_email,
141 }) 141 })
142 142
143 subject = 'Registration Confirmation for ' + site.name 143 subject = 'Registration Confirmation for ' + site.name
144 send_mail(subject, msg, admin_email, [self.cleaned_data['email']]) 144 send_mail(subject, msg, admin_email, [self.cleaned_data['email']],
145 defer=False)
145 logging.info('Accounts/registration conf. email sent to %s for user %s; IP = %s', 146 logging.info('Accounts/registration conf. email sent to %s for user %s; IP = %s',
146 self.cleaned_data['email'], pending_user.username, self.ip) 147 self.cleaned_data['email'], pending_user.username, self.ip)
147 148
148 return pending_user 149 return pending_user
149 150
150 def _validation_error(self, msg, param=None): 151 def _validation_error(self, msg, param=None):
151 logging.error('Accounts/registration [%s]: %s (%s)', self.ip, msg, param) 152 logging.error('Accounts/registration [%s]: %s (%s)', self.ip, msg, param)
152 raise forms.ValidationError(msg) 153 raise forms.ValidationError(msg)
154
155
156 class ForgotUsernameForm(forms.Form):
157 """Form used to recover lost username"""
158 email = forms.EmailField(widget=forms.TextInput(attrs={'class': 'text'}))
159
160 def save(self):
161 """Email the username associated with the email address to the user."""
162 email = self.cleaned_data['email']
163 try:
164 user = User.objects.get(email=email)
165 except User.DoesNotExist:
166 # nothing to do; we don't tell the user as this gives away info
167 # about our database
168 return
169
170 site = Site.objects.get_current()
171 admin_email = settings.ADMINS[0][1]
172
173 subject = 'Forgotten username for %s' % site.name
174 msg = render_to_string('accounts/forgot_user_email.txt', {
175 'user': user,
176 'site': site,
177 'admin_email': admin_email,
178 })
179 send_mail(subject, msg, admin_email, [email], defer=False)
180
181 logging.info('Forgotten username email sent to {} <{}>'.format(
182 user.username, email))