PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/forms.py

https://github.com/botum/sabelo
Python | 194 lines | 165 code | 21 blank | 8 comment | 15 complexity | ee51d8c7a2772e029bea892caf5b8fa9 MD5 | raw file
  1. # encoding:utf-8
  2. import re
  3. from datetime import date
  4. from django import forms
  5. from models import *
  6. from const import *
  7. from django.utils.translation import ugettext as _
  8. class TitleField(forms.CharField):
  9. def __init__(self, *args, **kwargs):
  10. super(TitleField, self).__init__(*args, **kwargs)
  11. self.required = True
  12. self.widget = forms.TextInput(attrs={'size' : 70, 'autocomplete' : 'off'})
  13. self.max_length = 255
  14. self.label = _('title')
  15. self.help_text = _('please enter a descriptive title for your question')
  16. self.initial = ''
  17. def clean(self, value):
  18. if len(value) < 10:
  19. raise forms.ValidationError(_('title must be > 10 characters'))
  20. return value
  21. class EditorField(forms.CharField):
  22. def __init__(self, *args, **kwargs):
  23. super(EditorField, self).__init__(*args, **kwargs)
  24. self.required = True
  25. self.widget = forms.Textarea(attrs={'id':'editor'})
  26. self.label = _('content')
  27. self.help_text = u''
  28. self.initial = ''
  29. def clean(self, value):
  30. if len(value) < 10:
  31. raise forms.ValidationError(_('question content must be > 10 characters'))
  32. return value
  33. class TagNamesField(forms.CharField):
  34. def __init__(self, *args, **kwargs):
  35. super(TagNamesField, self).__init__(*args, **kwargs)
  36. self.required = True
  37. self.widget = forms.TextInput(attrs={'size' : 50, 'autocomplete' : 'off'})
  38. self.max_length = 255
  39. self.label = _('tags')
  40. self.help_text = _('please use space to separate tags (this enables autocomplete feature)')
  41. self.initial = ''
  42. def clean(self, value):
  43. value = super(TagNamesField, self).clean(value)
  44. data = value.strip()
  45. if len(data) < 1:
  46. raise forms.ValidationError(_('tags are required'))
  47. list = data.split(' ')
  48. list_temp = []
  49. if len(list) > 5:
  50. raise forms.ValidationError(_('please use 5 tags or less'))
  51. for tag in list:
  52. if len(tag) > 20:
  53. raise forms.ValidationError(_('tags must be shorter than 20 characters'))
  54. #take tag regex from settings
  55. tagname_re = re.compile(r'[a-z0-9]+')
  56. if not tagname_re.match(tag):
  57. raise forms.ValidationError(_('please use following characters in tags: letters \'a-z\', numbers, and characters \'.-_#\''))
  58. # only keep one same tag
  59. if tag not in list_temp and len(tag.strip()) > 0:
  60. list_temp.append(tag)
  61. return u' '.join(list_temp)
  62. class WikiField(forms.BooleanField):
  63. def __init__(self, *args, **kwargs):
  64. super(WikiField, self).__init__(*args, **kwargs)
  65. self.required = False
  66. self.label = _('community wiki')
  67. 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')
  68. class SummaryField(forms.CharField):
  69. def __init__(self, *args, **kwargs):
  70. super(SummaryField, self).__init__(*args, **kwargs)
  71. self.required = False
  72. self.widget = forms.TextInput(attrs={'size' : 50, 'autocomplete' : 'off'})
  73. self.max_length = 300
  74. self.label = _('update summary:')
  75. self.help_text = _('enter a brief summary of your revision (e.g. fixed spelling, grammar, improved style, this field is optional)')
  76. class AskForm(forms.Form):
  77. title = TitleField()
  78. text = EditorField()
  79. tags = TagNamesField()
  80. wiki = WikiField()
  81. openid = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 40, 'class':'openid-input'}))
  82. user = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  83. email = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  84. class AnswerForm(forms.Form):
  85. text = EditorField()
  86. wiki = WikiField()
  87. openid = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 40, 'class':'openid-input'}))
  88. user = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  89. email = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  90. def __init__(self, question, *args, **kwargs):
  91. super(AnswerForm, self).__init__(*args, **kwargs)
  92. if question.wiki:
  93. self.fields['wiki'].initial = True
  94. class CloseForm(forms.Form):
  95. reason = forms.ChoiceField(choices=CLOSE_REASONS)
  96. class RetagQuestionForm(forms.Form):
  97. tags = TagNamesField()
  98. # initialize the default values
  99. def __init__(self, question, *args, **kwargs):
  100. super(RetagQuestionForm, self).__init__(*args, **kwargs)
  101. self.fields['tags'].initial = question.tagnames
  102. class RevisionForm(forms.Form):
  103. """
  104. Lists revisions of a Question or Answer
  105. """
  106. revision = forms.ChoiceField(widget=forms.Select(attrs={'style' : 'width:520px'}))
  107. def __init__(self, post, latest_revision, *args, **kwargs):
  108. super(RevisionForm, self).__init__(*args, **kwargs)
  109. revisions = post.revisions.all().values_list(
  110. 'revision', 'author__username', 'revised_at', 'summary')
  111. date_format = '%c'
  112. self.fields['revision'].choices = [
  113. (r[0], u'%s - %s (%s) %s' % (r[0], r[1], r[2].strftime(date_format), r[3]))
  114. for r in revisions]
  115. self.fields['revision'].initial = latest_revision.revision
  116. class EditQuestionForm(forms.Form):
  117. title = TitleField()
  118. text = EditorField()
  119. tags = TagNamesField()
  120. summary = SummaryField()
  121. def __init__(self, question, revision, *args, **kwargs):
  122. super(EditQuestionForm, self).__init__(*args, **kwargs)
  123. self.fields['title'].initial = revision.title
  124. self.fields['text'].initial = revision.text
  125. self.fields['tags'].initial = revision.tagnames
  126. # Once wiki mode is enabled, it can't be disabled
  127. if not question.wiki:
  128. self.fields['wiki'] = WikiField()
  129. class EditAnswerForm(forms.Form):
  130. text = EditorField()
  131. summary = SummaryField()
  132. def __init__(self, answer, revision, *args, **kwargs):
  133. super(EditAnswerForm, self).__init__(*args, **kwargs)
  134. self.fields['text'].initial = revision.text
  135. class EditUserForm(forms.Form):
  136. email = forms.EmailField(label=u'Email', help_text=_('this email does not have to be linked to gravatar'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  137. realname = forms.CharField(label=_('Real name'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  138. website = forms.URLField(label=_('Website'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  139. city = forms.CharField(label=_('Location'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  140. birthday = forms.DateField(label=_('Date of birth'), help_text=_('will not be shown, used to calculate age, format: YYYY-MM-DD'), required=False, widget=forms.TextInput(attrs={'size' : 35}))
  141. about = forms.CharField(label=_('Profile'), required=False, widget=forms.Textarea(attrs={'cols' : 60}))
  142. def __init__(self, user, *args, **kwargs):
  143. super(EditUserForm, self).__init__(*args, **kwargs)
  144. self.fields['email'].initial = user.email
  145. self.fields['realname'].initial = user.real_name
  146. self.fields['website'].initial = user.website
  147. self.fields['city'].initial = user.location
  148. if user.date_of_birth is not None:
  149. self.fields['birthday'].initial = user.date_of_birth
  150. else:
  151. self.fields['birthday'].initial = '1990-01-01'
  152. self.fields['about'].initial = user.about
  153. self.user = user
  154. def clean_email(self):
  155. """For security reason one unique email in database"""
  156. if self.user.email != self.cleaned_data['email']:
  157. if 'email' in self.cleaned_data:
  158. try:
  159. user = User.objects.get(email = self.cleaned_data['email'])
  160. except User.DoesNotExist:
  161. return self.cleaned_data['email']
  162. except User.MultipleObjectsReturned:
  163. raise forms.ValidationError(_('this email has already been registered, please use another one'))
  164. raise forms.ValidationError(_('this email has already been registered, please use another one'))
  165. else:
  166. return self.cleaned_data['email']