from datetime import date
from django import forms
from models import *
-from const import *
from django.utils.translation import ugettext as _
+from django.contrib.humanize.templatetags.humanize import apnumber
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.conf import settings
-from django.contrib.contenttypes.models import ContentType
+from forum import settings
import logging
class TitleField(forms.CharField):
self.initial = ''
def clean(self, value):
- if len(value) < 10:
- raise forms.ValidationError(_('title must be > 10 characters'))
+ 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))
return value
self.initial = ''
def clean(self, value):
- if len(value) < 10:
- raise forms.ValidationError(_('question content must be > 10 characters'))
+ if len(value) < settings.FORM_MIN_QUESTION_BODY and not settings.FORM_EMPTY_QUESTION_BODY:
+ raise forms.ValidationError(_('question content must be must be at least %s characters' % settings.FORM_MIN_QUESTION_BODY))
return value
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 one %(min)s and up to %(max)s tags can be used.') % {
+ 'min': apnumber(settings.FORM_MIN_NUMBER_OF_TAGS), 'max': apnumber(settings.FORM_MAX_NUMBER_OF_TAGS)
+ }
self.initial = ''
def clean(self, value):
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'))
+ if len(list) > settings.FORM_MAX_NUMBER_OF_TAGS or len(list) < settings.FORM_MIN_NUMBER_OF_TAGS:
+ raise forms.ValidationError(_('please use betwen %(min)s and %(max)s tags') % {
+ 'min': apnumber(settings.FORM_MIN_NUMBER_OF_TAGS), 'max': apnumber(settings.FORM_MAX_NUMBER_OF_TAGS)
+ })
tagname_re = re.compile(r'[a-z0-9]+')
for tag in list:
self.label = _('update summary:')
self.help_text = _('enter a brief summary of your revision (e.g. fixed spelling, grammar, improved style, this field is optional)')
-class ModerateUserForm(forms.ModelForm):
- is_approved = forms.BooleanField(label=_("Automatically accept user's contributions for the email updates"),
- required=False)
-
- def clean_is_approved(self):
- if 'is_approved' not in self.cleaned_data:
- self.cleaned_data['is_approved'] = False
- return self.cleaned_data['is_approved']
-
- class Meta:
- model = User
- fields = ('is_approved',)
class FeedbackForm(forms.Form):
name = forms.CharField(label=_('Your name:'), required=False)
tags = TagNamesField()
wiki = WikiField()
- openid = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 40, 'class':'openid-input'}))
- user = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
- email = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
class AnswerForm(forms.Form):
text = EditorField()
wiki = WikiField()
- openid = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 40, 'class':'openid-input'}))
- user = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
- email = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
- email_notify = EmailNotifyField()
- def __init__(self, question, user, *args, **kwargs):
+
+ def __init__(self, question, *args, **kwargs):
super(AnswerForm, self).__init__(*args, **kwargs)
- self.fields['email_notify'].widget.attrs['id'] = 'question-subscribe-updates';
+
if question.wiki and settings.WIKI_ON:
self.fields['wiki'].initial = True
- if user.is_authenticated():
- if user in question.followed_by.all():
- self.fields['email_notify'].initial = True
- return
- self.fields['email_notify'].initial = False
-
-class CloseForm(forms.Form):
- reason = forms.ChoiceField(choices=CLOSE_REASONS)
class RetagQuestionForm(forms.Form):
tags = TagNamesField()
"""
revision = forms.ChoiceField(widget=forms.Select(attrs={'style' : 'width:520px'}))
- def __init__(self, post, latest_revision, *args, **kwargs):
+ def __init__(self, post, *args, **kwargs):
super(RevisionForm, self).__init__(*args, **kwargs)
+
revisions = post.revisions.all().values_list(
'revision', 'author__username', 'revised_at', 'summary')
date_format = '%c'
self.fields['revision'].choices = [
(r[0], u'%s - %s (%s) %s' % (r[0], r[1], r[2].strftime(date_format), r[3]))
for r in revisions]
- self.fields['revision'].initial = latest_revision.revision
+
+ self.fields['revision'].initial = post.active_revision.revision
class EditQuestionForm(forms.Form):
title = TitleField()
tags = TagNamesField()
summary = SummaryField()
- def __init__(self, question, revision, *args, **kwargs):
+ def __init__(self, question, revision=None, *args, **kwargs):
super(EditQuestionForm, self).__init__(*args, **kwargs)
+
+ if revision is None:
+ revision = question.active_revision
+
self.fields['title'].initial = revision.title
- self.fields['text'].initial = revision.text
+ 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()
text = EditorField()
summary = SummaryField()
- def __init__(self, answer, revision, *args, **kwargs):
+ def __init__(self, answer, revision=None, *args, **kwargs):
super(EditAnswerForm, self).__init__(*args, **kwargs)
- self.fields['text'].initial = revision.text
+
+ if revision is None:
+ revision = answer.active_revision
+
+ self.fields['text'].initial = revision.body
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}))
def __init__(self, user, *args, **kwargs):
super(EditUserForm, self).__init__(*args, **kwargs)
- logging.debug('initializing the form')
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
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)