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