]> git.openstreetmap.org Git - osqa.git/blob - forum/authentication/forms.py
initial import
[osqa.git] / forum / authentication / forms.py
1 from forum.utils.forms import NextUrlField,  UserNameField,  UserEmailField, SetPasswordForm
2 from forum.models import Question, User
3 from django.contrib.contenttypes.models import ContentType
4 from django.utils.translation import ugettext as _
5 from django.utils.safestring import mark_safe
6 from django import forms
7 import logging
8
9 class SimpleRegistrationForm(forms.Form):
10     next = NextUrlField()
11     username = UserNameField()
12     email = UserEmailField()
13
14 class TemporaryLoginRequestForm(forms.Form):
15     def __init__(self, data=None):
16         super(TemporaryLoginRequestForm, self).__init__(data)
17         self.user_cache = None
18
19     email = forms.EmailField(
20             required=True,
21             label=_("Your account email"),
22             error_messages={
23                 'required': _("You cannot leave this field blank"),
24                 'invalid': _('please enter a valid email address'),
25             }
26     )
27
28     def clean_email(self):
29         try:
30             user = User.objects.get(email=self.cleaned_data['email'])
31         except:
32             raise forms.ValidationError(_("Sorry, but this email is not on our database."))
33
34         self.user_cache = user
35         return self.cleaned_data['email']
36
37
38 class SimpleEmailSubscribeForm(forms.Form):
39     SIMPLE_SUBSCRIBE_CHOICES = (
40         ('y',_('okay, let\'s try!')),
41         ('n',_('no OSQA community email please, thanks'))
42     )
43     subscribe = forms.ChoiceField(widget=forms.widgets.RadioSelect(), \
44                                 error_messages={'required':_('please choose one of the options above')},
45                                 choices=SIMPLE_SUBSCRIBE_CHOICES)
46
47
48 class ChangePasswordForm(SetPasswordForm):
49     """ change password form """
50     oldpw = forms.CharField(widget=forms.PasswordInput(attrs={'class':'required'}),
51                 label=mark_safe(_('Current password')))
52
53     def __init__(self, data=None, user=None, *args, **kwargs):
54         if user is None:
55             raise TypeError("Keyword argument 'user' must be supplied")
56         super(ChangePasswordForm, self).__init__(data, *args, **kwargs)
57         self.user = user
58
59     def clean_oldpw(self):
60         """ test old password """
61         if not self.user.check_password(self.cleaned_data['oldpw']):
62             raise forms.ValidationError(_("Old password is incorrect. \
63                     Please enter the correct password."))
64         return self.cleaned_data['oldpw']