bestanswers.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. category_acl = acl['categories'][category.pk] = build_category_acl(
  57. category_acl, category, categories_roles, key_name
  58. )
  59. return acl
  60. def build_category_acl(acl, category, categories_roles, key_name):
  61. category_roles = categories_roles.get(category.pk, [])
  62. final_acl = {
  63. 'can_mark_best_answers': 0,
  64. 'can_change_marked_answers': 0,
  65. 'best_answer_change_time': 0,
  66. }
  67. final_acl.update(acl)
  68. algebra.sum_acls(
  69. final_acl,
  70. roles=category_roles,
  71. key=key_name,
  72. can_mark_best_answers=algebra.greater,
  73. can_change_marked_answers=algebra.greater,
  74. best_answer_change_time=algebra.greater_or_zero,
  75. )
  76. return final_acl
  77. def add_acl_to_post(user, post):
  78. post.acl.update({
  79. 'can_mark_as_best_answer': can_mark_as_best_answer(user, post),
  80. 'can_unmark_best_answer': can_unmark_best_answer(user, post),
  81. })
  82. def register_with(registry):
  83. registry.acl_annotator(Post, add_acl_to_post)
  84. def allow_mark_as_best_answer(user, target):
  85. if user.is_anonymous:
  86. raise PermissionDenied(_("You have to sign in to mark best answers."))
  87. if target.is_event:
  88. raise PermissionDenied(_("Events can't be marked as best answers."))
  89. category_acl = user.acl_cache['categories'].get(
  90. target.category_id, {
  91. 'can_mark_best_answers': 0,
  92. }
  93. )
  94. if not category_acl['can_mark_best_answers']:
  95. raise PermissionDenied(
  96. _(
  97. 'You don\'t have permission to mark best answers in the "%(category)s" category.'
  98. ) % {
  99. 'category': target.category,
  100. }
  101. )
  102. if category_acl['can_mark_best_answers'] == 1 and target.thread.starter != user:
  103. raise PermissionDenied(
  104. _(
  105. "You don't have permission to mark best answer in this thread because you "
  106. "didn't start it."
  107. )
  108. )
  109. if target.is_first_post:
  110. raise PermissionDenied(_("First post in a thread can't be marked as best answer."))
  111. if target.is_hidden:
  112. raise PermissionDenied(_("Hidden posts can't be marked as best answers."))
  113. if target.is_unapproved:
  114. raise PermissionDenied(_("Unapproved posts can't be marked as best answers."))
  115. if target.is_answer:
  116. raise PermissionDenied(_("This post is already marked as best answer."))
  117. if target.thread.best_answer_id:
  118. if not category_acl['can_change_marked_answers']:
  119. raise PermissionDenied(
  120. _(
  121. 'You don\'t have permission to change marked best answers in the '
  122. '"%(category)s" category.'
  123. ) % {
  124. 'category': target.category,
  125. }
  126. )
  127. if (category_acl['can_change_marked_answers'] == 1 and
  128. not has_time_to_change_answer(user, target)):
  129. raise PermissionDenied(
  130. ungettext(
  131. (
  132. "You don't have permission to change best answer that was marked for more "
  133. "than %(minutes)s minute."
  134. ),
  135. (
  136. "You don't have permission to change best answer that was marked for more "
  137. "than %(minutes)s minutes."
  138. ),
  139. category_acl['answer_change_time'],
  140. ) % {
  141. 'minutes': category_acl['answer_change_time'],
  142. }
  143. )
  144. if target.thread.best_answer_is_protected and not category_acl['can_protect_posts']:
  145. raise PermissionDenied(
  146. _(
  147. "You don't have permission to change this thread's marked best answer because "
  148. "a moderator has protected it."
  149. )
  150. )
  151. if not category_acl['can_close_threads']:
  152. if target.category.is_closed:
  153. raise PermissionDenied(
  154. _(
  155. 'You don\'t have permission to mark this post as best answer because its '
  156. 'category "%(category)s" is closed.'
  157. ) % {
  158. 'category': target.category,
  159. }
  160. )
  161. if target.thread.is_closed:
  162. raise PermissionDenied(
  163. _(
  164. "You can't mark this post as best answer because its thread is closed and you "
  165. "don't have permission to open it."
  166. )
  167. )
  168. if target.is_protected and not category_acl['can_protect_posts']:
  169. raise PermissionDenied(
  170. _(
  171. "You don't have permission to mark this post as best answer because a moderator "
  172. "has protected it."
  173. )
  174. )
  175. can_mark_as_best_answer = return_boolean(allow_mark_as_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. category_acl = user.acl_cache['categories'].get(
  180. target.category_id, {
  181. 'can_mark_best_answers': 0,
  182. }
  183. )
  184. if not category_acl['can_mark_best_answers']:
  185. raise PermissionDenied(
  186. _(
  187. 'You don\'t have permission to unmark threads answers in the "%(category)s" '
  188. 'category.'
  189. ) % {
  190. 'category': target.category,
  191. }
  192. )
  193. if not target.is_answer:
  194. raise PermissionDenied(
  195. _(
  196. "This post can't be unmarked because it's not currently marked as best answer."
  197. )
  198. )
  199. if category_acl['can_mark_best_answers'] == 1:
  200. if target.thread.starter != user:
  201. raise PermissionDenied(
  202. _(
  203. "You don't have permission to unmark this best answer because you are not a "
  204. "thread starter."
  205. )
  206. )
  207. if not has_time_to_change_answer(user, target):
  208. raise PermissionDenied(
  209. ungettext(
  210. (
  211. "You don't have permission to unmark best answer that was marked for more "
  212. "than %(minutes)s minute."
  213. ),
  214. (
  215. "You don't have permission to unmark best answer that was marked for more "
  216. "than %(minutes)s minutes."
  217. ),
  218. category_acl['answer_change_time'],
  219. ) % {
  220. 'minutes': category_acl['answer_change_time'],
  221. }
  222. )
  223. if not category_acl['can_close_threads']:
  224. if target.category.is_closed:
  225. raise PermissionDenied(
  226. _(
  227. 'You don\'t have permission to unmark this best answer because its category '
  228. '"%(category)s" is closed.'
  229. ) % {
  230. 'category': target.category,
  231. }
  232. )
  233. if target.thread.is_closed:
  234. raise PermissionDenied(
  235. _(
  236. "You can't unmark this best answer because its thread is closed and you don't "
  237. "have permission to open it."
  238. )
  239. )
  240. if target.is_protected and not category_acl['can_protect_posts']:
  241. raise PermissionDenied(
  242. _(
  243. "You don't have permission to unmark this best answer because a moderator has "
  244. "protected it."
  245. )
  246. )
  247. can_unmark_best_answer = return_boolean(allow_unmark_best_answer)
  248. def has_time_to_change_answer(user, target):
  249. category_acl = user.acl_cache['categories'].get(target.category_id, {})
  250. change_time = category_acl.get('best_answer_change_time', 0)
  251. if change_time:
  252. diff = timezone.now() - target.thread.best_answer_set_on
  253. diff_minutes = int(diff.total_seconds() / 60)
  254. return diff_minutes < change_time
  255. else:
  256. return True