bestanswers.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 set posts as answers."))
  87. if target.is_event:
  88. raise PermissionDenied(_("Events can't be set as 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 set 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 dont't have permission to set this post as an answer "
  106. "because you are not the thread starter."
  107. )
  108. )
  109. if target.is_first_post:
  110. raise PermissionDenied(_("First post in a thread can't be set as an answer."))
  111. if target.is_hidden:
  112. raise PermissionDenied(_("Hidden posts can't be set as answers."))
  113. if target.is_unapproved:
  114. raise PermissionDenied(_("Unapproved posts can't be set as answers."))
  115. if target.is_answer:
  116. raise PermissionDenied(_("This post is already set as an answer."))
  117. if target.thread.best_answer_id:
  118. if not category_acl['can_change_marked_answers']:
  119. raise PermissionDenied(_("You don't have permission to change selected answer."))
  120. if (category_acl['can_change_marked_answers'] == 1 and
  121. not has_time_to_change_answer(user, target)):
  122. raise PermissionDenied(
  123. ungettext(
  124. (
  125. "You don't have permission to change thread's answer that was set "
  126. "for more than %(minutes)s minute."),
  127. (
  128. "You don't have permission to change thread's answer that was set "
  129. "for more than %(minutes)s minutes."),
  130. category_acl['answer_change_time'],
  131. ) % {
  132. 'minutes': category_acl['answer_change_time'],
  133. }
  134. )
  135. if target.thread.best_answer_is_protected and not category_acl['can_protect_posts']:
  136. raise PermissionDenied(
  137. _(
  138. "You don't have permission to change this thread's answer because moderator "
  139. "has protected it."
  140. )
  141. )
  142. if not category_acl['can_close_threads']:
  143. if target.category.is_closed:
  144. raise PermissionDenied(
  145. _(
  146. 'You can\'t sets this post as an answer because it\'s category '
  147. '"%(category)s" is closed.'
  148. ) % {
  149. 'category': target.category,
  150. }
  151. )
  152. if target.thread.is_closed:
  153. raise PermissionDenied(
  154. _(
  155. "You can't set this post as an answer because it's thread is closed and you "
  156. "don't have permission to open it."
  157. )
  158. )
  159. if target.is_protected and not category_acl['can_protect_posts']:
  160. raise PermissionDenied(
  161. _("You can't set this post as an answer because moderator has protected it.")
  162. )
  163. can_mark_as_best_answer = return_boolean(allow_mark_as_best_answer)
  164. def allow_unmark_best_answer(user, target):
  165. if user.is_anonymous:
  166. raise PermissionDenied(_("You have to sign in to unset threads answers."))
  167. category_acl = user.acl_cache['categories'].get(
  168. target.category_id, {
  169. 'can_mark_best_answers': 0,
  170. }
  171. )
  172. if not category_acl['can_mark_best_answers']:
  173. raise PermissionDenied(
  174. _(
  175. 'You don\'t have permission to unset threads answers in the "%(category)s" '
  176. 'category.'
  177. ) % {
  178. 'category': target.category,
  179. }
  180. )
  181. if not target.is_answer:
  182. raise PermissionDenied(
  183. _(
  184. "You can't unset."
  185. )
  186. )
  187. if category_acl['can_mark_best_answers'] == 1:
  188. if target.thread.starter != user:
  189. raise PermissionDenied(
  190. _(
  191. "You dont't have permission to unset this answer because "
  192. "you are not a thread starter."
  193. )
  194. )
  195. if not has_time_to_change_answer(user, target):
  196. raise PermissionDenied(
  197. ungettext(
  198. (
  199. "You don't have permission to change thread's answer that was set "
  200. "for more than %(minutes)s minute."),
  201. (
  202. "You don't have permission to change thread's answer that was set "
  203. "for more than %(minutes)s minutes."),
  204. category_acl['answer_change_time'],
  205. ) % {
  206. 'minutes': category_acl['answer_change_time'],
  207. }
  208. )
  209. if not category_acl['can_close_threads']:
  210. if target.category.is_closed:
  211. raise PermissionDenied(
  212. _(
  213. 'You can\'t unset this answer because it\'s scategory "%(category)s" is closed.'
  214. ) % {
  215. 'category': target.category,
  216. }
  217. )
  218. if target.thread.is_closed:
  219. raise PermissionDenied(
  220. _(
  221. "You don't have permission to unset this answer because it's thread is closed "
  222. "and you don't have permission to open it."
  223. )
  224. )
  225. if target.is_protected and not category_acl['can_protect_posts']:
  226. raise PermissionDenied(
  227. _(
  228. "You don't have permission to unset this thread's answer because moderator has "
  229. "protected it."
  230. )
  231. )
  232. can_unmark_best_answer = return_boolean(allow_unmark_best_answer)
  233. def has_time_to_change_answer(user, target):
  234. category_acl = user.acl_cache['categories'].get(target.category_id, {})
  235. change_time = category_acl.get('best_answer_change_time', 0)
  236. if change_time:
  237. diff = timezone.now() - target.thread.best_answer_set_on
  238. diff_minutes = int(diff.total_seconds() / 60)
  239. return diff_minutes < change_time
  240. else:
  241. return True