admin.py 5.6 KB

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