]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/forms.py
adding an example test for the user module. the fixture portion of this maybe not...
[osqa.git] / forum / forms.py
index 70466b57c2478f2d6771bbb6172bc806855f028b..f2e46b8eb4ab183d2e6c2bb2c0c057c80a4ce8b5 100644 (file)
@@ -2,14 +2,13 @@ import re
 from datetime import date
 from django import forms
 from models import *
 from datetime import date
 from django import forms
 from models import *
-from const import *
 from django.utils.translation import ugettext as _
 from django.utils.translation import ugettext as _
+from django.contrib.humanize.templatetags.humanize import apnumber
 from forum.models import User
 from forum.models import User
-from django.contrib.contenttypes.models import ContentType
+
 from django.utils.safestring import mark_safe
 from forum.utils.forms import NextUrlField, UserNameField, SetPasswordForm
 from django.utils.safestring import mark_safe
 from forum.utils.forms import NextUrlField, UserNameField, SetPasswordForm
-from django.conf import settings
-from django.contrib.contenttypes.models import ContentType
+from forum import settings
 import logging
 
 class TitleField(forms.CharField):
 import logging
 
 class TitleField(forms.CharField):
@@ -24,25 +23,43 @@ class TitleField(forms.CharField):
 
     def clean(self, value):
         if len(value) < settings.FORM_MIN_QUESTION_TITLE:
 
     def clean(self, value):
         if len(value) < settings.FORM_MIN_QUESTION_TITLE:
-            raise forms.ValidationError(_('title must be must be at least %s characters' % settings.FORM_MIN_QUESTION_TITLE))
+            raise forms.ValidationError(_('title must be must be at least %s characters') % settings.FORM_MIN_QUESTION_TITLE)
 
         return value
 
 class EditorField(forms.CharField):
     def __init__(self, *args, **kwargs):
         super(EditorField, self).__init__(*args, **kwargs)
 
         return value
 
 class EditorField(forms.CharField):
     def __init__(self, *args, **kwargs):
         super(EditorField, self).__init__(*args, **kwargs)
-        self.required = True
         self.widget = forms.Textarea(attrs={'id':'editor'})
         self.label  = _('content')
         self.help_text = u''
         self.initial = ''
 
         self.widget = forms.Textarea(attrs={'id':'editor'})
         self.label  = _('content')
         self.help_text = u''
         self.initial = ''
 
+
+class QuestionEditorField(EditorField):
+    def __init__(self, *args, **kwargs):
+        super(QuestionEditorField, self).__init__(*args, **kwargs)
+        self.required = not bool(settings.FORM_EMPTY_QUESTION_BODY)
+
+
+    def clean(self, value):
+        if self.required and (len(value) < settings.FORM_MIN_QUESTION_BODY):
+            raise forms.ValidationError(_('question content must be at least %s characters') % settings.FORM_MIN_QUESTION_BODY)
+
+        return value
+
+class AnswerEditorField(EditorField):
+    def __init__(self, *args, **kwargs):
+        super(AnswerEditorField, self).__init__(*args, **kwargs)
+        self.required = True
+
     def clean(self, value):
     def clean(self, value):
-        if len(value) < settings.FORM_MIN_QUESTION_BODY and not settings.FORM_EMPTEY_QUESTION_BODY:
-            raise forms.ValidationError(_('question content must be must be at least %s characters' % settings.FORM_MIN_QUESTION_BODY))
+        if len(value) < settings.FORM_MIN_QUESTION_BODY:
+            raise forms.ValidationError(_('answer content must be at least %s characters') % settings.FORM_MIN_QUESTION_BODY)
 
         return value
 
 
         return value
 
+
 class TagNamesField(forms.CharField):
     def __init__(self, *args, **kwargs):
         super(TagNamesField, self).__init__(*args, **kwargs)
 class TagNamesField(forms.CharField):
     def __init__(self, *args, **kwargs):
         super(TagNamesField, self).__init__(*args, **kwargs)
@@ -51,40 +68,44 @@ class TagNamesField(forms.CharField):
         self.max_length = 255
         self.label  = _('tags')
         #self.help_text = _('please use space to separate tags (this enables autocomplete feature)')
         self.max_length = 255
         self.label  = _('tags')
         #self.help_text = _('please use space to separate tags (this enables autocomplete feature)')
-        self.help_text = _('Tags are short keywords, with no spaces within. Up to five tags can be used.')
+        self.help_text = _('Tags are short keywords, with no spaces within. At least %(min)s and up to %(max)s tags can be used.') % {
+            'min': settings.FORM_MIN_NUMBER_OF_TAGS, 'max': settings.FORM_MAX_NUMBER_OF_TAGS    
+        }
         self.initial = ''
 
     def clean(self, value):
         value = super(TagNamesField, self).clean(value)
         self.initial = ''
 
     def clean(self, value):
         value = super(TagNamesField, self).clean(value)
-        data = value.strip()
-        if len(data) < 1:
-            raise forms.ValidationError(_('tags are required'))
+        data = value.strip().lower()
 
         split_re = re.compile(r'[ ,]+')
         list = split_re.split(data)
 
         split_re = re.compile(r'[ ,]+')
         list = split_re.split(data)
-        list_temp = []
-        if len(list) > 5:
-            raise forms.ValidationError(_('please use 5 tags or less'))
 
 
-        tagname_re = re.compile(r'[a-z0-9]+')
+        if len(list) > settings.FORM_MAX_NUMBER_OF_TAGS or len(list) < settings.FORM_MIN_NUMBER_OF_TAGS:
+            raise forms.ValidationError(_('please use between %(min)s and %(max)s tags') % { 'min': settings.FORM_MIN_NUMBER_OF_TAGS, 'max': settings.FORM_MAX_NUMBER_OF_TAGS})
+
+        list_temp = []
+        tagname_re = re.compile(r'^[\w+\.-]+$', re.UNICODE)
         for tag in list:
         for tag in list:
-            if len(tag) > 20:
-                raise forms.ValidationError(_('tags must be shorter than 20 characters'))
+            if len(tag) > settings.FORM_MAX_LENGTH_OF_TAG or len(tag) < settings.FORM_MIN_LENGTH_OF_TAG:
+                raise forms.ValidationError(_('please use between %(min)s and %(max)s characters in you tags') % { 'min': settings.FORM_MIN_LENGTH_OF_TAG, 'max': settings.FORM_MAX_LENGTH_OF_TAG})
             if not tagname_re.match(tag):
             if not tagname_re.match(tag):
-                raise forms.ValidationError(_('please use following characters in tags: letters \'a-z\', numbers, and characters \'.-_#\''))
+                raise forms.ValidationError(_('please use following characters in tags: letters , numbers, and characters \'.-_\''))
             # only keep one same tag
             if tag not in list_temp and len(tag.strip()) > 0:
                 list_temp.append(tag)
             # only keep one same tag
             if tag not in list_temp and len(tag.strip()) > 0:
                 list_temp.append(tag)
+
         return u' '.join(list_temp)
 
 class WikiField(forms.BooleanField):
         return u' '.join(list_temp)
 
 class WikiField(forms.BooleanField):
-    def __init__(self, *args, **kwargs):
+    def __init__(self, disabled=False, *args, **kwargs):
         super(WikiField, self).__init__(*args, **kwargs)
         self.required = False
         self.label  = _('community wiki')
         self.help_text = _('if you choose community wiki option, the question and answer do not generate points and name of author will not be shown')
         super(WikiField, self).__init__(*args, **kwargs)
         self.required = False
         self.label  = _('community wiki')
         self.help_text = _('if you choose community wiki option, the question and answer do not generate points and name of author will not be shown')
+        if disabled:
+            self.widget=forms.CheckboxInput(attrs={'disabled': "disabled"})
     def clean(self,value):
     def clean(self,value):
-        return value and settings.WIKI_ON
+        return value
 
 class EmailNotifyField(forms.BooleanField):
     def __init__(self, *args, **kwargs):
 
 class EmailNotifyField(forms.BooleanField):
     def __init__(self, *args, **kwargs):
@@ -110,24 +131,27 @@ class FeedbackForm(forms.Form):
 
 class AskForm(forms.Form):
     title  = TitleField()
 
 class AskForm(forms.Form):
     title  = TitleField()
-    text   = EditorField()
+    text   = QuestionEditorField()
     tags   = TagNamesField()
     tags   = TagNamesField()
-    wiki = WikiField()
 
 
+    def __init__(self, *args, **kwargs):
+        super(AskForm, self).__init__(*args, **kwargs)
+        if settings.WIKI_ON:
+            self.fields['wiki'] = WikiField()
 
 class AnswerForm(forms.Form):
 
 class AnswerForm(forms.Form):
-    text   = EditorField()
+    text   = AnswerEditorField()
     wiki   = WikiField()
 
     def __init__(self, question, *args, **kwargs):
         super(AnswerForm, self).__init__(*args, **kwargs)
 
     wiki   = WikiField()
 
     def __init__(self, question, *args, **kwargs):
         super(AnswerForm, self).__init__(*args, **kwargs)
 
-        if question.wiki and settings.WIKI_ON:
-            self.fields['wiki'].initial = True
+        if settings.WIKI_ON:
+            self.fields['wiki'] = WikiField()
 
 
+            if question.nis.wiki:
+                self.fields['wiki'].initial = True
 
 
-class CloseForm(forms.Form):
-    reason = forms.ChoiceField(choices=CLOSE_REASONS)
 
 class RetagQuestionForm(forms.Form):
     tags   = TagNamesField()
 
 class RetagQuestionForm(forms.Form):
     tags   = TagNamesField()
@@ -156,11 +180,11 @@ class RevisionForm(forms.Form):
 
 class EditQuestionForm(forms.Form):
     title  = TitleField()
 
 class EditQuestionForm(forms.Form):
     title  = TitleField()
-    text   = EditorField()
+    text   = QuestionEditorField()
     tags   = TagNamesField()
     summary = SummaryField()
 
     tags   = TagNamesField()
     summary = SummaryField()
 
-    def __init__(self, question, revision=None, *args, **kwargs):
+    def __init__(self, question, user, revision=None, *args, **kwargs):
         super(EditQuestionForm, self).__init__(*args, **kwargs)
 
         if revision is None:
         super(EditQuestionForm, self).__init__(*args, **kwargs)
 
         if revision is None:
@@ -169,16 +193,15 @@ class EditQuestionForm(forms.Form):
         self.fields['title'].initial = revision.title
         self.fields['text'].initial = revision.body
         self.fields['tags'].initial = revision.tagnames
         self.fields['title'].initial = revision.title
         self.fields['text'].initial = revision.body
         self.fields['tags'].initial = revision.tagnames
-            
-        # Once wiki mode is enabled, it can't be disabled
-        if not question.wiki:
-            self.fields['wiki'] = WikiField()
+
+        if settings.WIKI_ON:
+            self.fields['wiki'] = WikiField(disabled=(question.nis.wiki and not user.can_cancel_wiki(question)), initial=question.nis.wiki)
 
 class EditAnswerForm(forms.Form):
 
 class EditAnswerForm(forms.Form):
-    text = EditorField()
+    text = AnswerEditorField()
     summary = SummaryField()
 
     summary = SummaryField()
 
-    def __init__(self, answer, revision=None, *args, **kwargs):
+    def __init__(self, answer, user, revision=None, *args, **kwargs):
         super(EditAnswerForm, self).__init__(*args, **kwargs)
 
         if revision is None:
         super(EditAnswerForm, self).__init__(*args, **kwargs)
 
         if revision is None:
@@ -186,10 +209,11 @@ class EditAnswerForm(forms.Form):
 
         self.fields['text'].initial = revision.body
 
 
         self.fields['text'].initial = revision.body
 
+        if settings.WIKI_ON:
+            self.fields['wiki'] = WikiField(disabled=(answer.nis.wiki and not user.can_cancel_wiki(answer)), initial=answer.nis.wiki)
+
 class EditUserForm(forms.Form):
     email = forms.EmailField(label=u'Email', help_text=_('this email does not have to be linked to gravatar'), required=True, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
 class EditUserForm(forms.Form):
     email = forms.EmailField(label=u'Email', help_text=_('this email does not have to be linked to gravatar'), required=True, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
-    if settings.EDITABLE_SCREEN_NAME:
-       username = UserNameField(label=_('Screen name'))
     realname = forms.CharField(label=_('Real name'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
     website = forms.URLField(label=_('Website'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
     city = forms.CharField(label=_('Location'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
     realname = forms.CharField(label=_('Real name'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
     website = forms.URLField(label=_('Website'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
     city = forms.CharField(label=_('Location'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
@@ -198,8 +222,8 @@ class EditUserForm(forms.Form):
 
     def __init__(self, user, *args, **kwargs):
         super(EditUserForm, self).__init__(*args, **kwargs)
 
     def __init__(self, user, *args, **kwargs):
         super(EditUserForm, self).__init__(*args, **kwargs)
-        logging.debug('initializing the form')
         if settings.EDITABLE_SCREEN_NAME:
         if settings.EDITABLE_SCREEN_NAME:
+            self.fields['username'] = UserNameField(label=_('Screen name'))
             self.fields['username'].initial = user.username
             self.fields['username'].user_instance = user
         self.fields['email'].initial = user.email
             self.fields['username'].initial = user.username
             self.fields['username'].user_instance = user
         self.fields['email'].initial = user.email
@@ -229,6 +253,12 @@ class EditUserForm(forms.Form):
                     raise forms.ValidationError(_('this email has already been registered, please use another one'))
         return self.cleaned_data['email']
 
                     raise forms.ValidationError(_('this email has already been registered, please use another one'))
         return self.cleaned_data['email']
 
+NOTIFICATION_CHOICES = (
+    ('i', _('Instantly')),
+    ('d', _('Daily')),
+    ('w', _('Weekly')),
+    ('n', _('No notifications')),
+)
 
 class SubscriptionSettingsForm(forms.Form):
     member_joins = forms.ChoiceField(widget=forms.RadioSelect, choices=NOTIFICATION_CHOICES)
 
 class SubscriptionSettingsForm(forms.Form):
     member_joins = forms.ChoiceField(widget=forms.RadioSelect, choices=NOTIFICATION_CHOICES)