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'})
'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)
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):
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()
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):
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:
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:
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}))
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: