usercp.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.special != '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.base_fields['allow_signature_links'] = forms.BooleanField(widget=YesNoSwitch, initial=False, required=False)
  13. form.base_fields['allow_signature_images'] = forms.BooleanField(widget=YesNoSwitch, initial=False, required=False)
  14. form.layout.append((
  15. _("Profile Settings"),
  16. (
  17. ('name_changes_allowed', {'label': _("Allowed Username changes number"), 'help_text': _("Enter zero to don't allow users with this role to change their names.")}),
  18. ('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.")}),
  19. ('can_use_signature', {'label': _("Can have signature")}),
  20. ('allow_signature_links', {'label': _("Can put links in signature")}),
  21. ('allow_signature_images', {'label': _("Can put images in signature")}),
  22. ),
  23. ))
  24. class UserCPACL(BaseACL):
  25. def show_username_change(self):
  26. return self.acl['name_changes_allowed'] > 0
  27. def changes_expire(self):
  28. return self.acl['changes_expire'] > 0
  29. def changes_left(self, user):
  30. if not self.acl['name_changes_allowed']:
  31. return 0
  32. if self.acl['changes_expire']:
  33. changes_left = self.acl['name_changes_allowed'] - user.namechanges.filter(
  34. date__gte=timezone.now() - timedelta(days=self.acl['changes_expire']),
  35. ).count()
  36. else:
  37. changes_left = self.acl['name_changes_allowed'] - user.namechanges.all().count()
  38. if changes_left:
  39. return changes_left
  40. return 0
  41. def can_use_signature(self):
  42. return self.acl['signature']
  43. def allow_signature_links(self):
  44. return self.acl['signature_links']
  45. def allow_signature_images(self):
  46. return self.acl['signature_images']
  47. def build(acl, roles):
  48. acl.usercp = UserCPACL()
  49. acl.usercp.acl['name_changes_allowed'] = 0
  50. acl.usercp.acl['changes_expire'] = 0
  51. acl.usercp.acl['signature'] = False
  52. acl.usercp.acl['signature_links'] = False
  53. acl.usercp.acl['signature_images'] = False
  54. for role in roles:
  55. if 'name_changes_allowed' in role and role['name_changes_allowed'] > acl.usercp.acl['name_changes_allowed']:
  56. acl.usercp.acl['name_changes_allowed'] = role['name_changes_allowed']
  57. if 'changes_expire' in role and role['changes_expire'] > acl.usercp.acl['changes_expire']:
  58. acl.usercp.acl['changes_expire'] = role['changes_expire']
  59. if 'can_use_signature' in role and role['can_use_signature']:
  60. acl.usercp.acl['signature'] = role['can_use_signature']
  61. if 'allow_signature_links' in role and role['allow_signature_links']:
  62. acl.usercp.acl['signature_links'] = role['allow_signature_links']
  63. if 'allow_signature_images' in role and role['allow_signature_images']:
  64. acl.usercp.acl['signature_images'] = role['allow_signature_images']