bestanswers.py 10 KB

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