summaryrefslogtreecommitdiffstats
path: root/forum/authentication/forms.py
blob: 7fa06b0158564082b1dcdf990d2785d65c9ef6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from forum.utils.forms import NextUrlField,  UserNameField,  UserEmailField, SetPasswordForm
from forum.models import EmailFeedSetting, Question, User
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from django import forms
from forum.forms import EditUserEmailFeedsForm
import logging

class SimpleRegistrationForm(forms.Form):
    next = NextUrlField()
    username = UserNameField()
    email = UserEmailField()

class TemporaryLoginRequestForm(forms.Form):
    def __init__(self, data=None):
        super(TemporaryLoginRequestForm, self).__init__(data)
        self.user_cache = None

    email = forms.EmailField(
            required=True,
            label=_("Your account email"),
            error_messages={
                'required': _("You cannot leave this field blank"),
                'invalid': _('please enter a valid email address'),
            }
    )

    def clean_email(self):
        try:
            user = User.objects.get(email=self.cleaned_data['email'])
        except:
            raise forms.ValidationError(_("Sorry, but this email is not on our database."))

        self.user_cache = user
        return self.cleaned_data['email']


class SimpleEmailSubscribeForm(forms.Form):
    SIMPLE_SUBSCRIBE_CHOICES = (
        ('y',_('okay, let\'s try!')),
        ('n',_('no OSQA community email please, thanks'))
    )
    subscribe = forms.ChoiceField(widget=forms.widgets.RadioSelect(), \
                                error_messages={'required':_('please choose one of the options above')},
                                choices=SIMPLE_SUBSCRIBE_CHOICES)

    def save(self,user=None):
        EFF = EditUserEmailFeedsForm
        if self.cleaned_data['subscribe'] == 'y':
            email_settings_form = EFF()
            logging.debug('%s wants to subscribe' % user.username)
        else:
            email_settings_form = EFF(initial=EFF.NO_EMAIL_INITIAL)
        email_settings_form.save(user,save_unbound=True)

class ChangePasswordForm(SetPasswordForm):
    """ change password form """
    oldpw = forms.CharField(widget=forms.PasswordInput(attrs={'class':'required'}),
                label=mark_safe(_('Current password')))

    def __init__(self, data=None, user=None, *args, **kwargs):
        if user is None:
            raise TypeError("Keyword argument 'user' must be supplied")
        super(ChangePasswordForm, self).__init__(data, *args, **kwargs)
        self.user = user

    def clean_oldpw(self):
        """ test old password """
        if not self.user.check_password(self.cleaned_data['oldpw']):
            raise forms.ValidationError(_("Old password is incorrect. \
                    Please enter the correct password."))
        return self.cleaned_data['oldpw']