PageRenderTime 33ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/soclone/forms/__init__.py

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