bestanswers.py 12 KB

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