usercp.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from datetime import timedelta
  2. from django.utils.translation import ugettext_lazy as _
  3. from django.utils import timezone
  4. import floppyforms as forms
  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(label=_("Allowed Username changes number"),
  10. help_text=_("Enter zero to don't allow users with this role to change their names."),
  11. min_value=0, initial=1)
  12. form.base_fields['changes_expire'] = forms.IntegerField(label=_("Don't count username changes older than"),
  13. 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."),
  14. min_value=0, initial=0)
  15. form.base_fields['can_use_signature'] = forms.BooleanField(label=_("Can have signature"),
  16. widget=YesNoSwitch, initial=False, required=False)
  17. form.base_fields['allow_signature_links'] = forms.BooleanField(label=_("Can put links in signature"),
  18. widget=YesNoSwitch, initial=False, required=False)
  19. form.base_fields['allow_signature_images'] = forms.BooleanField(label=_("Can put images in signature"),
  20. widget=YesNoSwitch, initial=False, required=False)
  21. form.fieldsets.append((
  22. _("Profile Settings"),
  23. ('name_changes_allowed', 'changes_expire', 'can_use_signature', 'allow_signature_links', 'allow_signature_images')
  24. ))
  25. class UserCPACL(BaseACL):
  26. def show_username_change(self):
  27. return self.acl['name_changes_allowed'] > 0
  28. def changes_expire(self):
  29. return self.acl['changes_expire'] > 0
  30. def changes_left(self, user):
  31. if not self.acl['name_changes_allowed']:
  32. return 0
  33. if self.acl['changes_expire']:
  34. changes_left = self.acl['name_changes_allowed'] - user.namechanges.filter(
  35. date__gte=timezone.now() - timedelta(days=self.acl['changes_expire']),
  36. ).count()
  37. else:
  38. changes_left = self.acl['name_changes_allowed'] - user.namechanges.all().count()
  39. if changes_left:
  40. return changes_left
  41. return 0
  42. def can_use_signature(self):
  43. return self.acl['signature']
  44. def allow_signature_links(self):
  45. return self.acl['signature_links']
  46. def allow_signature_images(self):
  47. return self.acl['signature_images']
  48. def build(acl, roles):
  49. acl.usercp = UserCPACL()
  50. acl.usercp.acl['name_changes_allowed'] = 0
  51. acl.usercp.acl['changes_expire'] = 0
  52. acl.usercp.acl['signature'] = False
  53. acl.usercp.acl['signature_links'] = False
  54. acl.usercp.acl['signature_images'] = False
  55. for role in roles:
  56. try:
  57. if 'name_changes_allowed' in role and role['name_changes_allowed'] > acl.usercp.acl['name_changes_allowed']:
  58. acl.usercp.acl['name_changes_allowed'] = role['name_changes_allowed']
  59. if 'changes_expire' in role and role['changes_expire'] > acl.usercp.acl['changes_expire']:
  60. acl.usercp.acl['changes_expire'] = role['changes_expire']
  61. if 'can_use_signature' in role and role['can_use_signature']:
  62. acl.usercp.acl['signature'] = role['can_use_signature']
  63. if 'allow_signature_links' in role and role['allow_signature_links']:
  64. acl.usercp.acl['signature_links'] = role['allow_signature_links']
  65. if 'allow_signature_images' in role and role['allow_signature_images']:
  66. acl.usercp.acl['signature_images'] = role['allow_signature_images']
  67. except KeyError:
  68. pass