bestanswers.py 11 KB

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