]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/forms.py
Update to the email system
[osqa.git] / forum / forms.py
index 49bd5ffa1c7140b106f237260ce14862b335a2cc..0a5afe39f3ce590124e158af0004ae7e10ea113d 100644 (file)
@@ -61,7 +61,7 @@ class AnswerEditorField(EditorField):
 
 
 class TagNamesField(forms.CharField):
-    def __init__(self, *args, **kwargs):
+    def __init__(self, user=None, *args, **kwargs):
         super(TagNamesField, self).__init__(*args, **kwargs)
         self.required = True
         self.widget = forms.TextInput(attrs={'size' : 50, 'autocomplete' : 'off'})
@@ -72,6 +72,7 @@ class TagNamesField(forms.CharField):
             'min': settings.FORM_MIN_NUMBER_OF_TAGS, 'max': settings.FORM_MAX_NUMBER_OF_TAGS    
         }
         self.initial = ''
+        self.user = user
 
     def clean(self, value):
         value = super(TagNamesField, self).clean(value)
@@ -94,16 +95,27 @@ class TagNamesField(forms.CharField):
             if tag not in list_temp and len(tag.strip()) > 0:
                 list_temp.append(tag)
 
+        if settings.LIMIT_TAG_CREATION and not self.user.can_create_tags():
+            existent = Tag.objects.filter(name__in=list_temp).values_list('name', flat=True)
+
+            if len(existent) < len(list_temp):
+                unexistent = [n for n in list_temp if not n in existent]
+                raise forms.ValidationError(_("You don't have enough reputation to create new tags. The following tags do not exist yet: %s") %
+                        ', '.join(unexistent))
+
+
         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')
+        if disabled:
+            self.widget=forms.CheckboxInput(attrs={'disabled': "disabled"})
     def clean(self,value):
-        return value and settings.WIKI_ON
+        return value
 
 class EmailNotifyField(forms.BooleanField):
     def __init__(self, *args, **kwargs):
@@ -130,9 +142,14 @@ class FeedbackForm(forms.Form):
 class AskForm(forms.Form):
     title  = TitleField()
     text   = QuestionEditorField()
-    tags   = TagNamesField()
-    wiki = WikiField()
 
+    def __init__(self, data=None, user=None, *args, **kwargs):
+        super(AskForm, self).__init__(data, *args, **kwargs)
+
+        self.fields['tags']   = TagNamesField(user)
+
+        if settings.WIKI_ON:
+            self.fields['wiki'] = WikiField()
 
 class AnswerForm(forms.Form):
     text   = AnswerEditorField()
@@ -141,8 +158,11 @@ class AnswerForm(forms.Form):
     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 RetagQuestionForm(forms.Form):
@@ -173,10 +193,9 @@ class RevisionForm(forms.Form):
 class EditQuestionForm(forms.Form):
     title  = TitleField()
     text   = QuestionEditorField()
-    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:
@@ -184,17 +203,18 @@ class EditQuestionForm(forms.Form):
 
         self.fields['title'].initial = revision.title
         self.fields['text'].initial = revision.body
+
+        self.fields['tags'] = TagNamesField(user)
         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):
     text = AnswerEditorField()
     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:
@@ -202,6 +222,9 @@ class EditAnswerForm(forms.Form):
 
         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}))
     realname = forms.CharField(label=_('Real name'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
@@ -229,9 +252,7 @@ class EditUserForm(forms.Form):
         self.user = user
 
     def clean_email(self):
-        """For security reason one unique email in database"""
         if self.user.email != self.cleaned_data['email']:
-            #todo dry it, there is a similar thing in openidauth
             if settings.EMAIL_UNIQUE == True:
                 if 'email' in self.cleaned_data:
                     try: