users.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from django import forms
  2. from django.utils.translation import gettext_lazy as _
  3. from ....admin.forms import YesNoSwitch
  4. from ... import settings
  5. from .base import ChangeSettingsForm
  6. class ChangeUsersSettingsForm(ChangeSettingsForm):
  7. settings = [
  8. "account_activation",
  9. "allow_custom_avatars",
  10. "avatar_upload_limit",
  11. "default_avatar",
  12. "default_gravatar_fallback",
  13. "blank_avatar",
  14. "signature_length_max",
  15. "subscribe_reply",
  16. "subscribe_start",
  17. "username_length_max",
  18. "username_length_min",
  19. ]
  20. account_activation = forms.ChoiceField(
  21. label=_("Require new accounts activation"),
  22. choices=[
  23. ("none", _("No activation required")),
  24. ("user", _("Activation token sent to user e-mail")),
  25. ("admin", _("Activation by administrator")),
  26. ("closed", _("Disable new registrations")),
  27. ],
  28. widget=forms.RadioSelect(),
  29. )
  30. username_length_min = forms.IntegerField(
  31. label=_("Minimum allowed username length"), min_value=2, max_value=20
  32. )
  33. username_length_max = forms.IntegerField(
  34. label=_("Maximum allowed username length"), min_value=2, max_value=20
  35. )
  36. allow_custom_avatars = YesNoSwitch(
  37. label=_("Allow custom avatar uploads"),
  38. help_text=_(
  39. "Turning this option off will forbid forum users from uploading custom "
  40. "avatars. Good for forums adressed at young users."
  41. ),
  42. )
  43. avatar_upload_limit = forms.IntegerField(
  44. label=_("Maximum size of uploaded avatar"),
  45. help_text=_("Enter maximum allowed file size (in KB) for avatar uploads."),
  46. min_value=0,
  47. )
  48. default_avatar = forms.ChoiceField(
  49. label=_("Default avatar"),
  50. choices=[
  51. ("dynamic", _("Individual")),
  52. ("gravatar", _("Gravatar")),
  53. ("gallery", _("Random avatar from gallery")),
  54. ],
  55. widget=forms.RadioSelect(),
  56. )
  57. default_gravatar_fallback = forms.ChoiceField(
  58. label=_("Fallback for default gravatar"),
  59. help_text=_(
  60. "Select which avatar to use when user has no gravatar associated with "
  61. "their e-mail address."
  62. ),
  63. choices=[
  64. ("dynamic", _("Individual")),
  65. ("gallery", _("Random avatar from gallery")),
  66. ],
  67. widget=forms.RadioSelect(),
  68. )
  69. blank_avatar = forms.ImageField(
  70. label=_("Blank avatar"),
  71. help_text=_(
  72. "Blank avatar is displayed in the interface when user's avatar is not "
  73. "available: when user was deleted or is guest. Uploaded image should be "
  74. "a square"
  75. ),
  76. required=False,
  77. )
  78. blank_avatar_delete = forms.BooleanField(
  79. label=_("Delete custom blank avatar"), required=False
  80. )
  81. signature_length_max = forms.IntegerField(
  82. label=_("Maximum allowed signature length"), min_value=10, max_value=5000
  83. )
  84. subscribe_start = forms.ChoiceField(
  85. label=_("Started threads"),
  86. choices=[
  87. ("no", _("Don't watch")),
  88. ("watch", _("Put on watched threads list")),
  89. (
  90. "watch_email",
  91. _("Put on watched threads list and e-mail user when somebody replies"),
  92. ),
  93. ],
  94. widget=forms.RadioSelect(),
  95. )
  96. subscribe_reply = forms.ChoiceField(
  97. label=_("Replied threads"),
  98. choices=[
  99. ("no", _("Don't watch")),
  100. ("watch", _("Put on watched threads list")),
  101. (
  102. "watch_email",
  103. _("Put on watched threads list and e-mail user when somebody replies"),
  104. ),
  105. ],
  106. widget=forms.RadioSelect(),
  107. )
  108. def clean_blank_avatar(self):
  109. upload = self.cleaned_data.get("blank_avatar")
  110. if not upload or upload == self.initial.get("blank_avatar"):
  111. return None
  112. if upload.image.width != upload.image.height:
  113. raise forms.ValidationError(_("Submitted image is not a square."))
  114. min_size = max(settings.MISAGO_AVATARS_SIZES)
  115. if upload.image.width < min_size:
  116. raise forms.ValidationError(
  117. _("Submitted image's edge should be at least %(size)s pixels long.")
  118. % {"size": min_size}
  119. )
  120. return upload