polls.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from django.core.exceptions import PermissionDenied
  2. from django.utils.translation import ugettext_lazy as _, ungettext
  3. from misago.acl import algebra
  4. from misago.acl.decorators import return_boolean
  5. from misago.acl.models import Role
  6. from misago.core import forms
  7. from ..models import Poll, Thread
  8. """
  9. Admin Permissions Forms
  10. """
  11. class RolePermissionsForm(forms.Form):
  12. legend = _("Polls")
  13. can_start_polls = forms.TypedChoiceField(
  14. label=_("Can start polls"),
  15. coerce=int,
  16. initial=0,
  17. choices=(
  18. (0, _("No")),
  19. (1, _("Own threads")),
  20. (2, _("All threads"))
  21. )
  22. )
  23. can_edit_polls = forms.TypedChoiceField(
  24. label=_("Can edit polls"),
  25. coerce=int,
  26. initial=0,
  27. choices=(
  28. (0, _("No")),
  29. (1, _("Own polls")),
  30. (2, _("All polls"))
  31. )
  32. )
  33. can_delete_polls = forms.TypedChoiceField(
  34. label=_("Can edit polls"),
  35. coerce=int,
  36. initial=0,
  37. choices=(
  38. (0, _("No")),
  39. (1, _("Own polls")),
  40. (2, _("All polls"))
  41. )
  42. )
  43. poll_edit_time = forms.IntegerField(
  44. label=_("Time limit for own polls edits, in minutes"),
  45. help_text=_("Enter 0 to don't limit time for editing own polls."),
  46. initial=0,
  47. min_value=0
  48. )
  49. can_always_see_poll_voters = forms.YesNoSwitch(
  50. label=_("Can always see polls voters"),
  51. help_text=_("Allows users to see who voted in poll even if poll votes are secret.")
  52. )
  53. def change_permissions_form(role):
  54. if isinstance(role, Role) and role.special_role != 'anonymous':
  55. return RolePermissionsForm
  56. else:
  57. return None
  58. """
  59. ACL Builder
  60. """
  61. def build_acl(acl, roles, key_name):
  62. acl.update({
  63. 'can_start_polls': 0,
  64. 'can_edit_polls': 0,
  65. 'can_delete_polls': 0,
  66. 'poll_edit_time': 0,
  67. 'can_always_see_poll_voters': 0
  68. })
  69. return algebra.sum_acls(acl, roles=roles, key=key_name,
  70. can_start_polls=algebra.greater,
  71. can_edit_polls=algebra.greater,
  72. can_delete_polls=algebra.greater,
  73. poll_edit_time=algebra.greater_or_zero,
  74. can_always_see_poll_voters=algebra.greater
  75. )
  76. """
  77. ACL's for targets
  78. """
  79. def add_acl_to_poll(user, poll):
  80. poll.acl.update({
  81. 'can_edit': False,
  82. 'can_delete': False,
  83. })
  84. def add_acl_to_thread(user, thread):
  85. thread.acl.update({
  86. 'can_start_poll': can_start_poll(user, thread)
  87. })
  88. def register_with(registry):
  89. registry.acl_annotator(Poll, add_acl_to_poll)
  90. registry.acl_annotator(Thread, add_acl_to_thread)
  91. """
  92. ACL tests
  93. """
  94. def allow_start_poll(user, target):
  95. if user.is_anonymous():
  96. raise PermissionDenied(_("You have to sign in to start polls."))
  97. category_acl = user.acl['categories'].get(target.category_id, {
  98. 'can_close_threads': False,
  99. })
  100. if not user.acl.get('can_start_polls'):
  101. raise PermissionDenied(_("You can't start polls."))
  102. if user.acl.get('can_start_polls') < 2 and user.pk != target.starter_id:
  103. raise PermissionDenied(_("You can't start polls in other users threads."))
  104. if not category_acl.get('can_close_threads'):
  105. if target.category.is_closed:
  106. raise PermissionDenied(_("This category is closed. You can't start polls in it."))
  107. if target.is_closed:
  108. raise PermissionDenied(_("This thread is closed. You can't start polls in it."))
  109. try:
  110. if target.poll:
  111. raise PermissionDenied(_("There's already a poll in this thread."))
  112. except Poll.DoesNotExist:
  113. pass
  114. can_start_poll = return_boolean(allow_start_poll)