PageRenderTime 183ms CodeModel.GetById 100ms app.highlight 13ms RepoModel.GetById 68ms app.codeStats 0ms

/soclone/forms/__init__.py

http://soclone.googlecode.com/
Python | 115 lines | 108 code | 5 blank | 2 comment | 0 complexity | 5c48a877ac645f985b9888d234cae653 MD5 | raw file
  1import datetime
  2import random
  3
  4from django import forms
  5from django.template.defaultfilters import slugify
  6
  7from soclone.forms.fields import TagnameField
  8from soclone.forms.widgets import MarkdownTextArea
  9from soclone.models import Question
 10
 11RESERVED_TITLES = (u'answer', u'close', u'edit', u'delete', u'favourite',
 12                   u'comment', u'flag', u'vote')
 13
 14WIKI_CHECKBOX_LABEL = u'community owned wiki question'
 15
 16def clean_question_title(form):
 17    """
 18    Ensures that Question titles don't conflict with URLs used to take
 19    particular actions on a Question.
 20    """
 21    if slugify(form.cleaned_data['title']) in RESERVED_TITLES:
 22        internets = random.choice([
 23            u'We have filters on Internets which make',
 24            u"I hear there's rumors on the, uh, Internets that"])
 25        raise forms.ValidationError(
 26            u'%s this title is invalid - please choose another.' % internets)
 27    return form.cleaned_data['title']
 28
 29class RevisionForm(forms.Form):
 30    """
 31    Lists revisions of a Question or Answer for selection for use as the
 32    start point for further editing. This isn't included on the relevant
 33    edit forms because we don't care which revision was selected when the
 34    edit is being submitted.
 35    """
 36    revision = forms.ChoiceField()
 37
 38    def __init__(self, post, latest_revision, *args, **kwargs):
 39        super(RevisionForm, self).__init__(*args, **kwargs)
 40        revisions = post.revisions.all().values_list(
 41            'revision', 'author__username', 'revised_at', 'summary')
 42        if (len(revisions) > 1 and
 43            (revisions[0][2].year == revisions[len(revisions)-1][2].year ==
 44             datetime.datetime.now().year)):
 45            # All revisions occured this year, so don't show the revision year
 46            date_format = '%b %d at %H:%M'
 47        else:
 48            date_format = '%b %d %Y at %H:%M'
 49        self.fields['revision'].choices = [
 50            (r[0], u'%s - %s (%s) %s' % (r[0], r[1], r[2].strftime(date_format), r[3]))
 51            for r in revisions]
 52        self.fields['revision'].initial = latest_revision.revision
 53
 54class AskQuestionForm(forms.Form):
 55    title = forms.CharField(max_length=300)
 56    text  = forms.CharField(widget=MarkdownTextArea())
 57    tags  = TagnameField()
 58    wiki  = forms.BooleanField(required=False, label=WIKI_CHECKBOX_LABEL)
 59
 60    clean_title = clean_question_title
 61
 62class RetagQuestionForm(forms.Form):
 63    tags = TagnameField()
 64
 65    def __init__(self, question, *args, **kwargs):
 66        super(RetagQuestionForm, self).__init__(*args, **kwargs)
 67        self.fields['tags'].initial = question.tagnames
 68
 69class EditQuestionForm(forms.Form):
 70    title   = forms.CharField(max_length=300)
 71    text    = forms.CharField(widget=MarkdownTextArea())
 72    tags    = TagnameField()
 73    summary = forms.CharField(max_length=300, required=False, label=u'Edit Summary')
 74
 75    def __init__(self, question, revision, *args, **kwargs):
 76        """
 77        Sets the form up to edit the given question, with initial values for
 78        the given QuestionRevision.
 79        """
 80        super(EditQuestionForm, self).__init__(*args, **kwargs)
 81        self.fields['title'].initial = revision.title
 82        self.fields['text'].initial = revision.text
 83        self.fields['tags'].initial = revision.tagnames
 84        # Once wiki mode is enabled, it can't be disabled
 85        if not question.wiki:
 86            self.fields['wiki'] = forms.BooleanField(required=False,
 87                                                     label=WIKI_CHECKBOX_LABEL)
 88
 89    clean_title = clean_question_title
 90
 91class AddAnswerForm(forms.Form):
 92    text = forms.CharField(widget=MarkdownTextArea())
 93    wiki = forms.BooleanField(required=False, label=WIKI_CHECKBOX_LABEL)
 94
 95class EditAnswerForm(forms.Form):
 96    text    = forms.CharField(widget=MarkdownTextArea())
 97    summary = forms.CharField(max_length=300, required=False, label=u'Edit Summary')
 98
 99    def __init__(self, answer, revision, *args, **kwargs):
100        """
101        Sets the form up to edit the given Answer, with initial values for
102        the given AnswerRevision.
103        """
104        super(EditAnswerForm, self).__init__(*args, **kwargs)
105        self.fields['text'].initial = revision.text
106        # Once wiki mode is enabled, it can't be disabled
107        if not answer.wiki:
108            self.fields['wiki'] = forms.BooleanField(required=False,
109                                                     label=WIKI_CHECKBOX_LABEL)
110
111class CommentForm(forms.Form):
112    comment = forms.CharField(min_length=10, max_length=300, widget=forms.Textarea(attrs={'maxlength': 300, 'cols': 70, 'rows': 2}))
113
114class CloseQuestionForm(forms.Form):
115    reason = forms.ChoiceField(choices=Question.CLOSE_REASONS)