]> git.openstreetmap.org Git - osqa.git/commitdiff
Added the option to limit the tags creation. It will defaults to false, and there...
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Sat, 29 May 2010 01:01:48 +0000 (01:01 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Sat, 29 May 2010 01:01:48 +0000 (01:01 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@341 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/forms.py
forum/models/user.py
forum/settings/form.py
forum/settings/minrep.py
forum/views/admin.py
forum/views/writers.py

index f2e46b8eb4ab183d2e6c2bb2c0c057c80a4ce8b5..66d78b2f75f3f916c9becc789094c20e1eba56c5 100644 (file)
@@ -61,7 +61,7 @@ class AnswerEditorField(EditorField):
 
 
 class TagNamesField(forms.CharField):
 
 
 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'})
         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 = ''
             '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)
 
     def clean(self, value):
         value = super(TagNamesField, self).clean(value)
@@ -94,6 +95,15 @@ class TagNamesField(forms.CharField):
             if tag not in list_temp and len(tag.strip()) > 0:
                 list_temp.append(tag)
 
             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):
         return u' '.join(list_temp)
 
 class WikiField(forms.BooleanField):
@@ -132,10 +142,12 @@ class FeedbackForm(forms.Form):
 class AskForm(forms.Form):
     title  = TitleField()
     text   = QuestionEditorField()
 class AskForm(forms.Form):
     title  = TitleField()
     text   = QuestionEditorField()
-    tags   = TagNamesField()
 
 
-    def __init__(self, *args, **kwargs):
-        super(AskForm, self).__init__(*args, **kwargs)
+    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()
 
         if settings.WIKI_ON:
             self.fields['wiki'] = WikiField()
 
@@ -181,7 +193,6 @@ class RevisionForm(forms.Form):
 class EditQuestionForm(forms.Form):
     title  = TitleField()
     text   = QuestionEditorField()
 class EditQuestionForm(forms.Form):
     title  = TitleField()
     text   = QuestionEditorField()
-    tags   = TagNamesField()
     summary = SummaryField()
 
     def __init__(self, question, user, revision=None, *args, **kwargs):
     summary = SummaryField()
 
     def __init__(self, question, user, revision=None, *args, **kwargs):
@@ -192,6 +203,8 @@ class EditQuestionForm(forms.Form):
 
         self.fields['title'].initial = revision.title
         self.fields['text'].initial = revision.body
 
         self.fields['title'].initial = revision.title
         self.fields['text'].initial = revision.body
+
+        self.fields['tags'] = TagNamesField(user)
         self.fields['tags'].initial = revision.tagnames
 
         if settings.WIKI_ON:
         self.fields['tags'].initial = revision.tagnames
 
         if settings.WIKI_ON:
index 31f4e19a87bf5125d3307534e6a97f5c987b7546..62eeac3aeb6fc9d6d1c3dae21b1f0b05356cdb1a 100644 (file)
@@ -61,6 +61,9 @@ class AnonymousUser(DjangoAnonymousUser):
     def can_accept_answer(self, answer):\r
         return False\r
 \r
     def can_accept_answer(self, answer):\r
         return False\r
 \r
+    def can_create_tags(self):\r
+        return False\r
+\r
     def can_edit_post(self, post):\r
         return False\r
 \r
     def can_edit_post(self, post):\r
         return False\r
 \r
@@ -217,6 +220,10 @@ class User(BaseModel, DjangoUser):
     def can_accept_answer(self, answer):\r
         return self == answer.question.author\r
 \r
     def can_accept_answer(self, answer):\r
         return self == answer.question.author\r
 \r
+    @true_if_is_super_or_staff\r
+    def can_create_tags(self):\r
+        return self.reputation >= int(settings.REP_TO_CREATE_TAGS)\r
+\r
     @true_if_is_super_or_staff\r
     def can_edit_post(self, post):\r
         return self == post.author or self.reputation >= int(settings.REP_TO_EDIT_OTHERS\r
     @true_if_is_super_or_staff\r
     def can_edit_post(self, post):\r
         return self == post.author or self.reputation >= int(settings.REP_TO_EDIT_OTHERS\r
index 0637c130ef2f5166446f84e2a71cc8959f3f1af1..ec213764d4a7a677c043393e9e264f6042d3a263 100644 (file)
@@ -9,6 +9,11 @@ label = _("Enable community wiki"),
 help_text = _("Can questions or answers be marked as community wiki."),
 required=False))
 
 help_text = _("Can questions or answers be marked as community wiki."),
 required=False))
 
+LIMIT_TAG_CREATION = Setting('LIMIT_TAG_CREATION', False, FORUM_SET, dict(
+label = _("Limit tag creation"),
+help_text = _("Limit tag creation to super users, staff or users with a minimum reputation."),
+required=False))
+
 
 """ settings for questions """
 FORM_MIN_QUESTION_TITLE = Setting('FORM_MIN_QUESTION_TITLE', 10, FORUM_SET, dict(
 
 """ settings for questions """
 FORM_MIN_QUESTION_TITLE = Setting('FORM_MIN_QUESTION_TITLE', 10, FORUM_SET, dict(
index 579215dd6194ce2a57a16ba73dfb82615f7afa1d..c403fe10100253ba5337c022d8d65fac91be898c 100644 (file)
@@ -27,6 +27,10 @@ REP_TO_UPLOAD = Setting('REP_TO_UPLOAD', 60, MIN_REP_SET, dict(
 label = _("Minimum reputation to upload"),\r
 help_text = _("The minimum reputation an user must have to be allowed to upload a file.")))\r
 \r
 label = _("Minimum reputation to upload"),\r
 help_text = _("The minimum reputation an user must have to be allowed to upload a file.")))\r
 \r
+REP_TO_CREATE_TAGS = Setting('REP_TO_CREATE_TAGS', 250, MIN_REP_SET, dict(\r
+label = _("Minimum reputation to create tags"),\r
+help_text = _("The minimum reputation an user must have to be allowed to create new tags.")))\r
+\r
 REP_TO_CLOSE_OWN = Setting('REP_TO_CLOSE_OWN', 250, MIN_REP_SET, dict(\r
 label = _("Minimum reputation to close own question"),\r
 help_text = _("The minimum reputation an user must have to be allowed to close his own question.")))\r
 REP_TO_CLOSE_OWN = Setting('REP_TO_CLOSE_OWN', 250, MIN_REP_SET, dict(\r
 label = _("Minimum reputation to close own question"),\r
 help_text = _("The minimum reputation an user must have to be allowed to close his own question.")))\r
index 54cb5ff3afbc74ad017ca4470278512ce79d433b..2b3426efac14622346f92d6474f2f2fd975dd6af 100644 (file)
@@ -191,6 +191,7 @@ def go_bootstrap(request):
     settings.REP_TO_COMMENT.set_value(0)
     settings.REP_TO_LIKE_COMMENT.set_value(0)
     settings.REP_TO_UPLOAD.set_value(0)
     settings.REP_TO_COMMENT.set_value(0)
     settings.REP_TO_LIKE_COMMENT.set_value(0)
     settings.REP_TO_UPLOAD.set_value(0)
+    settings.REP_TO_CREATE_TAGS.set_value(0)
     settings.REP_TO_CLOSE_OWN.set_value(60)
     settings.REP_TO_REOPEN_OWN.set_value(120)
     settings.REP_TO_RETAG.set_value(150)
     settings.REP_TO_CLOSE_OWN.set_value(60)
     settings.REP_TO_REOPEN_OWN.set_value(120)
     settings.REP_TO_RETAG.set_value(150)
index 99e93775015f28b84b9902ee2c441d3effd9726a..b6e13af629fe3af691889d3f71d9b96d2bceec14 100644 (file)
@@ -67,7 +67,7 @@ def upload(request):#ajax upload file to a question or answer
 @decoratable
 def ask(request):
     if request.POST and "text" in request.POST:
 @decoratable
 def ask(request):
     if request.POST and "text" in request.POST:
-        form = AskForm(request.POST)
+        form = AskForm(request.POST, user=request.user)
         if form.is_valid():
             if request.user.is_authenticated():
                 ask_action = AskAction(user=request.user, ip=request.META['REMOTE_ADDR']).save(data=form.cleaned_data)
         if form.is_valid():
             if request.user.is_authenticated():
                 ask_action = AskAction(user=request.user, ip=request.META['REMOTE_ADDR']).save(data=form.cleaned_data)
@@ -82,14 +82,12 @@ def ask(request):
                 request.session['temp_node_type'] = 'question'
                 return HttpResponseRedirect(reverse('auth_action_signin', kwargs={'action': 'newquestion'}))
     elif request.method == "POST" and "go" in request.POST:
                 request.session['temp_node_type'] = 'question'
                 return HttpResponseRedirect(reverse('auth_action_signin', kwargs={'action': 'newquestion'}))
     elif request.method == "POST" and "go" in request.POST:
-        form = AskForm({'title': request.POST['q']})
+        form = AskForm({'title': request.POST['q']}, user=request.user)
     else:
     else:
-        form = AskForm()
+        form = AskForm(user=request.user)
 
 
-    #tags = _get_tags_cache_json()
     return render_to_response('ask.html', {
         'form' : form,
     return render_to_response('ask.html', {
         'form' : form,
-        #'tags' : tags,
         'email_validation_faq_url':reverse('faq') + '#validate',
         }, context_instance=RequestContext(request))
 
         'email_validation_faq_url':reverse('faq') + '#validate',
         }, context_instance=RequestContext(request))