forms.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. from django import forms
  2. from django.utils.translation import gettext_lazy as _
  3. from ...admin.forms import YesNoSwitch
  4. from ..cache import clear_settings_cache
  5. class ChangeSettingsForm(forms.Form):
  6. settings = []
  7. def save(self, settings):
  8. self.save_settings(settings)
  9. self.clear_cache()
  10. def save_settings(self, settings):
  11. for setting in self.settings:
  12. setting_obj = settings[setting]
  13. new_value = self.cleaned_data.get(setting)
  14. self.save_setting(setting_obj, new_value)
  15. def save_setting(self, setting, value):
  16. setting.value = value
  17. setting.save()
  18. def clear_cache(self):
  19. clear_settings_cache()
  20. class ChangeCaptchaSettingsForm(ChangeSettingsForm):
  21. settings = [
  22. "captcha_type",
  23. "recaptcha_site_key",
  24. "recaptcha_secret_key",
  25. "qa_question",
  26. "qa_help_text",
  27. "qa_answers",
  28. ]
  29. captcha_type = forms.ChoiceField(
  30. label=_("Enable CAPTCHA"),
  31. choices=[
  32. ("no", _("No CAPTCHA")),
  33. ("re", _("reCaptcha")),
  34. ("qa", _("Question and answer")),
  35. ],
  36. widget=forms.RadioSelect(),
  37. )
  38. recaptcha_site_key = forms.CharField(
  39. label=_("Site key"), max_length=100, required=False
  40. )
  41. recaptcha_secret_key = forms.CharField(
  42. label=_("Secret key"), max_length=100, required=False
  43. )
  44. qa_question = forms.CharField(
  45. label=_("Test question"), max_length=100, required=False
  46. )
  47. qa_help_text = forms.CharField(
  48. label=_("Question help text"), max_length=250, required=False
  49. )
  50. qa_answers = forms.CharField(
  51. label=_("Valid answers"),
  52. help_text=_("Enter each answer in new line. Answers are case-insensitive."),
  53. widget=forms.Textarea({"rows": 4}),
  54. max_length=250,
  55. required=False,
  56. )
  57. def clean(self):
  58. cleaned_data = super().clean()
  59. if cleaned_data.get("captcha_type") == "re":
  60. if not cleaned_data.get("recaptcha_site_key"):
  61. self.add_error(
  62. "recaptcha_site_key",
  63. _(
  64. "You need to enter site key if "
  65. "selected CAPTCHA type is reCaptcha."
  66. ),
  67. )
  68. if not cleaned_data.get("recaptcha_secret_key"):
  69. self.add_error(
  70. "recaptcha_secret_key",
  71. _(
  72. "You need to enter secret key if "
  73. "selected CAPTCHA type is reCaptcha."
  74. ),
  75. )
  76. if cleaned_data.get("captcha_type") == "qa":
  77. if not cleaned_data.get("qa_question"):
  78. self.add_error(
  79. "qa_question",
  80. _("You need to set question if selected CAPTCHA type is Q&A."),
  81. )
  82. if not cleaned_data.get("qa_answers"):
  83. self.add_error(
  84. "qa_answers",
  85. _(
  86. "You need to set question answers if "
  87. "selected CAPTCHA type is Q&A."
  88. ),
  89. )
  90. return cleaned_data
  91. class ChangeGeneralSettingsForm(ChangeSettingsForm):
  92. settings = [
  93. "forum_name",
  94. "forum_index_title",
  95. "forum_index_meta_description",
  96. "forum_branding_display",
  97. "forum_branding_text",
  98. "forum_footnote",
  99. "email_footer",
  100. ]
  101. forum_name = forms.CharField(label=_("Forum name"), min_length=2, max_length=255)
  102. forum_index_title = forms.CharField(
  103. label=_("Title"),
  104. help_text=_("You may set a custom title on forum index by typing it here."),
  105. max_length=255,
  106. required=False,
  107. )
  108. forum_index_meta_description = forms.CharField(
  109. label=_("Meta Description"),
  110. help_text=_("Short description of your forum for internet crawlers."),
  111. max_length=255,
  112. required=False,
  113. )
  114. forum_branding_display = YesNoSwitch(
  115. label=_("Display branding"), help_text=_("Switch branding in forum's navbar.")
  116. )
  117. forum_branding_text = forms.CharField(
  118. label=_("Branding text"),
  119. help_text=_("Optional text displayed besides brand image in navbar."),
  120. max_length=255,
  121. required=False,
  122. )
  123. forum_footnote = forms.CharField(
  124. label=_("Forum footnote"),
  125. help_text=_("Short message displayed in forum footer."),
  126. max_length=300,
  127. required=False,
  128. )
  129. email_footer = forms.CharField(
  130. label=_("E-mails footer"),
  131. help_text=_(
  132. "Optional short message included at the end of e-mails sent by forum."
  133. ),
  134. max_length=255,
  135. required=False,
  136. )
  137. class ChangeUsersSettingsForm(ChangeSettingsForm):
  138. settings = [
  139. "account_activation",
  140. "allow_custom_avatars",
  141. "avatar_upload_limit",
  142. "default_avatar",
  143. "default_gravatar_fallback",
  144. "signature_length_max",
  145. "subscribe_reply",
  146. "subscribe_start",
  147. "username_length_max",
  148. "username_length_min",
  149. ]
  150. account_activation = forms.ChoiceField(
  151. label=_("Require new accounts activation"),
  152. choices=[
  153. ("none", _("No activation required")),
  154. ("user", _("Activation token sent to user e-mail")),
  155. ("admin", _("Activation by administrator")),
  156. ("closed", _("Disable new registrations")),
  157. ],
  158. widget=forms.RadioSelect(),
  159. )
  160. username_length_min = forms.IntegerField(
  161. label=_("Minimum allowed username length"), min_value=2, max_value=20
  162. )
  163. username_length_max = forms.IntegerField(
  164. label=_("Maximum allowed username length"), min_value=2, max_value=20
  165. )
  166. allow_custom_avatars = YesNoSwitch(
  167. label=_("Allow custom avatar uploads"),
  168. help_text=_(
  169. "Turning this option off will forbid forum users from uploading custom "
  170. "avatars. Good for forums adressed at young users."
  171. ),
  172. )
  173. avatar_upload_limit = forms.IntegerField(
  174. label=_("Maximum size of uploaded avatar"),
  175. help_text=_("Enter maximum allowed file size (in KB) for avatar uploads."),
  176. min_value=0,
  177. )
  178. default_avatar = forms.ChoiceField(
  179. label=_("Default avatar"),
  180. choices=[
  181. ("dynamic", _("Individual")),
  182. ("gravatar", _("Gravatar")),
  183. ("gallery", _("Random avatar from gallery")),
  184. ],
  185. widget=forms.RadioSelect(),
  186. )
  187. default_gravatar_fallback = forms.ChoiceField(
  188. label=_("Fallback for default gravatar"),
  189. help_text=_(
  190. "Select which avatar to use when user has no gravatar associated with "
  191. "their e-mail address."
  192. ),
  193. choices=[
  194. ("dynamic", _("Individual")),
  195. ("gallery", _("Random avatar from gallery")),
  196. ],
  197. widget=forms.RadioSelect(),
  198. )
  199. signature_length_max = forms.IntegerField(
  200. label=_("Maximum allowed signature length"), min_value=10, max_value=5000
  201. )
  202. subscribe_start = forms.ChoiceField(
  203. label=_("Started threads"),
  204. choices=[
  205. ("no", _("Don't watch")),
  206. ("watch", _("Put on watched threads list")),
  207. (
  208. "watch_email",
  209. _("Put on watched threads list and e-mail user when somebody replies"),
  210. ),
  211. ],
  212. widget=forms.RadioSelect(),
  213. )
  214. subscribe_reply = forms.ChoiceField(
  215. label=_("Replied threads"),
  216. choices=[
  217. ("no", _("Don't watch")),
  218. ("watch", _("Put on watched threads list")),
  219. (
  220. "watch_email",
  221. _("Put on watched threads list and e-mail user when somebody replies"),
  222. ),
  223. ],
  224. widget=forms.RadioSelect(),
  225. )