bestanswers.py 12 KB

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