admin.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from django.contrib.auth import get_user_model
  2. from django.utils.translation import ugettext_lazy as _
  3. from misago.core import forms
  4. from misago.core.validators import validate_sluggable
  5. from misago.acl.models import Role
  6. from misago.users.models import Rank
  7. from misago.users.validators import (validate_username, validate_email,
  8. validate_password)
  9. class UserBaseForm(forms.ModelForm):
  10. username = forms.CharField(
  11. label=_("Username"))
  12. title = forms.CharField(
  13. label=_("Custom title"),
  14. required=False)
  15. email = forms.EmailField(
  16. label=_("E-mail address"))
  17. class Meta:
  18. model = get_user_model()
  19. fields = ['username', 'email', 'title']
  20. def clean_username(self):
  21. data = self.cleaned_data['username']
  22. validate_username(data)
  23. return data
  24. def clean_email(self):
  25. data = self.cleaned_data['email']
  26. validate_email(data)
  27. return data
  28. def clean_new_password(self):
  29. data = self.cleaned_data['new_password']
  30. validate_password(data)
  31. return data
  32. def clean_roles(self):
  33. data = self.cleaned_data['roles']
  34. for role in data:
  35. if role.special_role == 'authenticated':
  36. break
  37. else:
  38. message = _('All registered members must have "Member" role.')
  39. raise forms.ValidationError(message)
  40. return data
  41. class NewUserForm(UserBaseForm):
  42. new_password = forms.CharField(
  43. label=_("Password"),
  44. widget=forms.PasswordInput)
  45. class Meta:
  46. model = get_user_model()
  47. fields = ['username', 'email', 'title']
  48. class EditUserForm(forms.ModelForm):
  49. new_password = forms.CharField(
  50. label=_("Change password to"),
  51. widget=forms.PasswordInput,
  52. required=False)
  53. class Meta:
  54. model = get_user_model()
  55. fields = ['username', 'email', 'title']
  56. def UserFormFactory(FormType, instance):
  57. extra_fields = {}
  58. ranks = Rank.objects.order_by('name')
  59. if ranks.exists():
  60. extra_fields['rank'] = forms.ModelChoiceField(
  61. label=_("Rank"),
  62. help_text=_("Ranks are used to group and distinguish users. "
  63. "They are also used to add permissions to groups of "
  64. "users."),
  65. queryset=ranks,
  66. initial=instance.rank,
  67. required=False,
  68. empty_label=_("No rank"))
  69. roles = Role.objects.order_by('name')
  70. extra_fields['roles'] = forms.ModelMultipleChoiceField(
  71. label=_("Roles"),
  72. help_text=_("Individual roles of this user."),
  73. queryset=roles,
  74. initial=instance.roles.all() if instance.pk else None,
  75. widget=forms.CheckboxSelectMultiple)
  76. return type('UserFormFinal', (FormType,), extra_fields)
  77. def StaffFlagUserFormFactory(FormType, instance, add_staff_field):
  78. FormType = UserFormFactory(FormType, instance)
  79. if add_staff_field:
  80. staff_levels = (
  81. (0, _("No access")),
  82. (1, _("Administrator")),
  83. (2, _("Superadmin")),
  84. )
  85. staff_fields = {
  86. 'staff_level': forms.TypedChoiceField(
  87. label=_("Admin level"),
  88. help_text=_('Only administrators can access admin sites. '
  89. 'In addition to admin site access, superadmins '
  90. 'can also change other members admin levels.'),
  91. coerce=int,
  92. choices=staff_levels,
  93. initial=instance.staff_level),
  94. }
  95. return type('StaffUserForm', (FormType,), staff_fields)
  96. else:
  97. return FormType
  98. class RankForm(forms.ModelForm):
  99. name = forms.CharField(
  100. label=_("Name"),
  101. validators=[validate_sluggable()],
  102. help_text=_('Short and descriptive name of all users with this rank. '
  103. '"The Team" or "Game Masters" are good examples.'))
  104. title = forms.CharField(
  105. label=_("User title"), required=False,
  106. help_text=_('Optional, singular version of rank name displayed by '
  107. 'user names. For example "GM" or "Dev".'))
  108. description = forms.CharField(
  109. label=_("Description"), max_length=2048, required=False,
  110. widget=forms.Textarea(attrs={'rows': 3}),
  111. help_text=_("Optional description explaining function or status of "
  112. "members distincted with this rank."))
  113. roles = forms.ModelMultipleChoiceField(
  114. label=_("User roles"), queryset=Role.objects.order_by('name'),
  115. required=False, widget=forms.CheckboxSelectMultiple,
  116. help_text=_('Rank can give users with it additional roles.'))
  117. css_class = forms.CharField(
  118. label=_("CSS class"), required=False,
  119. help_text=_("Optional css class added to content belonging to this "
  120. "rank owner."))
  121. is_tab = forms.BooleanField(
  122. label=_("Give rank dedicated tab on users list"), required=False,
  123. help_text=_("Selecting this option will make users with this rank "
  124. "easily discoverable by others trough dedicated page on "
  125. "forum users list."))
  126. is_on_index = forms.BooleanField(
  127. label=_("Show users online on forum index"), required=False,
  128. help_text=_("Selecting this option will make forum inform other "
  129. "users of their availability by displaying them on forum "
  130. "index page."))
  131. class Meta:
  132. model = Rank
  133. fields = [
  134. 'name',
  135. 'description',
  136. 'css_class',
  137. 'title',
  138. 'roles',
  139. 'is_tab',
  140. 'is_on_index',
  141. ]
  142. def clean(self):
  143. data = super(RankForm, self).clean()
  144. self.instance.set_name(data.get('name'))
  145. return data