]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/forms/general.py
Migrate to Django 1.6
[osqa.git] / forum / forms / general.py
index c754e859ea40154000d26ede38e3d81c8fa039a7..06ee23c0d9ff423fe1dd80dccb254f39ce0a28f9 100644 (file)
@@ -3,16 +3,18 @@ import re
 from django.utils.translation import ugettext as _
 from django.utils.safestring import mark_safe
 from forum import settings
 from django.utils.translation import ugettext as _
 from django.utils.safestring import mark_safe
 from forum import settings
-from django.http import str_to_unicode
 from forum.models import User
 from forum.models import User
-from forum_modules.recaptcha.formfield import ReCaptchaField
+from forum.modules import call_all_handlers
 import urllib
 import urllib
+import logging
 
 
-DEFAULT_NEXT = '/' + getattr(settings, 'FORUM_SCRIPT_ALIAS')
+DEFAULT_NEXT = getattr(settings, 'APP_BASE_URL')
 def clean_next(next):
     if next is None:
         return DEFAULT_NEXT
 def clean_next(next):
     if next is None:
         return DEFAULT_NEXT
-    next = str_to_unicode(urllib.unquote(next), 'utf-8')
+    next = urllib.unquote(next)
+    if not isinstance(next, unicode):
+        next = unicode(next, 'utf-8')
     next = next.strip()
     if next.startswith('/'):
         return next
     next = next.strip()
     if next.startswith('/'):
         return next
@@ -35,7 +37,7 @@ class NextUrlField(forms.CharField):
         return clean_next(value)
 
 login_form_widget_attrs = { 'class': 'required login' }
         return clean_next(value)
 
 login_form_widget_attrs = { 'class': 'required login' }
-username_re = re.compile(r'^[\w\s ]+$', re.UNICODE)
+username_re = re.compile(r'^[\-\w\s ]+$', re.UNICODE)
 
 class UserNameField(StrippedNonEmptyCharField):
     def __init__(self,db_model=User, db_field='username', must_exist=False,skip_clean=False,label=_('choose a username'),**kw):
 
 class UserNameField(StrippedNonEmptyCharField):
     def __init__(self,db_model=User, db_field='username', must_exist=False,skip_clean=False,label=_('choose a username'),**kw):
@@ -48,7 +50,7 @@ class UserNameField(StrippedNonEmptyCharField):
                         'forbidden':_('sorry, this name is not allowed, please choose another'),
                         'missing':_('sorry, there is no user with this name'),
                         'multiple-taken':_('sorry, we have a serious error - user name is taken by several users'),
                         'forbidden':_('sorry, this name is not allowed, please choose another'),
                         'missing':_('sorry, there is no user with this name'),
                         'multiple-taken':_('sorry, we have a serious error - user name is taken by several users'),
-                        'invalid':_('user name can only consist of letters, empty space and underscore'),
+                        'invalid':_('user name can only consist of letters, empty space, hyphens and underscore'),
                         'toshort':_('user name is to short, please use at least %d characters') % settings.MIN_USERNAME_LENGTH
                     }
         if 'error_messages' in kw:
                         'toshort':_('user name is to short, please use at least %d characters') % settings.MIN_USERNAME_LENGTH
                     }
         if 'error_messages' in kw:
@@ -125,6 +127,32 @@ class UserEmailField(forms.EmailField):
         else:
             return email 
 
         else:
             return email 
 
+class UserRealNameField(StrippedNonEmptyCharField):
+    def __init__(self, db_model=User, db_field='real_name', must_exist=True, skip_clean=False, label=_('Your real name'),**kw):
+        self.must_exist = must_exist
+        self.skip_clean = skip_clean
+        self.db_model = db_model
+        self.db_field = db_field
+        error_messages={'required':_('Real name is required')
+                    }
+        if 'error_messages' in kw:
+            error_messages.update(kw['error_messages'])
+            del kw['error_messages']
+        super(UserRealNameField,self).__init__(max_length=100,
+                widget=forms.TextInput(attrs=login_form_widget_attrs),
+                label=label,
+                error_messages=error_messages,
+                **kw
+                )
+
+    def clean(self, real_name):
+        if self.skip_clean == True:
+            return real_name
+        try:
+            return super(UserRealNameField, self).clean(real_name)
+        except forms.ValidationError:
+            raise forms.ValidationError(self.error_messages['required'])
+
 class SetPasswordForm(forms.Form):
     password1 = forms.CharField(widget=forms.PasswordInput(attrs=login_form_widget_attrs),
                                 label=_('choose password'),
 class SetPasswordForm(forms.Form):
     password1 = forms.CharField(widget=forms.PasswordInput(attrs=login_form_widget_attrs),
                                 label=_('choose password'),
@@ -156,4 +184,17 @@ class SetPasswordForm(forms.Form):
             return self.cleaned_data['password2']
 
 class SimpleCaptchaForm(forms.Form):
             return self.cleaned_data['password2']
 
 class SimpleCaptchaForm(forms.Form):
-    captcha = ReCaptchaField()
+    fields = {}
+
+    def __init__(self, *args, **kwargs):
+        super(SimpleCaptchaForm, self).__init__(*args, **kwargs)
+
+        spam_fields = call_all_handlers('create_anti_spam_field')
+        if spam_fields:
+            spam_fields = dict(spam_fields)
+            for name, field in spam_fields.items():
+                self.fields[name] = field
+
+            self._anti_spam_fields = spam_fields.keys()
+        else:
+            self._anti_spam_fields = []