From: hernani Date: Sat, 29 May 2010 01:01:48 +0000 (+0000) Subject: Added the option to limit the tags creation. It will defaults to false, and there... X-Git-Tag: live~791 X-Git-Url: https://git.openstreetmap.org./osqa.git/commitdiff_plain/fe388980b431aef835eaf43f4547e51f763c3f4b?ds=sidebyside Added the option to limit the tags creation. It will defaults to false, and there is no interface for an admin to load a bunch of tags yet. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@341 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- diff --git a/forum/forms.py b/forum/forms.py index f2e46b8..66d78b2 100644 --- a/forum/forms.py +++ b/forum/forms.py @@ -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,6 +95,15 @@ 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): @@ -132,10 +142,12 @@ class FeedbackForm(forms.Form): 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() @@ -181,7 +193,6 @@ class RevisionForm(forms.Form): class EditQuestionForm(forms.Form): title = TitleField() text = QuestionEditorField() - tags = TagNamesField() 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['tags'] = TagNamesField(user) self.fields['tags'].initial = revision.tagnames if settings.WIKI_ON: diff --git a/forum/models/user.py b/forum/models/user.py index 31f4e19..62eeac3 100644 --- a/forum/models/user.py +++ b/forum/models/user.py @@ -61,6 +61,9 @@ class AnonymousUser(DjangoAnonymousUser): def can_accept_answer(self, answer): return False + def can_create_tags(self): + return False + def can_edit_post(self, post): return False @@ -217,6 +220,10 @@ class User(BaseModel, DjangoUser): def can_accept_answer(self, answer): return self == answer.question.author + @true_if_is_super_or_staff + def can_create_tags(self): + return self.reputation >= int(settings.REP_TO_CREATE_TAGS) + @true_if_is_super_or_staff def can_edit_post(self, post): return self == post.author or self.reputation >= int(settings.REP_TO_EDIT_OTHERS diff --git a/forum/settings/form.py b/forum/settings/form.py index 0637c13..ec21376 100644 --- a/forum/settings/form.py +++ b/forum/settings/form.py @@ -9,6 +9,11 @@ label = _("Enable community wiki"), 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( diff --git a/forum/settings/minrep.py b/forum/settings/minrep.py index 579215d..c403fe1 100644 --- a/forum/settings/minrep.py +++ b/forum/settings/minrep.py @@ -27,6 +27,10 @@ REP_TO_UPLOAD = Setting('REP_TO_UPLOAD', 60, MIN_REP_SET, dict( label = _("Minimum reputation to upload"), help_text = _("The minimum reputation an user must have to be allowed to upload a file."))) +REP_TO_CREATE_TAGS = Setting('REP_TO_CREATE_TAGS', 250, MIN_REP_SET, dict( +label = _("Minimum reputation to create tags"), +help_text = _("The minimum reputation an user must have to be allowed to create new tags."))) + REP_TO_CLOSE_OWN = Setting('REP_TO_CLOSE_OWN', 250, MIN_REP_SET, dict( label = _("Minimum reputation to close own question"), help_text = _("The minimum reputation an user must have to be allowed to close his own question."))) diff --git a/forum/views/admin.py b/forum/views/admin.py index 54cb5ff..2b3426e 100644 --- a/forum/views/admin.py +++ b/forum/views/admin.py @@ -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_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) diff --git a/forum/views/writers.py b/forum/views/writers.py index 99e9377..b6e13af 100644 --- a/forum/views/writers.py +++ b/forum/views/writers.py @@ -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: - 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) @@ -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: - form = AskForm({'title': request.POST['q']}) + form = AskForm({'title': request.POST['q']}, user=request.user) else: - form = AskForm() + form = AskForm(user=request.user) - #tags = _get_tags_cache_json() return render_to_response('ask.html', { 'form' : form, - #'tags' : tags, 'email_validation_faq_url':reverse('faq') + '#validate', }, context_instance=RequestContext(request))