polls.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. from django import forms
  2. from django.core.exceptions import PermissionDenied
  3. from django.utils import timezone
  4. from django.utils.translation import gettext_lazy as _
  5. from django.utils.translation import ngettext
  6. from ...acl import algebra
  7. from ...acl.decorators import return_boolean
  8. from ...acl.models import Role
  9. from ...admin.forms import YesNoSwitch
  10. from ..models import Poll, Thread
  11. __all__ = [
  12. "allow_start_poll",
  13. "can_start_poll",
  14. "allow_edit_poll",
  15. "can_edit_poll",
  16. "allow_delete_poll",
  17. "can_delete_poll",
  18. "allow_vote_poll",
  19. "can_vote_poll",
  20. "allow_see_poll_votes",
  21. "can_see_poll_votes",
  22. ]
  23. class RolePermissionsForm(forms.Form):
  24. legend = _("Polls")
  25. can_start_polls = forms.TypedChoiceField(
  26. label=_("Can start polls"),
  27. coerce=int,
  28. initial=0,
  29. choices=[(0, _("No")), (1, _("Own threads")), (2, _("All threads"))],
  30. )
  31. can_edit_polls = forms.TypedChoiceField(
  32. label=_("Can edit polls"),
  33. coerce=int,
  34. initial=0,
  35. choices=[(0, _("No")), (1, _("Own polls")), (2, _("All polls"))],
  36. )
  37. can_delete_polls = forms.TypedChoiceField(
  38. label=_("Can delete polls"),
  39. coerce=int,
  40. initial=0,
  41. choices=[(0, _("No")), (1, _("Own polls")), (2, _("All polls"))],
  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 = YesNoSwitch(
  50. label=_("Can always see polls voters"),
  51. help_text=_(
  52. "Allows users to see who voted in poll even if poll votes are secret."
  53. ),
  54. )
  55. def change_permissions_form(role):
  56. if isinstance(role, Role) and role.special_role != "anonymous":
  57. return RolePermissionsForm
  58. def build_acl(acl, roles, key_name):
  59. acl.update(
  60. {
  61. "can_start_polls": 0,
  62. "can_edit_polls": 0,
  63. "can_delete_polls": 0,
  64. "poll_edit_time": 0,
  65. "can_always_see_poll_voters": 0,
  66. }
  67. )
  68. return algebra.sum_acls(
  69. acl,
  70. roles=roles,
  71. key=key_name,
  72. can_start_polls=algebra.greater,
  73. can_edit_polls=algebra.greater,
  74. can_delete_polls=algebra.greater,
  75. poll_edit_time=algebra.greater_or_zero,
  76. can_always_see_poll_voters=algebra.greater,
  77. )
  78. def add_acl_to_poll(user_acl, poll):
  79. poll.acl.update(
  80. {
  81. "can_vote": can_vote_poll(user_acl, poll),
  82. "can_edit": can_edit_poll(user_acl, poll),
  83. "can_delete": can_delete_poll(user_acl, poll),
  84. "can_see_votes": can_see_poll_votes(user_acl, poll),
  85. }
  86. )
  87. def add_acl_to_thread(user_acl, thread):
  88. thread.acl.update({"can_start_poll": can_start_poll(user_acl, thread)})
  89. def register_with(registry):
  90. registry.acl_annotator(Poll, add_acl_to_poll)
  91. registry.acl_annotator(Thread, add_acl_to_thread)
  92. def allow_start_poll(user_acl, target):
  93. if user_acl["is_anonymous"]:
  94. raise PermissionDenied(_("You have to sign in to start polls."))
  95. category_acl = user_acl["categories"].get(
  96. target.category_id, {"can_close_threads": False}
  97. )
  98. if not user_acl.get("can_start_polls"):
  99. raise PermissionDenied(_("You can't start polls."))
  100. if user_acl.get("can_start_polls") < 2 and user_acl["user_id"] != target.starter_id:
  101. raise PermissionDenied(_("You can't start polls in other users threads."))
  102. if not category_acl.get("can_close_threads"):
  103. if target.category.is_closed:
  104. raise PermissionDenied(
  105. _("This category is closed. You can't start polls in it.")
  106. )
  107. if target.is_closed:
  108. raise PermissionDenied(
  109. _("This thread is closed. You can't start polls in it.")
  110. )
  111. can_start_poll = return_boolean(allow_start_poll)
  112. def allow_edit_poll(user_acl, target):
  113. if user_acl["is_anonymous"]:
  114. raise PermissionDenied(_("You have to sign in to edit polls."))
  115. category_acl = user_acl["categories"].get(
  116. target.category_id, {"can_close_threads": False}
  117. )
  118. if not user_acl.get("can_edit_polls"):
  119. raise PermissionDenied(_("You can't edit polls."))
  120. if user_acl.get("can_edit_polls") < 2:
  121. if user_acl["user_id"] != target.poster_id:
  122. raise PermissionDenied(
  123. _("You can't edit other users polls in this category.")
  124. )
  125. if not has_time_to_edit_poll(user_acl, target):
  126. message = ngettext(
  127. "You can't edit polls that are older than %(minutes)s minute.",
  128. "You can't edit polls that are older than %(minutes)s minutes.",
  129. user_acl["poll_edit_time"],
  130. )
  131. raise PermissionDenied(message % {"minutes": user_acl["poll_edit_time"]})
  132. if target.is_over:
  133. raise PermissionDenied(_("This poll is over. You can't edit it."))
  134. if not category_acl.get("can_close_threads"):
  135. if target.category.is_closed:
  136. raise PermissionDenied(
  137. _("This category is closed. You can't edit polls in it.")
  138. )
  139. if target.thread.is_closed:
  140. raise PermissionDenied(
  141. _("This thread is closed. You can't edit polls in it.")
  142. )
  143. can_edit_poll = return_boolean(allow_edit_poll)
  144. def allow_delete_poll(user_acl, target):
  145. if user_acl["is_anonymous"]:
  146. raise PermissionDenied(_("You have to sign in to delete polls."))
  147. category_acl = user_acl["categories"].get(
  148. target.category_id, {"can_close_threads": False}
  149. )
  150. if not user_acl.get("can_delete_polls"):
  151. raise PermissionDenied(_("You can't delete polls."))
  152. if user_acl.get("can_delete_polls") < 2:
  153. if user_acl["user_id"] != target.poster_id:
  154. raise PermissionDenied(
  155. _("You can't delete other users polls in this category.")
  156. )
  157. if not has_time_to_edit_poll(user_acl, target):
  158. message = ngettext(
  159. "You can't delete polls that are older than %(minutes)s minute.",
  160. "You can't delete polls that are older than %(minutes)s minutes.",
  161. user_acl["poll_edit_time"],
  162. )
  163. raise PermissionDenied(message % {"minutes": user_acl["poll_edit_time"]})
  164. if target.is_over:
  165. raise PermissionDenied(_("This poll is over. You can't delete it."))
  166. if not category_acl.get("can_close_threads"):
  167. if target.category.is_closed:
  168. raise PermissionDenied(
  169. _("This category is closed. You can't delete polls in it.")
  170. )
  171. if target.thread.is_closed:
  172. raise PermissionDenied(
  173. _("This thread is closed. You can't delete polls in it.")
  174. )
  175. can_delete_poll = return_boolean(allow_delete_poll)
  176. def allow_vote_poll(user_acl, target):
  177. if user_acl["is_anonymous"]:
  178. raise PermissionDenied(_("You have to sign in to vote in polls."))
  179. if target.has_selected_choices and not target.allow_revotes:
  180. raise PermissionDenied(_("You have already voted in this poll."))
  181. if target.is_over:
  182. raise PermissionDenied(_("This poll is over. You can't vote in it."))
  183. category_acl = user_acl["categories"].get(
  184. target.category_id, {"can_close_threads": False}
  185. )
  186. if not category_acl.get("can_close_threads"):
  187. if target.category.is_closed:
  188. raise PermissionDenied(_("This category is closed. You can't vote in it."))
  189. if target.thread.is_closed:
  190. raise PermissionDenied(_("This thread is closed. You can't vote in it."))
  191. can_vote_poll = return_boolean(allow_vote_poll)
  192. def allow_see_poll_votes(user_acl, target):
  193. if not target.is_public and not user_acl["can_always_see_poll_voters"]:
  194. raise PermissionDenied(_("You dont have permission to this poll's voters."))
  195. can_see_poll_votes = return_boolean(allow_see_poll_votes)
  196. def has_time_to_edit_poll(user_acl, target):
  197. edit_time = user_acl["poll_edit_time"]
  198. if edit_time:
  199. diff = timezone.now() - target.posted_on
  200. diff_minutes = int(diff.total_seconds() / 60)
  201. return diff_minutes < edit_time
  202. return True