acl.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from datetime import timedelta
  2. from django import forms
  3. from django.utils.translation import ugettext_lazy as _
  4. from django.utils import timezone
  5. from misago.acl.builder import BaseACL
  6. from misago.forms import YesNoSwitch
  7. def make_form(request, role, form):
  8. if role.token != 'guest':
  9. form.base_fields['name_changes_allowed'] = forms.IntegerField(min_value=0,initial=1)
  10. form.base_fields['changes_expire'] = forms.IntegerField(min_value=0,initial=0)
  11. form.base_fields['can_use_signature'] = forms.BooleanField(widget=YesNoSwitch,initial=False,required=False)
  12. form.layout.append((
  13. _("User Profile"),
  14. (
  15. ('name_changes_allowed', {'label': _("Allowed Username changes number"), 'help_text': _("Enter zero to don't allow users with this role to change their names.")}),
  16. ('changes_expire', {'label': _("Don't count username changes older than"), 'help_text': _("Number of days since name change that makes that change no longer count to limit. For example, if you enter 7 days and set changes limit 3, users with this rank will not be able to make more than three changes in duration of 7 days. Enter zero to make all changes count.")}),
  17. ('can_use_signature', {'label': _("Can have signature")}),
  18. ),
  19. ))
  20. class UserCPACL(BaseACL):
  21. def show_username_change(self):
  22. return self.acl['name_changes_allowed'] > 0
  23. def changes_expire(self):
  24. return self.acl['changes_expire'] > 0
  25. def changes_left(self, user):
  26. if not self.acl['name_changes_allowed']:
  27. return 0
  28. if self.acl['changes_expire']:
  29. changes_left = self.acl['name_changes_allowed'] - user.namechanges.filter(
  30. date__gte=timezone.now() - timedelta(days=self.acl['changes_expire']),
  31. ).count()
  32. else:
  33. changes_left = self.acl['name_changes_allowed'] - user.namechanges.all().count()
  34. if changes_left:
  35. return changes_left
  36. return 0
  37. def can_use_signature(self):
  38. return self.acl['signature']
  39. def build(acl, roles):
  40. acl.usercp = UserCPACL()
  41. acl.usercp.acl['signature'] = False
  42. acl.usercp.acl['name_changes_allowed'] = 0
  43. acl.usercp.acl['changes_expire'] = 0
  44. for role in roles:
  45. if 'can_use_signature' in role and role['can_use_signature'] > acl.usercp.acl['signature']:
  46. acl.usercp.acl['signature'] = role['can_use_signature']
  47. if 'name_changes_allowed' in role and role['name_changes_allowed'] > acl.usercp.acl['name_changes_allowed']:
  48. acl.usercp.acl['name_changes_allowed'] = role['name_changes_allowed']
  49. if 'changes_expire' in role and role['changes_expire'] > acl.usercp.acl['changes_expire']:
  50. acl.usercp.acl['changes_expire'] = role['changes_expire']