]> git.openstreetmap.org Git - osqa.git/blob - forum/utils/forms.py
fix in email sender, it was aplitting the attachments, or creating multiple, don...
[osqa.git] / forum / utils / forms.py
1 from django import forms
2 import re
3 from django.utils.translation import ugettext as _
4 from django.utils.safestring import mark_safe
5 from django.conf import settings
6 from django.http import str_to_unicode
7 from forum.models import User
8 import urllib
9
10 DEFAULT_NEXT = '/' + getattr(settings, 'FORUM_SCRIPT_ALIAS')
11 def clean_next(next):
12     if next is None:
13         return DEFAULT_NEXT
14     next = str_to_unicode(urllib.unquote(next), 'utf-8')
15     next = next.strip()
16     if next.startswith('/'):
17         return next
18     return DEFAULT_NEXT
19
20 def get_next_url(request):
21     return clean_next(request.REQUEST.get('next'))
22
23 class StrippedNonEmptyCharField(forms.CharField):
24     def clean(self,value):
25         value = value.strip()
26         if self.required and value == '':
27             raise forms.ValidationError(_('this field is required'))
28         return value
29
30 class NextUrlField(forms.CharField):
31     def __init__(self):
32         super(NextUrlField,self).__init__(max_length = 255,widget = forms.HiddenInput(),required = False)
33     def clean(self,value):
34         return clean_next(value)
35
36 login_form_widget_attrs = { 'class': 'required login' }
37 username_re = re.compile(r'^[\w ]+$')
38
39 class UserNameField(StrippedNonEmptyCharField):
40     RESERVED_NAMES = (u'fuck', u'shit', u'ass', u'sex', u'add',
41                        u'edit', u'save', u'delete', u'manage', u'update', 'remove', 'new')
42     def __init__(self,db_model=User, db_field='username', must_exist=False,skip_clean=False,label=_('choose a username'),**kw):
43         self.must_exist = must_exist
44         self.skip_clean = skip_clean
45         self.db_model = db_model 
46         self.db_field = db_field
47         error_messages={'required':_('user name is required'),
48                         'taken':_('sorry, this name is taken, please choose another'),
49                         'forbidden':_('sorry, this name is not allowed, please choose another'),
50                         'missing':_('sorry, there is no user with this name'),
51                         'multiple-taken':_('sorry, we have a serious error - user name is taken by several users'),
52                         'invalid':_('user name can only consist of letters, empty space and underscore'),
53                     }
54         if 'error_messages' in kw:
55             error_messages.update(kw['error_messages'])
56             del kw['error_messages']
57         super(UserNameField,self).__init__(max_length=30,
58                 widget=forms.TextInput(attrs=login_form_widget_attrs),
59                 label=label,
60                 error_messages=error_messages,
61                 **kw
62                 )
63
64     def clean(self,username):
65         """ validate username """
66         if self.skip_clean == True:
67             return username
68         if hasattr(self, 'user_instance') and isinstance(self.user_instance, User):
69             if username == self.user_instance.username:
70                 return username
71         try:
72             username = super(UserNameField, self).clean(username)
73         except forms.ValidationError:
74             raise forms.ValidationError(self.error_messages['required'])
75         if self.required and not username_re.search(username):
76             raise forms.ValidationError(self.error_messages['invalid'])
77         if username in self.RESERVED_NAMES:
78             raise forms.ValidationError(self.error_messages['forbidden'])
79         try:
80             user = self.db_model.objects.get(
81                     **{'%s' % self.db_field : username}
82             )
83             if user:
84                 if self.must_exist:
85                     return username
86                 else:
87                     raise forms.ValidationError(self.error_messages['taken'])
88         except self.db_model.DoesNotExist:
89             if self.must_exist:
90                 raise forms.ValidationError(self.error_messages['missing'])
91             else:
92                 return username
93         except self.db_model.MultipleObjectsReturned:
94             raise forms.ValidationError(self.error_messages['multiple-taken'])
95
96 class UserEmailField(forms.EmailField):
97     def __init__(self,skip_clean=False,**kw):
98         self.skip_clean = skip_clean
99         super(UserEmailField,self).__init__(widget=forms.TextInput(attrs=dict(login_form_widget_attrs,
100             maxlength=200)), label=mark_safe(_('your email address')),
101             error_messages={'required':_('email address is required'),
102                             'invalid':_('please enter a valid email address'),
103                             'taken':_('this email is already used by someone else, please choose another'),
104                             },
105             **kw
106             )
107
108     def clean(self,email):
109         """ validate if email exist in database
110         from legacy register
111         return: raise error if it exist """
112         email = super(UserEmailField,self).clean(email.strip())
113         if self.skip_clean:
114             return email
115         if settings.EMAIL_UNIQUE == True:
116             try:
117                 user = User.objects.get(email = email)
118                 raise forms.ValidationError(self.error_messsages['taken'])
119             except User.DoesNotExist:
120                 return email
121             except User.MultipleObjectsReturned:
122                 raise forms.ValidationError(self.error_messages['taken'])
123         else:
124             return email 
125
126 class SetPasswordForm(forms.Form):
127     password1 = forms.CharField(widget=forms.PasswordInput(attrs=login_form_widget_attrs),
128                                 label=_('choose password'),
129                                 error_messages={'required':_('password is required')},
130                                 )
131     password2 = forms.CharField(widget=forms.PasswordInput(attrs=login_form_widget_attrs),
132                                 label=mark_safe(_('retype password')),
133                                 error_messages={'required':_('please, retype your password'),
134                                                 'nomatch':_('sorry, entered passwords did not match, please try again')},
135                                 )
136
137     def __init__(self, data=None, user=None, *args, **kwargs):
138         super(SetPasswordForm, self).__init__(data, *args, **kwargs)
139
140     def clean_password2(self):
141         """
142         Validates that the two password inputs match.
143         
144         """
145         if 'password1' in self.cleaned_data:
146             if self.cleaned_data['password1'] == self.cleaned_data['password2']:
147                 self.password = self.cleaned_data['password2']
148                 self.cleaned_data['password'] = self.cleaned_data['password2']
149                 return self.cleaned_data['password2']
150             else:
151                 del self.cleaned_data['password2']
152                 raise forms.ValidationError(self.fields['password2'].error_messages['nomatch'])
153         else:
154             return self.cleaned_data['password2']
155