threads.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. from django.core.exceptions import PermissionDenied
  2. from django.db.models import Q
  3. from django.http import Http404
  4. from django.utils import timezone
  5. from django.utils.translation import ungettext, ugettext_lazy as _
  6. from misago.acl import add_acl, algebra
  7. from misago.acl.decorators import return_boolean
  8. from misago.categories.models import Category, RoleCategoryACL, CategoryRole
  9. from misago.categories.permissions import get_categories_roles
  10. from misago.core import forms
  11. from misago.threads.models import Thread, Post, Event
  12. __all__ = [
  13. 'register_with',
  14. 'allow_see_thread',
  15. 'can_see_thread',
  16. 'allow_start_thread',
  17. 'can_start_thread',
  18. 'allow_reply_thread',
  19. 'can_reply_thread',
  20. 'allow_edit_thread',
  21. 'can_edit_thread',
  22. 'allow_see_post',
  23. 'can_see_post',
  24. 'allow_edit_post',
  25. 'can_edit_post',
  26. 'allow_unhide_post',
  27. 'can_unhide_post',
  28. 'allow_hide_post',
  29. 'can_hide_post',
  30. 'allow_delete_post',
  31. 'can_delete_post',
  32. 'exclude_invisible_threads',
  33. 'exclude_invisible_posts'
  34. ]
  35. """
  36. Admin Permissions Form
  37. """
  38. class PermissionsForm(forms.Form):
  39. legend = _("Threads")
  40. can_see_all_threads = forms.TypedChoiceField(
  41. label=_("Can see threads"),
  42. coerce=int,
  43. initial=0,
  44. choices=((0, _("Started threads")), (1, _("All threads")))
  45. )
  46. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  47. can_reply_threads = forms.YesNoSwitch(label=_("Can reply to threads"))
  48. can_edit_threads = forms.TypedChoiceField(
  49. label=_("Can edit threads"),
  50. coerce=int,
  51. initial=0,
  52. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads")))
  53. )
  54. can_hide_own_threads = forms.TypedChoiceField(
  55. label=_("Can hide own threads"),
  56. help_text=_("Only threads started within time limit and "
  57. "with no replies can be hidden."),
  58. coerce=int,
  59. initial=0,
  60. choices=(
  61. (0, _("No")),
  62. (1, _("Hide threads")),
  63. (2, _("Delete threads"))
  64. )
  65. )
  66. thread_edit_time = forms.IntegerField(
  67. label=_("Time limit for own threads edits, in minutes"),
  68. help_text=_("Enter 0 to don't limit time for editing own threads."),
  69. initial=0,
  70. min_value=0
  71. )
  72. can_hide_threads = forms.TypedChoiceField(
  73. label=_("Can hide all threads"),
  74. coerce=int,
  75. initial=0,
  76. choices=(
  77. (0, _("No")),
  78. (1, _("Hide threads")),
  79. (2, _("Delete threads"))
  80. )
  81. )
  82. can_edit_posts = forms.TypedChoiceField(
  83. label=_("Can edit posts"),
  84. coerce=int,
  85. initial=0,
  86. choices=((0, _("No")), (1, _("Own posts")), (2, _("All posts")))
  87. )
  88. can_hide_own_posts = forms.TypedChoiceField(
  89. label=_("Can hide own posts"),
  90. help_text=_("Only last posts to thread made within "
  91. "edit time limit can be hidden."),
  92. coerce=int,
  93. initial=0,
  94. choices=(
  95. (0, _("No")),
  96. (1, _("Hide posts")),
  97. (2, _("Delete posts"))
  98. )
  99. )
  100. post_edit_time = forms.IntegerField(
  101. label=_("Time limit for own post edits, in minutes"),
  102. help_text=_("Enter 0 to don't limit time for editing own posts."),
  103. initial=0,
  104. min_value=0
  105. )
  106. can_hide_posts = forms.TypedChoiceField(
  107. label=_("Can hide all posts"),
  108. coerce=int,
  109. initial=0,
  110. choices=(
  111. (0, _("No")),
  112. (1, _("Hide posts")),
  113. (2, _("Delete posts"))
  114. )
  115. )
  116. can_protect_posts = forms.YesNoSwitch(
  117. label=_("Can protect posts"),
  118. help_text=_("Only users with this permission can edit protected posts.")
  119. )
  120. can_move_posts = forms.YesNoSwitch(label=_("Can move posts"))
  121. can_merge_posts = forms.YesNoSwitch(label=_("Can merge posts"))
  122. can_pin_threads = forms.TypedChoiceField(
  123. label=_("Can pin threads"),
  124. coerce=int,
  125. initial=0,
  126. choices=(
  127. (0, _("No")),
  128. (1, _("Locally")),
  129. (2, _("Globally"))
  130. )
  131. )
  132. can_close_threads = forms.YesNoSwitch(label=_("Can close threads"))
  133. can_move_threads = forms.YesNoSwitch(label=_("Can move threads"))
  134. can_merge_threads = forms.YesNoSwitch(label=_("Can merge threads"))
  135. can_split_threads = forms.YesNoSwitch(label=_("Can split threads"))
  136. can_review_moderated_content = forms.YesNoSwitch(
  137. label=_("Can review moderated content"),
  138. help_text=_("Will see and be able to accept moderated content.")
  139. )
  140. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  141. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  142. can_hide_events = forms.TypedChoiceField(
  143. label=_("Can hide events"),
  144. coerce=int,
  145. initial=0,
  146. choices=(
  147. (0, _("No")),
  148. (1, _("Hide events")),
  149. (2, _("Delete events"))
  150. )
  151. )
  152. def change_permissions_form(role):
  153. if isinstance(role, CategoryRole):
  154. return PermissionsForm
  155. else:
  156. return None
  157. """
  158. ACL Builder
  159. """
  160. def build_acl(acl, roles, key_name):
  161. acl['can_review_moderated_content'] = []
  162. acl['can_see_reports'] = []
  163. categories_roles = get_categories_roles(roles)
  164. for category in Category.objects.all_categories():
  165. category_acl = acl['categories'].get(category.pk, {'can_browse': 0})
  166. if category_acl['can_browse']:
  167. acl['categories'][category.pk] = build_category_acl(
  168. category_acl, category, categories_roles, key_name)
  169. if acl['categories'][category.pk]['can_review_moderated_content']:
  170. acl['can_review_moderated_content'].append(category.pk)
  171. if acl['categories'][category.pk]['can_see_reports']:
  172. acl['can_see_reports'].append(category.pk)
  173. return acl
  174. def build_category_acl(acl, category, categories_roles, key_name):
  175. category_roles = categories_roles.get(category.pk, [])
  176. final_acl = {
  177. 'can_see_all_threads': 0,
  178. 'can_start_threads': 0,
  179. 'can_reply_threads': 0,
  180. 'can_edit_threads': 0,
  181. 'can_edit_posts': 0,
  182. 'can_hide_own_threads': 0,
  183. 'can_hide_own_posts': 0,
  184. 'thread_edit_time': 0,
  185. 'post_edit_time': 0,
  186. 'can_hide_threads': 0,
  187. 'can_hide_posts': 0,
  188. 'can_protect_posts': 0,
  189. 'can_move_posts': 0,
  190. 'can_merge_posts': 0,
  191. 'can_pin_threads': 0,
  192. 'can_close_threads': 0,
  193. 'can_move_threads': 0,
  194. 'can_merge_threads': 0,
  195. 'can_split_threads': 0,
  196. 'can_review_moderated_content': 0,
  197. 'can_report_content': 0,
  198. 'can_see_reports': 0,
  199. 'can_hide_events': 0,
  200. }
  201. final_acl.update(acl)
  202. algebra.sum_acls(final_acl, roles=category_roles, key=key_name,
  203. can_see_all_threads=algebra.greater,
  204. can_start_threads=algebra.greater,
  205. can_reply_threads=algebra.greater,
  206. can_edit_threads=algebra.greater,
  207. can_edit_posts=algebra.greater,
  208. can_hide_threads=algebra.greater,
  209. can_hide_posts=algebra.greater,
  210. can_hide_own_threads=algebra.greater,
  211. can_hide_own_posts=algebra.greater,
  212. thread_edit_time=algebra.greater_or_zero,
  213. post_edit_time=algebra.greater_or_zero,
  214. can_protect_posts=algebra.greater,
  215. can_move_posts=algebra.greater,
  216. can_merge_posts=algebra.greater,
  217. can_pin_threads=algebra.greater,
  218. can_close_threads=algebra.greater,
  219. can_move_threads=algebra.greater,
  220. can_merge_threads=algebra.greater,
  221. can_split_threads=algebra.greater,
  222. can_review_moderated_content=algebra.greater,
  223. can_report_content=algebra.greater,
  224. can_see_reports=algebra.greater,
  225. can_hide_events=algebra.greater,
  226. )
  227. return final_acl
  228. """
  229. ACL's for targets
  230. """
  231. def add_acl_to_category(user, category):
  232. category_acl = user.acl['categories'].get(category.pk, {})
  233. category.acl.update({
  234. 'can_see_all_threads': 0,
  235. 'can_start_threads': 0,
  236. 'can_reply_threads': 0,
  237. 'can_edit_threads': 0,
  238. 'can_edit_posts': 0,
  239. 'can_hide_own_threads': 0,
  240. 'can_hide_own_posts': 0,
  241. 'thread_edit_time': 0,
  242. 'post_edit_time': 0,
  243. 'can_hide_threads': 0,
  244. 'can_hide_posts': 0,
  245. 'can_protect_posts': 0,
  246. 'can_move_posts': 0,
  247. 'can_merge_posts': 0,
  248. 'can_pin_threads': 0,
  249. 'can_close_threads': 0,
  250. 'can_move_threads': 0,
  251. 'can_merge_threads': 0,
  252. 'can_split_threads': 0,
  253. 'can_review_moderated_content': 0,
  254. 'can_report_content': 0,
  255. 'can_see_reports': 0,
  256. 'can_hide_events': 0,
  257. })
  258. algebra.sum_acls(category.acl, acls=[category_acl],
  259. can_see_all_threads=algebra.greater)
  260. if user.is_authenticated():
  261. algebra.sum_acls(category.acl, acls=[category_acl],
  262. can_start_threads=algebra.greater,
  263. can_reply_threads=algebra.greater,
  264. can_edit_threads=algebra.greater,
  265. can_edit_posts=algebra.greater,
  266. can_hide_threads=algebra.greater,
  267. can_hide_posts=algebra.greater,
  268. can_hide_own_threads=algebra.greater,
  269. can_hide_own_posts=algebra.greater,
  270. thread_edit_time=algebra.greater_or_zero,
  271. post_edit_time=algebra.greater_or_zero,
  272. can_protect_posts=algebra.greater,
  273. can_move_posts=algebra.greater,
  274. can_merge_posts=algebra.greater,
  275. can_pin_threads=algebra.greater,
  276. can_close_threads=algebra.greater,
  277. can_move_threads=algebra.greater,
  278. can_merge_threads=algebra.greater,
  279. can_split_threads=algebra.greater,
  280. can_review_moderated_content=algebra.greater,
  281. can_report_content=algebra.greater,
  282. can_see_reports=algebra.greater,
  283. can_hide_events=algebra.greater,
  284. )
  285. category.acl['can_see_own_threads'] = not category.acl['can_see_all_threads']
  286. def add_acl_to_thread(user, thread):
  287. category_acl = user.acl['categories'].get(thread.category_id, {})
  288. thread.acl.update({
  289. 'can_reply': can_reply_thread(user, thread),
  290. 'can_edit': can_edit_thread(user, thread),
  291. 'can_hide': category_acl.get('can_hide_threads', False),
  292. 'can_pin': category_acl.get('can_pin_threads', 0),
  293. 'can_close': category_acl.get('can_close_threads', False),
  294. 'can_move': category_acl.get('can_move_threads', False),
  295. 'can_review': category_acl.get('can_review_moderated_content', False),
  296. 'can_report': category_acl.get('can_report_content', False),
  297. 'can_see_reports': category_acl.get('can_see_reports', False),
  298. })
  299. if can_change_owned_thread(user, thread):
  300. if not category_acl.get('can_close_threads'):
  301. thread_is_protected = thread.is_closed or thread.category.is_closed
  302. else:
  303. thread_is_protected = False
  304. if not thread_is_protected and not thread.acl['can_hide']:
  305. if not thread.replies:
  306. can_hide_thread = category_acl.get('can_hide_own_threads')
  307. thread.acl['can_hide'] = can_hide_thread
  308. def add_acl_to_post(user, post):
  309. category_acl = user.acl['categories'].get(post.category_id, {})
  310. post.acl.update({
  311. 'can_reply': can_reply_thread(user, post.thread),
  312. 'can_edit': can_edit_post(user, post),
  313. 'can_see_hidden': category_acl.get('can_hide_posts'),
  314. 'can_unhide': can_unhide_post(user, post),
  315. 'can_hide': can_hide_post(user, post),
  316. 'can_delete': can_delete_post(user, post),
  317. 'can_protect': category_acl.get('can_protect_posts', False),
  318. 'can_report': category_acl.get('can_report_content', False),
  319. 'can_see_reports': category_acl.get('can_see_reports', False),
  320. 'can_approve': category_acl.get('can_review_moderated_content', False),
  321. })
  322. if not post.is_moderated:
  323. post.acl['can_approve'] = False
  324. if not post.acl['can_see_hidden']:
  325. if user.is_authenticated() and user.id == post.poster_id:
  326. post.acl['can_see_hidden'] = True
  327. else:
  328. post.acl['can_see_hidden'] = post.id == post.thread.first_post_id
  329. def add_acl_to_event(user, event):
  330. category_acl = user.acl['categories'].get(event.category_id, {})
  331. can_hide_events = category_acl.get('can_hide_events', 0)
  332. event.acl['can_hide'] = can_hide_events > 0
  333. event.acl['can_delete'] = can_hide_events == 2
  334. def register_with(registry):
  335. registry.acl_annotator(Category, add_acl_to_category)
  336. registry.acl_annotator(Thread, add_acl_to_thread)
  337. registry.acl_annotator(Post, add_acl_to_post)
  338. registry.acl_annotator(Event, add_acl_to_event)
  339. """
  340. ACL tests
  341. """
  342. def allow_see_thread(user, target):
  343. category_acl = user.acl['categories'].get(target.category_id, {})
  344. if not category_acl.get('can_browse'):
  345. raise Http404()
  346. if user.is_anonymous() or user.pk != target.starter_id:
  347. if not category_acl.get('can_see_all_threads'):
  348. raise Http404()
  349. if target.is_moderated:
  350. if not category_acl.get('can_review_moderated_content'):
  351. raise Http404()
  352. if target.is_hidden and not category_acl.get('can_hide_threads'):
  353. raise Http404()
  354. can_see_thread = return_boolean(allow_see_thread)
  355. def allow_start_thread(user, target):
  356. if user.is_anonymous():
  357. raise PermissionDenied(_("You have to sign in to start threads."))
  358. if target.is_closed and not target.acl['can_close_threads']:
  359. raise PermissionDenied(
  360. _("This category is closed. You can't start new threads in it."))
  361. if not user.acl['categories'].get(target.id, {'can_start_threads': False}):
  362. raise PermissionDenied(_("You don't have permission to start "
  363. "new threads in this category."))
  364. can_start_thread = return_boolean(allow_start_thread)
  365. def allow_reply_thread(user, target):
  366. if user.is_anonymous():
  367. raise PermissionDenied(_("You have to sign in to reply threads."))
  368. category_acl = target.category.acl
  369. if not category_acl['can_close_threads']:
  370. if target.category.is_closed:
  371. raise PermissionDenied(
  372. _("This category is closed. You can't reply to threads in it."))
  373. if target.is_closed:
  374. raise PermissionDenied(
  375. _("You can't reply to closed threads in this category."))
  376. if not category_acl['can_reply_threads']:
  377. raise PermissionDenied(_("You can't reply to threads in this category."))
  378. can_reply_thread = return_boolean(allow_reply_thread)
  379. def allow_edit_thread(user, target):
  380. if user.is_anonymous():
  381. raise PermissionDenied(_("You have to sign in to edit threads."))
  382. category_acl = target.category.acl
  383. if not category_acl['can_edit_threads']:
  384. raise PermissionDenied(_("You can't edit threads in this category."))
  385. if category_acl['can_edit_threads'] == 1:
  386. if target.starter_id != user.pk:
  387. raise PermissionDenied(
  388. _("You can't edit other users threads in this category."))
  389. if not category_acl['can_close_threads']:
  390. if target.category.is_closed:
  391. raise PermissionDenied(
  392. _("This category is closed. You can't edit threads in it."))
  393. if target.is_closed:
  394. raise PermissionDenied(
  395. _("You can't edit closed threads in this category."))
  396. if not has_time_to_edit_thread(user, target):
  397. message = ungettext("You can't edit threads that are "
  398. "older than %(minutes)s minute.",
  399. "You can't edit threads that are "
  400. "older than %(minutes)s minutes.",
  401. category_acl['thread_edit_time'])
  402. raise PermissionDenied(
  403. message % {'minutes': category_acl['thread_edit_time']})
  404. can_edit_thread = return_boolean(allow_edit_thread)
  405. def allow_see_post(user, target):
  406. if target.is_moderated:
  407. category_acl = user.acl['categories'].get(target.category_id, {})
  408. if not category_acl.get('can_review_moderated_content'):
  409. if user.is_anonymous() or user.pk != target.poster_id:
  410. raise Http404()
  411. can_see_post = return_boolean(allow_see_post)
  412. def allow_edit_post(user, target):
  413. if user.is_anonymous():
  414. raise PermissionDenied(_("You have to sign in to edit posts."))
  415. category_acl = target.category.acl
  416. if not category_acl['can_edit_posts']:
  417. raise PermissionDenied(_("You can't edit posts in this category."))
  418. if target.is_hidden and not can_unhide_post(user, target):
  419. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  420. if category_acl['can_edit_posts'] == 1:
  421. if target.poster_id != user.pk:
  422. raise PermissionDenied(
  423. _("You can't edit other users posts in this category."))
  424. if not category_acl['can_close_threads']:
  425. if target.category.is_closed:
  426. raise PermissionDenied(
  427. _("This category is closed. You can't edit posts in it."))
  428. if target.thread.is_closed:
  429. raise PermissionDenied(
  430. _("This thread is closed. You can't edit posts in it."))
  431. if target.is_protected and not category_acl['can_protect_posts']:
  432. raise PermissionDenied(
  433. _("This post is protected. You can't edit it."))
  434. if not has_time_to_edit_post(user, target):
  435. message = ungettext("You can't edit posts that are "
  436. "older than %(minutes)s minute.",
  437. "You can't edit posts that are "
  438. "older than %(minutes)s minutes.",
  439. category_acl['post_edit_time'])
  440. raise PermissionDenied(
  441. message % {'minutes': category_acl['post_edit_time']})
  442. can_edit_post = return_boolean(allow_edit_post)
  443. def allow_unhide_post(user, target):
  444. if user.is_anonymous():
  445. raise PermissionDenied(_("You have to sign in to reveal posts."))
  446. category_acl = target.category.acl
  447. if not category_acl['can_hide_posts']:
  448. if not category_acl['can_hide_own_posts']:
  449. raise PermissionDenied(_("You can't reveal posts in this category."))
  450. if user.id != target.poster_id:
  451. raise PermissionDenied(
  452. _("You can't reveal other users posts in this category."))
  453. if not category_acl['can_close_threads']:
  454. if target.category.is_closed:
  455. raise PermissionDenied(_("This category is closed. You can't "
  456. "reveal posts in it."))
  457. if target.thread.is_closed:
  458. raise PermissionDenied(_("This thread is closed. You can't "
  459. "reveal posts in it."))
  460. if target.is_protected and not category_acl['can_protect_posts']:
  461. raise PermissionDenied(
  462. _("This post is protected. You can't reveal it."))
  463. if has_time_to_edit_post(user, target):
  464. message = ungettext("You can't reveal posts that are "
  465. "older than %(minutes)s minute.",
  466. "You can't reveal posts that are "
  467. "older than %(minutes)s minutes.",
  468. category_acl['post_edit_time'])
  469. raise PermissionDenied(
  470. message % {'minutes': category_acl['post_edit_time']})
  471. if target.id == target.thread.first_post_id:
  472. raise PermissionDenied(_("You can't reveal thread's first post."))
  473. if not target.is_hidden:
  474. raise PermissionDenied(_("Only hidden posts can be revealed."))
  475. can_unhide_post = return_boolean(allow_unhide_post)
  476. def allow_hide_post(user, target):
  477. if user.is_anonymous():
  478. raise PermissionDenied(_("You have to sign in to hide posts."))
  479. category_acl = target.category.acl
  480. if not category_acl['can_hide_posts']:
  481. if not category_acl['can_hide_own_posts']:
  482. raise PermissionDenied(_("You can't hide posts in this category."))
  483. if user.id != target.poster_id:
  484. raise PermissionDenied(
  485. _("You can't hide other users posts in this category."))
  486. if not category_acl['can_close_threads']:
  487. if target.category.is_closed:
  488. raise PermissionDenied(_("This category is closed. You can't "
  489. "hide posts in it."))
  490. if target.thread.is_closed:
  491. raise PermissionDenied(_("This thread is closed. You can't "
  492. "hide posts in it."))
  493. if target.is_protected and not category_acl['can_protect_posts']:
  494. raise PermissionDenied(
  495. _("This post is protected. You can't hide it."))
  496. if has_time_to_edit_post(user, target):
  497. message = ungettext("You can't hide posts that are "
  498. "older than %(minutes)s minute.",
  499. "You can't hide posts that are "
  500. "older than %(minutes)s minutes.",
  501. category_acl['post_edit_time'])
  502. raise PermissionDenied(
  503. message % {'minutes': category_acl['post_edit_time']})
  504. if target.id == target.thread.first_post_id:
  505. raise PermissionDenied(_("You can't hide thread's first post."))
  506. if target.is_hidden:
  507. raise PermissionDenied(_("Only visible posts can be hidden."))
  508. can_hide_post = return_boolean(allow_hide_post)
  509. def allow_delete_post(user, target):
  510. if user.is_anonymous():
  511. raise PermissionDenied(_("You have to sign in to delete posts."))
  512. category_acl = target.category.acl
  513. if category_acl['can_hide_posts'] != 2:
  514. if not category_acl['can_hide_own_posts'] != 2:
  515. raise PermissionDenied(
  516. _("You can't delete posts in this category."))
  517. if user.id != target.poster_id:
  518. raise PermissionDenied(
  519. _("You can't delete other users posts in this category."))
  520. if not category_acl['can_close_threads']:
  521. if target.category.is_closed:
  522. raise PermissionDenied(_("This category is closed. You can't "
  523. "delete posts from it."))
  524. if target.thread.is_closed:
  525. raise PermissionDenied(_("This thread is closed. You can't "
  526. "delete posts from it."))
  527. if target.is_protected and not category_acl['can_protect_posts']:
  528. raise PermissionDenied(
  529. _("This post is protected. You can't delete it."))
  530. if has_time_to_edit_post(user, target):
  531. message = ungettext("You can't delete posts that are "
  532. "older than %(minutes)s minute.",
  533. "You can't delete posts that are "
  534. "older than %(minutes)s minutes.",
  535. category_acl['post_edit_time'])
  536. raise PermissionDenied(
  537. message % {'minutes': category_acl['post_edit_time']})
  538. if target.id == target.thread.first_post_id:
  539. raise PermissionDenied(_("You can't delete thread's first post."))
  540. can_delete_post = return_boolean(allow_delete_post)
  541. """
  542. Permission check helpers
  543. """
  544. def can_change_owned_thread(user, target):
  545. category_acl = user.acl['categories'].get(target.category_id, {})
  546. if user.is_anonymous() or user.pk != target.starter_id:
  547. return False
  548. if target.category.is_closed or target.is_closed:
  549. return False
  550. if target.first_post.is_protected:
  551. return False
  552. return has_time_to_edit_thread(user, target)
  553. def has_time_to_edit_thread(user, target):
  554. category_acl = user.acl['categories'].get(target.category_id, {})
  555. if category_acl.get('thread_edit_time'):
  556. diff = timezone.now() - target.started_on
  557. diff_minutes = int(diff.total_seconds() / 60)
  558. return diff_minutes < category_acl.get('thread_edit_time')
  559. else:
  560. return True
  561. def has_time_to_edit_post(user, target):
  562. category_acl = user.acl['categories'].get(target.category_id, {})
  563. if category_acl.get('post_edit_time'):
  564. diff = timezone.now() - target.posted_on
  565. diff_minutes = int(diff.total_seconds() / 60)
  566. return diff_minutes < category_acl.get('post_edit_time')
  567. else:
  568. return True
  569. """
  570. Queryset helpers
  571. """
  572. def exclude_invisible_category_threads(queryset, user, category):
  573. if user.is_authenticated():
  574. condition_author = Q(starter_id=user.id)
  575. can_mod = category.acl['can_review_moderated_content']
  576. can_hide = category.acl['can_hide_threads']
  577. if not can_mod and not can_hide:
  578. condition = Q(is_moderated=False) & Q(is_hidden=False)
  579. queryset = queryset.filter(condition_author | condition)
  580. elif not can_mod:
  581. condition = Q(is_moderated=False)
  582. queryset = queryset.filter(condition_author | condition)
  583. elif not can_hide:
  584. condition = Q(is_hidden=False)
  585. queryset = queryset.filter(condition_author | condition)
  586. else:
  587. if not category.acl['can_review_moderated_content']:
  588. queryset = queryset.filter(is_moderated=False)
  589. if not category.acl['can_hide_threads']:
  590. queryset = queryset.filter(is_hidden=False)
  591. return queryset
  592. def exclude_invisible_threads(user, categories, queryset):
  593. show_all = []
  594. show_accepted_visible = []
  595. show_accepted = []
  596. show_visible = []
  597. show_owned = []
  598. show_owned_visible = []
  599. for category in categories:
  600. add_acl(user, category)
  601. if not (category.acl['can_see'] and category.acl['can_browse']):
  602. continue
  603. can_hide = category.acl['can_hide_threads']
  604. if category.acl['can_see_all_threads']:
  605. can_mod = category.acl['can_review_moderated_content']
  606. if can_mod and can_hide:
  607. show_all.append(category)
  608. elif user.is_authenticated():
  609. if not can_mod and not can_hide:
  610. show_accepted_visible.append(category)
  611. elif not can_mod:
  612. show_accepted.append(category)
  613. elif not can_hide:
  614. show_visible.append(category)
  615. else:
  616. show_accepted_visible.append(category)
  617. elif user.is_authenticated():
  618. if can_hide:
  619. show_owned.append(category)
  620. else:
  621. show_owned_visible.append(category)
  622. conditions = None
  623. if show_all:
  624. conditions = Q(category__in=show_all)
  625. if show_accepted_visible:
  626. if user.is_authenticated():
  627. condition = Q(
  628. Q(starter=user) | Q(is_moderated=False),
  629. category__in=show_accepted_visible,
  630. is_hidden=False,
  631. )
  632. else:
  633. condition = Q(
  634. category__in=show_accepted_visible,
  635. is_hidden=False,
  636. is_moderated=False,
  637. )
  638. if conditions:
  639. conditions = conditions | condition
  640. else:
  641. conditions = condition
  642. if show_accepted:
  643. condition = Q(
  644. Q(starter=user) | Q(is_moderated=False),
  645. category__in=show_accepted,
  646. )
  647. if conditions:
  648. conditions = conditions | condition
  649. else:
  650. conditions = condition
  651. if show_visible:
  652. condition = Q(category__in=show_visible, is_hidden=False)
  653. if conditions:
  654. conditions = conditions | condition
  655. else:
  656. conditions = condition
  657. if show_owned:
  658. condition = Q(category__in=show_owned, starter=user)
  659. if conditions:
  660. conditions = conditions | condition
  661. else:
  662. conditions = condition
  663. if show_owned_visible:
  664. condition = Q(
  665. category__in=show_owned_visible,
  666. starter=user,
  667. is_hidden=False,
  668. )
  669. if conditions:
  670. conditions = conditions | condition
  671. else:
  672. conditions = condition
  673. if conditions:
  674. return Thread.objects.filter(conditions)
  675. else:
  676. return Thread.objects.none()
  677. def exclude_invisible_posts(queryset, user, category):
  678. if not category.acl['can_review_moderated_content']:
  679. if user.is_authenticated():
  680. condition_author = Q(poster_id=user.id)
  681. condition = Q(is_moderated=False)
  682. queryset = queryset.filter(condition_author | condition)
  683. else:
  684. queryset = queryset.filter(is_moderated=False)
  685. return queryset