bestanswers.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 misago.acl import algebra
  7. from misago.acl.decorators import return_boolean
  8. from misago.admin.forms import YesNoSwitch
  9. from misago.categories.models import Category, CategoryRole
  10. from misago.categories.permissions import get_categories_roles
  11. from misago.threads.models import Post, Thread
  12. __all__nope = [
  13. "allow_mark_best_answer",
  14. "can_mark_best_answer",
  15. "allow_mark_as_best_answer",
  16. "can_mark_as_best_answer",
  17. "allow_unmark_best_answer",
  18. "can_unmark_best_answer",
  19. "allow_hide_best_answer",
  20. "can_hide_best_answer",
  21. "allow_delete_best_answer",
  22. "can_delete_best_answer",
  23. ]
  24. class CategoryPermissionsForm(forms.Form):
  25. legend = _("Best answers")
  26. can_mark_best_answers = forms.TypedChoiceField(
  27. label=_("Can mark posts as best answers"),
  28. coerce=int,
  29. initial=0,
  30. choices=[(0, _("No")), (1, _("Own threads")), (2, _("All threads"))],
  31. )
  32. can_change_marked_answers = forms.TypedChoiceField(
  33. label=_("Can change marked answers"),
  34. coerce=int,
  35. initial=0,
  36. choices=[(0, _("No")), (1, _("Own threads")), (2, _("All threads"))],
  37. )
  38. best_answer_change_time = forms.IntegerField(
  39. label=_(
  40. "Time limit for changing marked best answer in owned thread, in minutes"
  41. ),
  42. help_text=_(
  43. "Enter 0 to don't limit time for changing marked best answer in owned thread."
  44. ),
  45. initial=0,
  46. min_value=0,
  47. )
  48. def change_permissions_form(role):
  49. if isinstance(role, CategoryRole):
  50. return CategoryPermissionsForm
  51. else:
  52. return None
  53. def build_acl(acl, roles, key_name):
  54. categories_roles = get_categories_roles(roles)
  55. categories = list(Category.objects.all_categories(include_root=True))
  56. for category in categories:
  57. category_acl = acl["categories"].get(category.pk, {"can_browse": 0})
  58. if category_acl["can_browse"]:
  59. acl["categories"][category.pk] = build_category_acl(
  60. category_acl, category, categories_roles, key_name
  61. )
  62. private_category = Category.objects.private_threads()
  63. private_threads_acl = acl["categories"].get(private_category.pk)
  64. if private_threads_acl:
  65. private_threads_acl.update(
  66. {
  67. "can_mark_best_answers": 0,
  68. "can_change_marked_answers": 0,
  69. "best_answer_change_time": 0,
  70. }
  71. )
  72. return acl
  73. def build_category_acl(acl, category, categories_roles, key_name):
  74. category_roles = categories_roles.get(category.pk, [])
  75. final_acl = {
  76. "can_mark_best_answers": 0,
  77. "can_change_marked_answers": 0,
  78. "best_answer_change_time": 0,
  79. }
  80. final_acl.update(acl)
  81. algebra.sum_acls(
  82. final_acl,
  83. roles=category_roles,
  84. key=key_name,
  85. can_mark_best_answers=algebra.greater,
  86. can_change_marked_answers=algebra.greater,
  87. best_answer_change_time=algebra.greater_or_zero,
  88. )
  89. return final_acl
  90. def add_acl_to_thread(user_acl, thread):
  91. thread.acl.update(
  92. {
  93. "can_mark_best_answer": can_mark_best_answer(user_acl, thread),
  94. "can_change_best_answer": can_change_best_answer(user_acl, thread),
  95. "can_unmark_best_answer": can_unmark_best_answer(user_acl, thread),
  96. }
  97. )
  98. def add_acl_to_post(user_acl, post):
  99. post.acl.update(
  100. {
  101. "can_mark_as_best_answer": can_mark_as_best_answer(user_acl, post),
  102. "can_hide_best_answer": can_hide_best_answer(user_acl, post),
  103. "can_delete_best_answer": can_delete_best_answer(user_acl, post),
  104. }
  105. )
  106. def register_with(registry):
  107. registry.acl_annotator(Thread, add_acl_to_thread)
  108. registry.acl_annotator(Post, add_acl_to_post)
  109. def allow_mark_best_answer(user_acl, target):
  110. if user_acl["is_anonymous"]:
  111. raise PermissionDenied(_("You have to sign in to mark best answers."))
  112. category_acl = user_acl["categories"].get(target.category_id, {})
  113. if not category_acl.get("can_mark_best_answers"):
  114. raise PermissionDenied(
  115. _(
  116. 'You don\'t have permission to mark best answers in the "%(category)s" category.'
  117. )
  118. % {"category": target.category}
  119. )
  120. if (
  121. category_acl["can_mark_best_answers"] == 1
  122. and user_acl["user_id"] != target.starter_id
  123. ):
  124. raise PermissionDenied(
  125. _(
  126. "You don't have permission to mark best answer in this thread because you didn't "
  127. "start it."
  128. )
  129. )
  130. if not category_acl["can_close_threads"]:
  131. if target.category.is_closed:
  132. raise PermissionDenied(
  133. _(
  134. "You don't have permission to mark best answer in this thread because its "
  135. 'category "%(category)s" is closed.'
  136. )
  137. % {"category": target.category}
  138. )
  139. if target.is_closed:
  140. raise PermissionDenied(
  141. _(
  142. "You can't mark best answer in this thread because it's closed and you don't "
  143. "have permission to open it."
  144. )
  145. )
  146. can_mark_best_answer = return_boolean(allow_mark_best_answer)
  147. def allow_change_best_answer(user_acl, target):
  148. if not target.has_best_answer:
  149. return # shortcircut permission test
  150. category_acl = user_acl["categories"].get(target.category_id, {})
  151. if not category_acl.get("can_change_marked_answers"):
  152. raise PermissionDenied(
  153. _(
  154. "You don't have permission to change this thread's marked answer because it's "
  155. 'in the "%(category)s" category.'
  156. )
  157. % {"category": target.category}
  158. )
  159. if category_acl["can_change_marked_answers"] == 1:
  160. if user_acl["user_id"] != target.starter_id:
  161. raise PermissionDenied(
  162. _(
  163. "You don't have permission to change this thread's marked answer because you "
  164. "are not a thread starter."
  165. )
  166. )
  167. if not has_time_to_change_answer(user_acl, target):
  168. raise PermissionDenied(
  169. ngettext(
  170. (
  171. "You don't have permission to change best answer that was marked for more "
  172. "than %(minutes)s minute."
  173. ),
  174. (
  175. "You don't have permission to change best answer that was marked for more "
  176. "than %(minutes)s minutes."
  177. ),
  178. category_acl["best_answer_change_time"],
  179. )
  180. % {"minutes": category_acl["best_answer_change_time"]}
  181. )
  182. if target.best_answer_is_protected and not category_acl["can_protect_posts"]:
  183. raise PermissionDenied(
  184. _(
  185. "You don't have permission to change this thread's best answer because "
  186. "a moderator has protected it."
  187. )
  188. )
  189. can_change_best_answer = return_boolean(allow_change_best_answer)
  190. def allow_unmark_best_answer(user_acl, target):
  191. if user_acl["is_anonymous"]:
  192. raise PermissionDenied(_("You have to sign in to unmark best answers."))
  193. if not target.has_best_answer:
  194. return # shortcircut test
  195. category_acl = user_acl["categories"].get(target.category_id, {})
  196. if not category_acl.get("can_change_marked_answers"):
  197. raise PermissionDenied(
  198. _(
  199. 'You don\'t have permission to unmark threads answers in the "%(category)s" '
  200. "category."
  201. )
  202. % {"category": target.category}
  203. )
  204. if category_acl["can_change_marked_answers"] == 1:
  205. if user_acl["user_id"] != target.starter_id:
  206. raise PermissionDenied(
  207. _(
  208. "You don't have permission to unmark this best answer because you are not a "
  209. "thread starter."
  210. )
  211. )
  212. if not has_time_to_change_answer(user_acl, target):
  213. raise PermissionDenied(
  214. ngettext(
  215. (
  216. "You don't have permission to unmark best answer that was marked for more "
  217. "than %(minutes)s minute."
  218. ),
  219. (
  220. "You don't have permission to unmark best answer that was marked for more "
  221. "than %(minutes)s minutes."
  222. ),
  223. category_acl["best_answer_change_time"],
  224. )
  225. % {"minutes": category_acl["best_answer_change_time"]}
  226. )
  227. if not category_acl["can_close_threads"]:
  228. if target.category.is_closed:
  229. raise PermissionDenied(
  230. _(
  231. "You don't have permission to unmark this best answer because its category "
  232. '"%(category)s" is closed.'
  233. )
  234. % {"category": target.category}
  235. )
  236. if target.is_closed:
  237. raise PermissionDenied(
  238. _(
  239. "You can't unmark this thread's best answer because it's closed and you "
  240. "don't have permission to open it."
  241. )
  242. )
  243. if target.best_answer_is_protected and not category_acl["can_protect_posts"]:
  244. raise PermissionDenied(
  245. _(
  246. "You don't have permission to unmark this thread's best answer because a "
  247. "moderator has protected it."
  248. )
  249. )
  250. can_unmark_best_answer = return_boolean(allow_unmark_best_answer)
  251. def allow_mark_as_best_answer(user_acl, target):
  252. if user_acl["is_anonymous"]:
  253. raise PermissionDenied(_("You have to sign in to mark best answers."))
  254. if target.is_event:
  255. raise PermissionDenied(_("Events can't be marked as best answers."))
  256. category_acl = user_acl["categories"].get(target.category_id, {})
  257. if not category_acl.get("can_mark_best_answers"):
  258. raise PermissionDenied(
  259. _(
  260. 'You don\'t have permission to mark best answers in the "%(category)s" category.'
  261. )
  262. % {"category": target.category}
  263. )
  264. if (
  265. category_acl["can_mark_best_answers"] == 1
  266. and user_acl["user_id"] != target.thread.starter_id
  267. ):
  268. raise PermissionDenied(
  269. _(
  270. "You don't have permission to mark best answer in this thread because you "
  271. "didn't start it."
  272. )
  273. )
  274. if target.is_first_post:
  275. raise PermissionDenied(
  276. _("First post in a thread can't be marked as best answer.")
  277. )
  278. if target.is_hidden:
  279. raise PermissionDenied(_("Hidden posts can't be marked as best answers."))
  280. if target.is_unapproved:
  281. raise PermissionDenied(_("Unapproved posts can't be marked as best answers."))
  282. if target.is_protected and not category_acl["can_protect_posts"]:
  283. raise PermissionDenied(
  284. _(
  285. "You don't have permission to mark this post as best answer because a moderator "
  286. "has protected it."
  287. )
  288. )
  289. can_mark_as_best_answer = return_boolean(allow_mark_as_best_answer)
  290. def allow_hide_best_answer(user_acl, target):
  291. if target.is_best_answer:
  292. raise PermissionDenied(
  293. _("You can't hide this post because its marked as best answer.")
  294. )
  295. can_hide_best_answer = return_boolean(allow_hide_best_answer)
  296. def allow_delete_best_answer(user_acl, target):
  297. if target.is_best_answer:
  298. raise PermissionDenied(
  299. _("You can't delete this post because its marked as best answer.")
  300. )
  301. can_delete_best_answer = return_boolean(allow_delete_best_answer)
  302. def has_time_to_change_answer(user_acl, target):
  303. category_acl = user_acl["categories"].get(target.category_id, {})
  304. change_time = category_acl.get("best_answer_change_time", 0)
  305. if change_time:
  306. diff = timezone.now() - target.best_answer_marked_on
  307. diff_minutes = int(diff.total_seconds() / 60)
  308. return diff_minutes < change_time
  309. else:
  310. return True