users.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. from django import forms
  2. from django.utils.translation import gettext_lazy as _
  3. from ....admin.forms import YesNoSwitch
  4. from ....core.validators import validate_image_square
  5. from ....users.validators import validate_username_content
  6. from ... import settings
  7. from .base import ChangeSettingsForm
  8. class ChangeUsersSettingsForm(ChangeSettingsForm):
  9. settings = [
  10. "account_activation",
  11. "allow_custom_avatars",
  12. "avatar_upload_limit",
  13. "default_avatar",
  14. "default_gravatar_fallback",
  15. "blank_avatar",
  16. "signature_length_max",
  17. "subscribe_reply",
  18. "subscribe_start",
  19. "username_length_max",
  20. "username_length_min",
  21. "anonymous_username",
  22. "users_per_page",
  23. "users_per_page_orphans",
  24. "top_posters_ranking_length",
  25. "top_posters_ranking_size",
  26. "allow_data_downloads",
  27. "data_downloads_expiration",
  28. "allow_delete_own_account",
  29. "new_inactive_accounts_delete",
  30. "ip_storage_time",
  31. ]
  32. account_activation = forms.ChoiceField(
  33. label=_("Require new accounts activation"),
  34. choices=[
  35. ("none", _("No activation required")),
  36. ("user", _("Activation token sent to user e-mail")),
  37. ("admin", _("Activation by administrator")),
  38. ("closed", _("Disable new registrations")),
  39. ],
  40. widget=forms.RadioSelect(),
  41. )
  42. new_inactive_accounts_delete = forms.IntegerField(
  43. label=_(
  44. "Delete new inactive accounts if they weren't activated "
  45. "within this number of days"
  46. ),
  47. help_text=_("Enter 0 to never delete inactive new accounts."),
  48. min_value=0,
  49. )
  50. username_length_min = forms.IntegerField(
  51. label=_("Minimum allowed username length"), min_value=2, max_value=20
  52. )
  53. username_length_max = forms.IntegerField(
  54. label=_("Maximum allowed username length"), min_value=2, max_value=20
  55. )
  56. allow_custom_avatars = YesNoSwitch(
  57. label=_("Allow custom avatar uploads"),
  58. help_text=_(
  59. "Turning this option off will forbid forum users from uploading custom "
  60. "avatars. Good for forums adressed at young users."
  61. ),
  62. )
  63. avatar_upload_limit = forms.IntegerField(
  64. label=_("Maximum size of uploaded avatar"),
  65. help_text=_("Enter maximum allowed file size (in KB) for avatar uploads."),
  66. min_value=0,
  67. )
  68. default_avatar = forms.ChoiceField(
  69. label=_("Default avatar"),
  70. choices=[
  71. ("dynamic", _("Individual")),
  72. ("gravatar", _("Gravatar")),
  73. ("gallery", _("Random avatar from gallery")),
  74. ],
  75. widget=forms.RadioSelect(),
  76. )
  77. default_gravatar_fallback = forms.ChoiceField(
  78. label=_("Fallback for default gravatar"),
  79. help_text=_(
  80. "Select which avatar to use when user has no gravatar associated with "
  81. "their e-mail address."
  82. ),
  83. choices=[
  84. ("dynamic", _("Individual")),
  85. ("gallery", _("Random avatar from gallery")),
  86. ],
  87. widget=forms.RadioSelect(),
  88. )
  89. blank_avatar = forms.ImageField(
  90. label=_("Blank avatar"),
  91. help_text=_(
  92. "Blank avatar is displayed in the interface when user's avatar is not "
  93. "available: when user was deleted or is guest. Uploaded image should be "
  94. "a square."
  95. ),
  96. required=False,
  97. )
  98. blank_avatar_delete = forms.BooleanField(
  99. label=_("Delete custom blank avatar"), required=False
  100. )
  101. signature_length_max = forms.IntegerField(
  102. label=_("Maximum allowed signature length"), min_value=10, max_value=5000
  103. )
  104. subscribe_start = forms.ChoiceField(
  105. label=_("Started threads"),
  106. choices=[
  107. ("no", _("Don't watch")),
  108. ("watch", _("Put on watched threads list")),
  109. (
  110. "watch_email",
  111. _("Put on watched threads list and e-mail user when somebody replies"),
  112. ),
  113. ],
  114. widget=forms.RadioSelect(),
  115. )
  116. subscribe_reply = forms.ChoiceField(
  117. label=_("Replied threads"),
  118. choices=[
  119. ("no", _("Don't watch")),
  120. ("watch", _("Put on watched threads list")),
  121. (
  122. "watch_email",
  123. _("Put on watched threads list and e-mail user when somebody replies"),
  124. ),
  125. ],
  126. widget=forms.RadioSelect(),
  127. )
  128. users_per_page = forms.IntegerField(
  129. label=_("Number of users displayed on a single page"), min_value=4
  130. )
  131. users_per_page_orphans = forms.IntegerField(
  132. label=_("Maximum orphans"),
  133. help_text=_(
  134. "If number of users to be displayed on the last page is less or equal to "
  135. "number specified in this setting, those users will instead be displayed "
  136. "on previous page, reducing the total number of pages on the list."
  137. ),
  138. min_value=0,
  139. )
  140. top_posters_ranking_length = forms.IntegerField(
  141. label=_("Maximum age in days of posts that should count to the ranking"),
  142. min_value=1,
  143. )
  144. top_posters_ranking_size = forms.IntegerField(
  145. label=_("Maximum number of ranked users"), min_value=2
  146. )
  147. allow_data_downloads = YesNoSwitch(label=_("Allow users to download their data"))
  148. data_downloads_expiration = forms.IntegerField(
  149. label=_("Maximum age in hours of data downloads before they expire"),
  150. help_text=_(
  151. "Data downloads older than specified will have their files deleted and "
  152. "will be marked as expired."
  153. ),
  154. min_value=1,
  155. )
  156. allow_delete_own_account = YesNoSwitch(
  157. label=_("Allow users to delete their own accounts")
  158. )
  159. ip_storage_time = forms.IntegerField(
  160. label=_("IP storage time"),
  161. help_text=_(
  162. "Number of days for which users IP addresses are stored in forum database. "
  163. "Enter zero to store registered IP addresses forever. Deleting user "
  164. "account always deletes the IP addresses associated with it."
  165. ),
  166. min_value=0,
  167. )
  168. anonymous_username = forms.CharField(
  169. label=_("Anonymous username"),
  170. help_text=_(
  171. "This username is displayed instead of delete user's actual name "
  172. "next to their content."
  173. ),
  174. min_length=1,
  175. max_length=15,
  176. validators=[validate_username_content],
  177. )
  178. def clean_blank_avatar(self):
  179. upload = self.cleaned_data.get("blank_avatar")
  180. if not upload or upload == self.initial.get("blank_avatar"):
  181. return None
  182. validate_image_square(upload.image)
  183. min_size = max(settings.MISAGO_AVATARS_SIZES)
  184. if upload.image.width < min_size:
  185. raise forms.ValidationError(
  186. _("Uploaded image's edge should be at least %(size)s pixels long.")
  187. % {"size": min_size}
  188. )
  189. return upload
  190. def clean(self):
  191. cleaned_data = super().clean()
  192. if cleaned_data.get("users_per_page_orphans") > cleaned_data.get(
  193. "users_per_page"
  194. ):
  195. self.add_error(
  196. "users_per_page_orphans",
  197. _("This value must be lower than number of users per page."),
  198. )
  199. return cleaned_data