threads.py 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. from django import forms
  2. from django.core.exceptions import PermissionDenied
  3. from django.db.models import Q
  4. from django.http import Http404
  5. from django.utils import timezone
  6. from django.utils.translation import gettext_lazy as _
  7. from django.utils.translation import ngettext
  8. from ...acl import algebra
  9. from ...acl.decorators import return_boolean
  10. from ...acl.models import Role
  11. from ...acl.objectacl import add_acl_to_obj
  12. from ...admin.forms import YesNoSwitch
  13. from ...categories.models import Category, CategoryRole
  14. from ...categories.permissions import get_categories_roles
  15. from ..models import Post, Thread
  16. __all__ = [
  17. "allow_see_thread",
  18. "can_see_thread",
  19. "allow_start_thread",
  20. "can_start_thread",
  21. "allow_reply_thread",
  22. "can_reply_thread",
  23. "allow_edit_thread",
  24. "can_edit_thread",
  25. "allow_pin_thread",
  26. "can_pin_thread",
  27. "allow_unhide_thread",
  28. "can_unhide_thread",
  29. "allow_hide_thread",
  30. "can_hide_thread",
  31. "allow_delete_thread",
  32. "can_delete_thread",
  33. "allow_move_thread",
  34. "can_move_thread",
  35. "allow_merge_thread",
  36. "can_merge_thread",
  37. "allow_approve_thread",
  38. "can_approve_thread",
  39. "allow_see_post",
  40. "can_see_post",
  41. "allow_edit_post",
  42. "can_edit_post",
  43. "allow_unhide_post",
  44. "can_unhide_post",
  45. "allow_hide_post",
  46. "can_hide_post",
  47. "allow_delete_post",
  48. "can_delete_post",
  49. "allow_protect_post",
  50. "can_protect_post",
  51. "allow_approve_post",
  52. "can_approve_post",
  53. "allow_move_post",
  54. "can_move_post",
  55. "allow_merge_post",
  56. "can_merge_post",
  57. "allow_unhide_event",
  58. "can_unhide_event",
  59. "allow_split_post",
  60. "can_split_post",
  61. "allow_hide_event",
  62. "can_hide_event",
  63. "allow_delete_event",
  64. "can_delete_event",
  65. "exclude_invisible_threads",
  66. "exclude_invisible_posts",
  67. ]
  68. class RolePermissionsForm(forms.Form):
  69. legend = _("Threads")
  70. can_see_unapproved_content_lists = YesNoSwitch(
  71. label=_("Can see unapproved content list"),
  72. help_text=_(
  73. 'Allows access to "unapproved" tab on threads lists for '
  74. "easy listing of threads that are unapproved or contain "
  75. "unapproved posts. Despite the tab being available on all "
  76. "threads lists, it will only display threads belonging to "
  77. "categories in which the user has permission to approve "
  78. "content."
  79. ),
  80. )
  81. can_see_reported_content_lists = YesNoSwitch(
  82. label=_("Can see reported content list"),
  83. help_text=_(
  84. 'Allows access to "reported" tab on threads lists for '
  85. "easy listing of threads that contain reported posts. "
  86. "Despite the tab being available on all categories "
  87. "threads lists, it will only display threads belonging to "
  88. "categories in which the user has permission to see posts "
  89. "reports."
  90. ),
  91. )
  92. can_omit_flood_protection = YesNoSwitch(
  93. label=_("Can omit flood protection"),
  94. help_text=_("Allows posting more frequently than flood protection would."),
  95. )
  96. class CategoryPermissionsForm(forms.Form):
  97. legend = _("Threads")
  98. can_see_all_threads = forms.TypedChoiceField(
  99. label=_("Can see threads"),
  100. coerce=int,
  101. initial=0,
  102. choices=[(0, _("Started threads")), (1, _("All threads"))],
  103. )
  104. can_start_threads = YesNoSwitch(label=_("Can start threads"))
  105. can_reply_threads = YesNoSwitch(label=_("Can reply to threads"))
  106. can_edit_threads = forms.TypedChoiceField(
  107. label=_("Can edit threads"),
  108. coerce=int,
  109. initial=0,
  110. choices=[(0, _("No")), (1, _("Own threads")), (2, _("All threads"))],
  111. )
  112. can_hide_own_threads = forms.TypedChoiceField(
  113. label=_("Can hide own threads"),
  114. help_text=_(
  115. "Only threads started within time limit and "
  116. "with no replies can be hidden."
  117. ),
  118. coerce=int,
  119. initial=0,
  120. choices=[(0, _("No")), (1, _("Hide threads")), (2, _("Delete threads"))],
  121. )
  122. thread_edit_time = forms.IntegerField(
  123. label=_("Time limit for own threads edits, in minutes"),
  124. help_text=_("Enter 0 to don't limit time for editing own threads."),
  125. initial=0,
  126. min_value=0,
  127. )
  128. can_hide_threads = forms.TypedChoiceField(
  129. label=_("Can hide all threads"),
  130. coerce=int,
  131. initial=0,
  132. choices=[(0, _("No")), (1, _("Hide threads")), (2, _("Delete threads"))],
  133. )
  134. can_pin_threads = forms.TypedChoiceField(
  135. label=_("Can pin threads"),
  136. coerce=int,
  137. initial=0,
  138. choices=[(0, _("No")), (1, _("Locally")), (2, _("Globally"))],
  139. )
  140. can_close_threads = YesNoSwitch(label=_("Can close threads"))
  141. can_move_threads = YesNoSwitch(label=_("Can move threads"))
  142. can_merge_threads = YesNoSwitch(label=_("Can merge threads"))
  143. can_edit_posts = forms.TypedChoiceField(
  144. label=_("Can edit posts"),
  145. coerce=int,
  146. initial=0,
  147. choices=[(0, _("No")), (1, _("Own posts")), (2, _("All posts"))],
  148. )
  149. can_hide_own_posts = forms.TypedChoiceField(
  150. label=_("Can hide own posts"),
  151. help_text=_(
  152. "Only last posts to thread made within edit time limit can be hidden."
  153. ),
  154. coerce=int,
  155. initial=0,
  156. choices=[(0, _("No")), (1, _("Hide posts")), (2, _("Delete posts"))],
  157. )
  158. post_edit_time = forms.IntegerField(
  159. label=_("Time limit for own post edits, in minutes"),
  160. help_text=_("Enter 0 to don't limit time for editing own posts."),
  161. initial=0,
  162. min_value=0,
  163. )
  164. can_hide_posts = forms.TypedChoiceField(
  165. label=_("Can hide all posts"),
  166. coerce=int,
  167. initial=0,
  168. choices=[(0, _("No")), (1, _("Hide posts")), (2, _("Delete posts"))],
  169. )
  170. can_see_posts_likes = forms.TypedChoiceField(
  171. label=_("Can see posts likes"),
  172. coerce=int,
  173. initial=0,
  174. choices=[
  175. (0, _("No")),
  176. (1, _("Number only")),
  177. (2, _("Number and list of likers")),
  178. ],
  179. )
  180. can_like_posts = YesNoSwitch(
  181. label=_("Can like posts"),
  182. help_text=_("Only users with this permission to see likes can like posts."),
  183. )
  184. can_protect_posts = YesNoSwitch(
  185. label=_("Can protect posts"),
  186. help_text=_("Only users with this permission can edit protected posts."),
  187. )
  188. can_move_posts = YesNoSwitch(
  189. label=_("Can move posts"),
  190. help_text=_("Will be able to move posts to other threads."),
  191. )
  192. can_merge_posts = YesNoSwitch(label=_("Can merge posts"))
  193. can_approve_content = YesNoSwitch(
  194. label=_("Can approve content"),
  195. help_text=_("Will be able to see and approve unapproved content."),
  196. )
  197. can_report_content = YesNoSwitch(label=_("Can report posts"))
  198. can_see_reports = YesNoSwitch(label=_("Can see reports"))
  199. can_hide_events = forms.TypedChoiceField(
  200. label=_("Can hide events"),
  201. coerce=int,
  202. initial=0,
  203. choices=[(0, _("No")), (1, _("Hide events")), (2, _("Delete events"))],
  204. )
  205. require_threads_approval = YesNoSwitch(label=_("Require threads approval"))
  206. require_replies_approval = YesNoSwitch(label=_("Require replies approval"))
  207. require_edits_approval = YesNoSwitch(label=_("Require edits approval"))
  208. def change_permissions_form(role):
  209. if isinstance(role, Role) and role.special_role != "anonymous":
  210. return RolePermissionsForm
  211. elif isinstance(role, CategoryRole):
  212. return CategoryPermissionsForm
  213. else:
  214. return None
  215. def build_acl(acl, roles, key_name):
  216. acl.update(
  217. {
  218. "can_see_unapproved_content_lists": False,
  219. "can_see_reported_content_lists": False,
  220. "can_omit_flood_protection": False,
  221. "can_approve_content": [],
  222. "can_see_reports": [],
  223. }
  224. )
  225. acl = algebra.sum_acls(
  226. acl,
  227. roles=roles,
  228. key=key_name,
  229. can_see_unapproved_content_lists=algebra.greater,
  230. can_see_reported_content_lists=algebra.greater,
  231. can_omit_flood_protection=algebra.greater,
  232. )
  233. categories_roles = get_categories_roles(roles)
  234. categories = list(Category.objects.all_categories(include_root=True))
  235. for category in categories:
  236. category_acl = acl["categories"].get(category.pk, {"can_browse": 0})
  237. if category_acl["can_browse"]:
  238. category_acl = acl["categories"][category.pk] = build_category_acl(
  239. category_acl, category, categories_roles, key_name
  240. )
  241. if category_acl.get("can_approve_content"):
  242. acl["can_approve_content"].append(category.pk)
  243. if category_acl.get("can_see_reports"):
  244. acl["can_see_reports"].append(category.pk)
  245. return acl
  246. def build_category_acl(acl, category, categories_roles, key_name):
  247. category_roles = categories_roles.get(category.pk, [])
  248. final_acl = {
  249. "can_see_all_threads": 0,
  250. "can_start_threads": 0,
  251. "can_reply_threads": 0,
  252. "can_edit_threads": 0,
  253. "can_edit_posts": 0,
  254. "can_hide_own_threads": 0,
  255. "can_hide_own_posts": 0,
  256. "thread_edit_time": 0,
  257. "post_edit_time": 0,
  258. "can_hide_threads": 0,
  259. "can_hide_posts": 0,
  260. "can_protect_posts": 0,
  261. "can_move_posts": 0,
  262. "can_merge_posts": 0,
  263. "can_pin_threads": 0,
  264. "can_close_threads": 0,
  265. "can_move_threads": 0,
  266. "can_merge_threads": 0,
  267. "can_report_content": 0,
  268. "can_see_reports": 0,
  269. "can_see_posts_likes": 0,
  270. "can_like_posts": 0,
  271. "can_approve_content": 0,
  272. "require_threads_approval": 0,
  273. "require_replies_approval": 0,
  274. "require_edits_approval": 0,
  275. "can_hide_events": 0,
  276. }
  277. final_acl.update(acl)
  278. algebra.sum_acls(
  279. final_acl,
  280. roles=category_roles,
  281. key=key_name,
  282. can_see_all_threads=algebra.greater,
  283. can_start_threads=algebra.greater,
  284. can_reply_threads=algebra.greater,
  285. can_edit_threads=algebra.greater,
  286. can_edit_posts=algebra.greater,
  287. can_hide_threads=algebra.greater,
  288. can_hide_posts=algebra.greater,
  289. can_hide_own_threads=algebra.greater,
  290. can_hide_own_posts=algebra.greater,
  291. thread_edit_time=algebra.greater_or_zero,
  292. post_edit_time=algebra.greater_or_zero,
  293. can_protect_posts=algebra.greater,
  294. can_move_posts=algebra.greater,
  295. can_merge_posts=algebra.greater,
  296. can_pin_threads=algebra.greater,
  297. can_close_threads=algebra.greater,
  298. can_move_threads=algebra.greater,
  299. can_merge_threads=algebra.greater,
  300. can_report_content=algebra.greater,
  301. can_see_reports=algebra.greater,
  302. can_see_posts_likes=algebra.greater,
  303. can_like_posts=algebra.greater,
  304. can_approve_content=algebra.greater,
  305. require_threads_approval=algebra.greater,
  306. require_replies_approval=algebra.greater,
  307. require_edits_approval=algebra.greater,
  308. can_hide_events=algebra.greater,
  309. )
  310. return final_acl
  311. def add_acl_to_category(user_acl, category):
  312. category_acl = user_acl["categories"].get(category.pk, {})
  313. category.acl.update(
  314. {
  315. "can_see_all_threads": 0,
  316. "can_see_own_threads": 0,
  317. "can_start_threads": 0,
  318. "can_reply_threads": 0,
  319. "can_edit_threads": 0,
  320. "can_edit_posts": 0,
  321. "can_hide_own_threads": 0,
  322. "can_hide_own_posts": 0,
  323. "thread_edit_time": 0,
  324. "post_edit_time": 0,
  325. "can_hide_threads": 0,
  326. "can_hide_posts": 0,
  327. "can_protect_posts": 0,
  328. "can_move_posts": 0,
  329. "can_merge_posts": 0,
  330. "can_pin_threads": 0,
  331. "can_close_threads": 0,
  332. "can_move_threads": 0,
  333. "can_merge_threads": 0,
  334. "can_report_content": 0,
  335. "can_see_reports": 0,
  336. "can_see_posts_likes": 0,
  337. "can_like_posts": 0,
  338. "can_approve_content": 0,
  339. "require_threads_approval": category.require_threads_approval,
  340. "require_replies_approval": category.require_replies_approval,
  341. "require_edits_approval": category.require_edits_approval,
  342. "can_hide_events": 0,
  343. }
  344. )
  345. algebra.sum_acls(
  346. category.acl,
  347. acls=[category_acl],
  348. can_see_all_threads=algebra.greater,
  349. can_see_posts_likes=algebra.greater,
  350. )
  351. if user_acl["is_authenticated"]:
  352. algebra.sum_acls(
  353. category.acl,
  354. acls=[category_acl],
  355. can_start_threads=algebra.greater,
  356. can_reply_threads=algebra.greater,
  357. can_edit_threads=algebra.greater,
  358. can_edit_posts=algebra.greater,
  359. can_hide_threads=algebra.greater,
  360. can_hide_posts=algebra.greater,
  361. can_hide_own_threads=algebra.greater,
  362. can_hide_own_posts=algebra.greater,
  363. thread_edit_time=algebra.greater_or_zero,
  364. post_edit_time=algebra.greater_or_zero,
  365. can_protect_posts=algebra.greater,
  366. can_move_posts=algebra.greater,
  367. can_merge_posts=algebra.greater,
  368. can_pin_threads=algebra.greater,
  369. can_close_threads=algebra.greater,
  370. can_move_threads=algebra.greater,
  371. can_merge_threads=algebra.greater,
  372. can_report_content=algebra.greater,
  373. can_see_reports=algebra.greater,
  374. can_like_posts=algebra.greater,
  375. can_approve_content=algebra.greater,
  376. require_threads_approval=algebra.greater,
  377. require_replies_approval=algebra.greater,
  378. require_edits_approval=algebra.greater,
  379. can_hide_events=algebra.greater,
  380. )
  381. if user_acl["can_approve_content"]:
  382. category.acl.update(
  383. {
  384. "require_threads_approval": 0,
  385. "require_replies_approval": 0,
  386. "require_edits_approval": 0,
  387. }
  388. )
  389. category.acl["can_see_own_threads"] = not category.acl["can_see_all_threads"]
  390. def add_acl_to_thread(user_acl, thread):
  391. category_acl = user_acl["categories"].get(thread.category_id, {})
  392. thread.acl.update(
  393. {
  394. "can_reply": can_reply_thread(user_acl, thread),
  395. "can_edit": can_edit_thread(user_acl, thread),
  396. "can_pin": can_pin_thread(user_acl, thread),
  397. "can_pin_globally": False,
  398. "can_hide": can_hide_thread(user_acl, thread),
  399. "can_unhide": can_unhide_thread(user_acl, thread),
  400. "can_delete": can_delete_thread(user_acl, thread),
  401. "can_close": category_acl.get("can_close_threads", False),
  402. "can_move": can_move_thread(user_acl, thread),
  403. "can_merge": can_merge_thread(user_acl, thread),
  404. "can_move_posts": category_acl.get("can_move_posts", False),
  405. "can_merge_posts": category_acl.get("can_merge_posts", False),
  406. "can_approve": can_approve_thread(user_acl, thread),
  407. "can_see_reports": category_acl.get("can_see_reports", False),
  408. }
  409. )
  410. if thread.acl["can_pin"] and category_acl.get("can_pin_threads") == 2:
  411. thread.acl["can_pin_globally"] = True
  412. def add_acl_to_post(user_acl, post):
  413. if post.is_event:
  414. add_acl_to_event(user_acl, post)
  415. else:
  416. add_acl_to_reply(user_acl, post)
  417. def add_acl_to_event(user_acl, event):
  418. can_hide_events = 0
  419. if user_acl["is_authenticated"]:
  420. category_acl = user_acl["categories"].get(
  421. event.category_id, {"can_hide_events": 0}
  422. )
  423. can_hide_events = category_acl["can_hide_events"]
  424. event.acl.update(
  425. {
  426. "can_see_hidden": can_hide_events > 0,
  427. "can_hide": can_hide_event(user_acl, event),
  428. "can_delete": can_delete_event(user_acl, event),
  429. }
  430. )
  431. def add_acl_to_reply(user_acl, post):
  432. category_acl = user_acl["categories"].get(post.category_id, {})
  433. post.acl.update(
  434. {
  435. "can_reply": can_reply_thread(user_acl, post.thread),
  436. "can_edit": can_edit_post(user_acl, post),
  437. "can_see_hidden": post.is_first_post or category_acl.get("can_hide_posts"),
  438. "can_unhide": can_unhide_post(user_acl, post),
  439. "can_hide": can_hide_post(user_acl, post),
  440. "can_delete": can_delete_post(user_acl, post),
  441. "can_protect": can_protect_post(user_acl, post),
  442. "can_approve": can_approve_post(user_acl, post),
  443. "can_move": can_move_post(user_acl, post),
  444. "can_merge": can_merge_post(user_acl, post),
  445. "can_report": category_acl.get("can_report_content", False),
  446. "can_see_reports": category_acl.get("can_see_reports", False),
  447. "can_see_likes": category_acl.get("can_see_posts_likes", 0),
  448. "can_like": False,
  449. }
  450. )
  451. if not post.acl["can_see_hidden"]:
  452. post.acl["can_see_hidden"] = post.id == post.thread.first_post_id
  453. if user_acl["is_authenticated"] and post.acl["can_see_likes"]:
  454. post.acl["can_like"] = category_acl.get("can_like_posts", False)
  455. def register_with(registry):
  456. registry.acl_annotator(Category, add_acl_to_category)
  457. registry.acl_annotator(Thread, add_acl_to_thread)
  458. registry.acl_annotator(Post, add_acl_to_post)
  459. def allow_see_thread(user_acl, target):
  460. category_acl = user_acl["categories"].get(
  461. target.category_id, {"can_see": False, "can_browse": False}
  462. )
  463. if not (category_acl["can_see"] and category_acl["can_browse"]):
  464. raise Http404()
  465. if target.is_hidden and (
  466. user_acl["is_anonymous"] or not category_acl["can_hide_threads"]
  467. ):
  468. raise Http404()
  469. if user_acl["is_anonymous"] or user_acl["user_id"] != target.starter_id:
  470. if not category_acl["can_see_all_threads"]:
  471. raise Http404()
  472. if target.is_unapproved and not category_acl["can_approve_content"]:
  473. raise Http404()
  474. can_see_thread = return_boolean(allow_see_thread)
  475. def allow_start_thread(user_acl, target):
  476. if user_acl["is_anonymous"]:
  477. raise PermissionDenied(_("You have to sign in to start threads."))
  478. category_acl = user_acl["categories"].get(target.pk, {"can_start_threads": False})
  479. if not category_acl["can_start_threads"]:
  480. raise PermissionDenied(
  481. _("You don't have permission to start new threads in this category.")
  482. )
  483. if target.is_closed and not category_acl["can_close_threads"]:
  484. raise PermissionDenied(
  485. _("This category is closed. You can't start new threads in it.")
  486. )
  487. can_start_thread = return_boolean(allow_start_thread)
  488. def allow_reply_thread(user_acl, target):
  489. if user_acl["is_anonymous"]:
  490. raise PermissionDenied(_("You have to sign in to reply threads."))
  491. category_acl = user_acl["categories"].get(
  492. target.category_id, {"can_reply_threads": False}
  493. )
  494. if not category_acl["can_reply_threads"]:
  495. raise PermissionDenied(_("You can't reply to threads in this category."))
  496. if not category_acl["can_close_threads"]:
  497. if target.category.is_closed:
  498. raise PermissionDenied(
  499. _("This category is closed. You can't reply to threads in it.")
  500. )
  501. if target.is_closed:
  502. raise PermissionDenied(
  503. _("You can't reply to closed threads in this category.")
  504. )
  505. can_reply_thread = return_boolean(allow_reply_thread)
  506. def allow_edit_thread(user_acl, target):
  507. if user_acl["is_anonymous"]:
  508. raise PermissionDenied(_("You have to sign in to edit threads."))
  509. category_acl = user_acl["categories"].get(
  510. target.category_id, {"can_edit_threads": False}
  511. )
  512. if not category_acl["can_edit_threads"]:
  513. raise PermissionDenied(_("You can't edit threads in this category."))
  514. if category_acl["can_edit_threads"] == 1:
  515. if user_acl["user_id"] != target.starter_id:
  516. raise PermissionDenied(
  517. _("You can't edit other users threads in this category.")
  518. )
  519. if not has_time_to_edit_thread(user_acl, target):
  520. message = ngettext(
  521. "You can't edit threads that are older than %(minutes)s minute.",
  522. "You can't edit threads that are older than %(minutes)s minutes.",
  523. category_acl["thread_edit_time"],
  524. )
  525. raise PermissionDenied(
  526. message % {"minutes": category_acl["thread_edit_time"]}
  527. )
  528. if not category_acl["can_close_threads"]:
  529. if target.category.is_closed:
  530. raise PermissionDenied(
  531. _("This category is closed. You can't edit threads in it.")
  532. )
  533. if target.is_closed:
  534. raise PermissionDenied(_("This thread is closed. You can't edit it."))
  535. can_edit_thread = return_boolean(allow_edit_thread)
  536. def allow_pin_thread(user_acl, target):
  537. if user_acl["is_anonymous"]:
  538. raise PermissionDenied(_("You have to sign in to change threads weights."))
  539. category_acl = user_acl["categories"].get(
  540. target.category_id, {"can_pin_threads": 0}
  541. )
  542. if not category_acl["can_pin_threads"]:
  543. raise PermissionDenied(_("You can't change threads weights in this category."))
  544. if not category_acl["can_close_threads"]:
  545. if target.category.is_closed:
  546. raise PermissionDenied(
  547. _("This category is closed. You can't change threads weights in it.")
  548. )
  549. if target.is_closed:
  550. raise PermissionDenied(
  551. _("This thread is closed. You can't change its weight.")
  552. )
  553. can_pin_thread = return_boolean(allow_pin_thread)
  554. def allow_unhide_thread(user_acl, target):
  555. if user_acl["is_anonymous"]:
  556. raise PermissionDenied(_("You have to sign in to hide threads."))
  557. category_acl = user_acl["categories"].get(
  558. target.category_id, {"can_close_threads": False}
  559. )
  560. if not category_acl["can_close_threads"]:
  561. if target.category.is_closed:
  562. raise PermissionDenied(
  563. _("This category is closed. You can't reveal threads in it.")
  564. )
  565. if target.is_closed:
  566. raise PermissionDenied(_("This thread is closed. You can't reveal it."))
  567. can_unhide_thread = return_boolean(allow_unhide_thread)
  568. def allow_hide_thread(user_acl, target):
  569. if user_acl["is_anonymous"]:
  570. raise PermissionDenied(_("You have to sign in to hide threads."))
  571. category_acl = user_acl["categories"].get(
  572. target.category_id, {"can_hide_threads": 0, "can_hide_own_threads": 0}
  573. )
  574. if (
  575. not category_acl["can_hide_threads"]
  576. and not category_acl["can_hide_own_threads"]
  577. ):
  578. raise PermissionDenied(_("You can't hide threads in this category."))
  579. if not category_acl["can_hide_threads"] and category_acl["can_hide_own_threads"]:
  580. if user_acl["user_id"] != target.starter_id:
  581. raise PermissionDenied(
  582. _("You can't hide other users theads in this category.")
  583. )
  584. if not has_time_to_edit_thread(user_acl, target):
  585. message = ngettext(
  586. "You can't hide threads that are older than %(minutes)s minute.",
  587. "You can't hide threads that are older than %(minutes)s minutes.",
  588. category_acl["thread_edit_time"],
  589. )
  590. raise PermissionDenied(
  591. message % {"minutes": category_acl["thread_edit_time"]}
  592. )
  593. if not category_acl["can_close_threads"]:
  594. if target.category.is_closed:
  595. raise PermissionDenied(
  596. _("This category is closed. You can't hide threads in it.")
  597. )
  598. if target.is_closed:
  599. raise PermissionDenied(_("This thread is closed. You can't hide it."))
  600. can_hide_thread = return_boolean(allow_hide_thread)
  601. def allow_delete_thread(user_acl, target):
  602. if user_acl["is_anonymous"]:
  603. raise PermissionDenied(_("You have to sign in to delete threads."))
  604. category_acl = user_acl["categories"].get(
  605. target.category_id, {"can_hide_threads": 0, "can_hide_own_threads": 0}
  606. )
  607. if (
  608. category_acl["can_hide_threads"] != 2
  609. and category_acl["can_hide_own_threads"] != 2
  610. ):
  611. raise PermissionDenied(_("You can't delete threads in this category."))
  612. if (
  613. category_acl["can_hide_threads"] != 2
  614. and category_acl["can_hide_own_threads"] == 2
  615. ):
  616. if user_acl["user_id"] != target.starter_id:
  617. raise PermissionDenied(
  618. _("You can't delete other users theads in this category.")
  619. )
  620. if not has_time_to_edit_thread(user_acl, target):
  621. message = ngettext(
  622. "You can't delete threads that are older than %(minutes)s minute.",
  623. "You can't delete threads that are older than %(minutes)s minutes.",
  624. category_acl["thread_edit_time"],
  625. )
  626. raise PermissionDenied(
  627. message % {"minutes": category_acl["thread_edit_time"]}
  628. )
  629. if not category_acl["can_close_threads"]:
  630. if target.category.is_closed:
  631. raise PermissionDenied(
  632. _("This category is closed. You can't delete threads in it.")
  633. )
  634. if target.is_closed:
  635. raise PermissionDenied(_("This thread is closed. You can't delete it."))
  636. can_delete_thread = return_boolean(allow_delete_thread)
  637. def allow_move_thread(user_acl, target):
  638. if user_acl["is_anonymous"]:
  639. raise PermissionDenied(_("You have to sign in to move threads."))
  640. category_acl = user_acl["categories"].get(
  641. target.category_id, {"can_move_threads": 0}
  642. )
  643. if not category_acl["can_move_threads"]:
  644. raise PermissionDenied(_("You can't move threads in this category."))
  645. if not category_acl["can_close_threads"]:
  646. if target.category.is_closed:
  647. raise PermissionDenied(
  648. _("This category is closed. You can't move it's threads.")
  649. )
  650. if target.is_closed:
  651. raise PermissionDenied(_("This thread is closed. You can't move it."))
  652. can_move_thread = return_boolean(allow_move_thread)
  653. def allow_merge_thread(user_acl, target, otherthread=False):
  654. if user_acl["is_anonymous"]:
  655. raise PermissionDenied(_("You have to sign in to merge threads."))
  656. category_acl = user_acl["categories"].get(
  657. target.category_id, {"can_merge_threads": 0}
  658. )
  659. if not category_acl["can_merge_threads"]:
  660. if otherthread:
  661. raise PermissionDenied(_("Other thread can't be merged with."))
  662. raise PermissionDenied(_("You can't merge threads in this category."))
  663. if not category_acl["can_close_threads"]:
  664. if target.category.is_closed:
  665. if otherthread:
  666. raise PermissionDenied(
  667. _("Other thread's category is closed. You can't merge with it.")
  668. )
  669. raise PermissionDenied(
  670. _("This category is closed. You can't merge it's threads.")
  671. )
  672. if target.is_closed:
  673. if otherthread:
  674. raise PermissionDenied(
  675. _("Other thread is closed and can't be merged with.")
  676. )
  677. raise PermissionDenied(
  678. _("This thread is closed. You can't merge it with other threads.")
  679. )
  680. can_merge_thread = return_boolean(allow_merge_thread)
  681. def allow_approve_thread(user_acl, target):
  682. if user_acl["is_anonymous"]:
  683. raise PermissionDenied(_("You have to sign in to approve threads."))
  684. category_acl = user_acl["categories"].get(
  685. target.category_id, {"can_approve_content": 0}
  686. )
  687. if not category_acl["can_approve_content"]:
  688. raise PermissionDenied(_("You can't approve threads in this category."))
  689. if not category_acl["can_close_threads"]:
  690. if target.category.is_closed:
  691. raise PermissionDenied(
  692. _("This category is closed. You can't approve threads in it.")
  693. )
  694. if target.is_closed:
  695. raise PermissionDenied(_("This thread is closed. You can't approve it."))
  696. can_approve_thread = return_boolean(allow_approve_thread)
  697. def allow_see_post(user_acl, target):
  698. category_acl = user_acl["categories"].get(
  699. target.category_id, {"can_approve_content": False, "can_hide_events": False}
  700. )
  701. if not target.is_event and target.is_unapproved:
  702. if user_acl["is_anonymous"]:
  703. raise Http404()
  704. if (
  705. not category_acl["can_approve_content"]
  706. and user_acl["user_id"] != target.poster_id
  707. ):
  708. raise Http404()
  709. if target.is_event and target.is_hidden and not category_acl["can_hide_events"]:
  710. raise Http404()
  711. can_see_post = return_boolean(allow_see_post)
  712. def allow_edit_post(user_acl, target):
  713. if user_acl["is_anonymous"]:
  714. raise PermissionDenied(_("You have to sign in to edit posts."))
  715. if target.is_event:
  716. raise PermissionDenied(_("Events can't be edited."))
  717. category_acl = user_acl["categories"].get(
  718. target.category_id, {"can_edit_posts": False}
  719. )
  720. if not category_acl["can_edit_posts"]:
  721. raise PermissionDenied(_("You can't edit posts in this category."))
  722. if (
  723. target.is_hidden
  724. and not target.is_first_post
  725. and not category_acl["can_hide_posts"]
  726. ):
  727. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  728. if category_acl["can_edit_posts"] == 1:
  729. if target.poster_id != user_acl["user_id"]:
  730. raise PermissionDenied(
  731. _("You can't edit other users posts in this category.")
  732. )
  733. if target.is_protected and not category_acl["can_protect_posts"]:
  734. raise PermissionDenied(_("This post is protected. You can't edit it."))
  735. if not has_time_to_edit_post(user_acl, target):
  736. message = ngettext(
  737. "You can't edit posts that are older than %(minutes)s minute.",
  738. "You can't edit posts that are older than %(minutes)s minutes.",
  739. category_acl["post_edit_time"],
  740. )
  741. raise PermissionDenied(
  742. message % {"minutes": category_acl["post_edit_time"]}
  743. )
  744. if not category_acl["can_close_threads"]:
  745. if target.category.is_closed:
  746. raise PermissionDenied(
  747. _("This category is closed. You can't edit posts in it.")
  748. )
  749. if target.thread.is_closed:
  750. raise PermissionDenied(
  751. _("This thread is closed. You can't edit posts in it.")
  752. )
  753. can_edit_post = return_boolean(allow_edit_post)
  754. def allow_unhide_post(user_acl, target):
  755. if user_acl["is_anonymous"]:
  756. raise PermissionDenied(_("You have to sign in to reveal posts."))
  757. category_acl = user_acl["categories"].get(
  758. target.category_id, {"can_hide_posts": 0, "can_hide_own_posts": 0}
  759. )
  760. if not category_acl["can_hide_posts"]:
  761. if not category_acl["can_hide_own_posts"]:
  762. raise PermissionDenied(_("You can't reveal posts in this category."))
  763. if user_acl["user_id"] != target.poster_id:
  764. raise PermissionDenied(
  765. _("You can't reveal other users posts in this category.")
  766. )
  767. if target.is_protected and not category_acl["can_protect_posts"]:
  768. raise PermissionDenied(_("This post is protected. You can't reveal it."))
  769. if not has_time_to_edit_post(user_acl, target):
  770. message = ngettext(
  771. "You can't reveal posts that are older than %(minutes)s minute.",
  772. "You can't reveal posts that are older than %(minutes)s minutes.",
  773. category_acl["post_edit_time"],
  774. )
  775. raise PermissionDenied(
  776. message % {"minutes": category_acl["post_edit_time"]}
  777. )
  778. if target.is_first_post:
  779. raise PermissionDenied(_("You can't reveal thread's first post."))
  780. if not category_acl["can_close_threads"]:
  781. if target.category.is_closed:
  782. raise PermissionDenied(
  783. _("This category is closed. You can't reveal posts in it.")
  784. )
  785. if target.thread.is_closed:
  786. raise PermissionDenied(
  787. _("This thread is closed. You can't reveal posts in it.")
  788. )
  789. can_unhide_post = return_boolean(allow_unhide_post)
  790. def allow_hide_post(user_acl, target):
  791. if user_acl["is_anonymous"]:
  792. raise PermissionDenied(_("You have to sign in to hide posts."))
  793. category_acl = user_acl["categories"].get(
  794. target.category_id, {"can_hide_posts": 0, "can_hide_own_posts": 0}
  795. )
  796. if not category_acl["can_hide_posts"]:
  797. if not category_acl["can_hide_own_posts"]:
  798. raise PermissionDenied(_("You can't hide posts in this category."))
  799. if user_acl["user_id"] != target.poster_id:
  800. raise PermissionDenied(
  801. _("You can't hide other users posts in this category.")
  802. )
  803. if target.is_protected and not category_acl["can_protect_posts"]:
  804. raise PermissionDenied(_("This post is protected. You can't hide it."))
  805. if not has_time_to_edit_post(user_acl, target):
  806. message = ngettext(
  807. "You can't hide posts that are older than %(minutes)s minute.",
  808. "You can't hide posts that are older than %(minutes)s minutes.",
  809. category_acl["post_edit_time"],
  810. )
  811. raise PermissionDenied(
  812. message % {"minutes": category_acl["post_edit_time"]}
  813. )
  814. if target.is_first_post:
  815. raise PermissionDenied(_("You can't hide thread's first post."))
  816. if not category_acl["can_close_threads"]:
  817. if target.category.is_closed:
  818. raise PermissionDenied(
  819. _("This category is closed. You can't hide posts in it.")
  820. )
  821. if target.thread.is_closed:
  822. raise PermissionDenied(
  823. _("This thread is closed. You can't hide posts in it.")
  824. )
  825. can_hide_post = return_boolean(allow_hide_post)
  826. def allow_delete_post(user_acl, target):
  827. if user_acl["is_anonymous"]:
  828. raise PermissionDenied(_("You have to sign in to delete posts."))
  829. category_acl = user_acl["categories"].get(
  830. target.category_id, {"can_hide_posts": 0, "can_hide_own_posts": 0}
  831. )
  832. if category_acl["can_hide_posts"] != 2:
  833. if category_acl["can_hide_own_posts"] != 2:
  834. raise PermissionDenied(_("You can't delete posts in this category."))
  835. if user_acl["user_id"] != target.poster_id:
  836. raise PermissionDenied(
  837. _("You can't delete other users posts in this category.")
  838. )
  839. if target.is_protected and not category_acl["can_protect_posts"]:
  840. raise PermissionDenied(_("This post is protected. You can't delete it."))
  841. if not has_time_to_edit_post(user_acl, target):
  842. message = ngettext(
  843. "You can't delete posts that are older than %(minutes)s minute.",
  844. "You can't delete posts that are older than %(minutes)s minutes.",
  845. category_acl["post_edit_time"],
  846. )
  847. raise PermissionDenied(
  848. message % {"minutes": category_acl["post_edit_time"]}
  849. )
  850. if target.is_first_post:
  851. raise PermissionDenied(_("You can't delete thread's first post."))
  852. if not category_acl["can_close_threads"]:
  853. if target.category.is_closed:
  854. raise PermissionDenied(
  855. _("This category is closed. You can't delete posts in it.")
  856. )
  857. if target.thread.is_closed:
  858. raise PermissionDenied(
  859. _("This thread is closed. You can't delete posts in it.")
  860. )
  861. can_delete_post = return_boolean(allow_delete_post)
  862. def allow_protect_post(user_acl, target):
  863. if user_acl["is_anonymous"]:
  864. raise PermissionDenied(_("You have to sign in to protect posts."))
  865. category_acl = user_acl["categories"].get(
  866. target.category_id, {"can_protect_posts": False}
  867. )
  868. if not category_acl["can_protect_posts"]:
  869. raise PermissionDenied(_("You can't protect posts in this category."))
  870. if not can_edit_post(user_acl, target):
  871. raise PermissionDenied(_("You can't protect posts you can't edit."))
  872. can_protect_post = return_boolean(allow_protect_post)
  873. def allow_approve_post(user_acl, target):
  874. if user_acl["is_anonymous"]:
  875. raise PermissionDenied(_("You have to sign in to approve posts."))
  876. category_acl = user_acl["categories"].get(
  877. target.category_id, {"can_approve_content": False}
  878. )
  879. if not category_acl["can_approve_content"]:
  880. raise PermissionDenied(_("You can't approve posts in this category."))
  881. if target.is_first_post:
  882. raise PermissionDenied(_("You can't approve thread's first post."))
  883. if (
  884. not target.is_first_post
  885. and not category_acl["can_hide_posts"]
  886. and target.is_hidden
  887. ):
  888. raise PermissionDenied(_("You can't approve posts the content you can't see."))
  889. if not category_acl["can_close_threads"]:
  890. if target.category.is_closed:
  891. raise PermissionDenied(
  892. _("This category is closed. You can't approve posts in it.")
  893. )
  894. if target.thread.is_closed:
  895. raise PermissionDenied(
  896. _("This thread is closed. You can't approve posts in it.")
  897. )
  898. can_approve_post = return_boolean(allow_approve_post)
  899. def allow_move_post(user_acl, target):
  900. if user_acl["is_anonymous"]:
  901. raise PermissionDenied(_("You have to sign in to move posts."))
  902. category_acl = user_acl["categories"].get(
  903. target.category_id, {"can_move_posts": False}
  904. )
  905. if not category_acl["can_move_posts"]:
  906. raise PermissionDenied(_("You can't move posts in this category."))
  907. if target.is_event:
  908. raise PermissionDenied(_("Events can't be moved."))
  909. if target.is_first_post:
  910. raise PermissionDenied(_("You can't move thread's first post."))
  911. if not category_acl["can_hide_posts"] and target.is_hidden:
  912. raise PermissionDenied(_("You can't move posts the content you can't see."))
  913. if not category_acl["can_close_threads"]:
  914. if target.category.is_closed:
  915. raise PermissionDenied(
  916. _("This category is closed. You can't move posts in it.")
  917. )
  918. if target.thread.is_closed:
  919. raise PermissionDenied(
  920. _("This thread is closed. You can't move posts in it.")
  921. )
  922. can_move_post = return_boolean(allow_move_post)
  923. def allow_merge_post(user_acl, target):
  924. if user_acl["is_anonymous"]:
  925. raise PermissionDenied(_("You have to sign in to merge posts."))
  926. category_acl = user_acl["categories"].get(
  927. target.category_id, {"can_merge_posts": False}
  928. )
  929. if not category_acl["can_merge_posts"]:
  930. raise PermissionDenied(_("You can't merge posts in this category."))
  931. if target.is_event:
  932. raise PermissionDenied(_("Events can't be merged."))
  933. if (
  934. target.is_hidden
  935. and not category_acl["can_hide_posts"]
  936. and not target.is_first_post
  937. ):
  938. raise PermissionDenied(_("You can't merge posts the content you can't see."))
  939. if not category_acl["can_close_threads"]:
  940. if target.category.is_closed:
  941. raise PermissionDenied(
  942. _("This category is closed. You can't merge posts in it.")
  943. )
  944. if target.thread.is_closed:
  945. raise PermissionDenied(
  946. _("This thread is closed. You can't merge posts in it.")
  947. )
  948. can_merge_post = return_boolean(allow_merge_post)
  949. def allow_split_post(user_acl, target):
  950. if user_acl["is_anonymous"]:
  951. raise PermissionDenied(_("You have to sign in to split posts."))
  952. category_acl = user_acl["categories"].get(
  953. target.category_id, {"can_move_posts": False}
  954. )
  955. if not category_acl["can_move_posts"]:
  956. raise PermissionDenied(_("You can't split posts in this category."))
  957. if target.is_event:
  958. raise PermissionDenied(_("Events can't be split."))
  959. if target.is_first_post:
  960. raise PermissionDenied(_("You can't split thread's first post."))
  961. if not category_acl["can_hide_posts"] and target.is_hidden:
  962. raise PermissionDenied(_("You can't split posts the content you can't see."))
  963. if not category_acl["can_close_threads"]:
  964. if target.category.is_closed:
  965. raise PermissionDenied(
  966. _("This category is closed. You can't split posts in it.")
  967. )
  968. if target.thread.is_closed:
  969. raise PermissionDenied(
  970. _("This thread is closed. You can't split posts in it.")
  971. )
  972. can_split_post = return_boolean(allow_split_post)
  973. def allow_unhide_event(user_acl, target):
  974. if user_acl["is_anonymous"]:
  975. raise PermissionDenied(_("You have to sign in to reveal events."))
  976. category_acl = user_acl["categories"].get(
  977. target.category_id, {"can_hide_events": 0}
  978. )
  979. if not category_acl["can_hide_events"]:
  980. raise PermissionDenied(_("You can't reveal events in this category."))
  981. if not category_acl["can_close_threads"]:
  982. if target.category.is_closed:
  983. raise PermissionDenied(
  984. _("This category is closed. You can't reveal events in it.")
  985. )
  986. if target.thread.is_closed:
  987. raise PermissionDenied(
  988. _("This thread is closed. You can't reveal events in it.")
  989. )
  990. can_unhide_event = return_boolean(allow_unhide_event)
  991. def allow_hide_event(user_acl, target):
  992. if user_acl["is_anonymous"]:
  993. raise PermissionDenied(_("You have to sign in to hide events."))
  994. category_acl = user_acl["categories"].get(
  995. target.category_id, {"can_hide_events": 0}
  996. )
  997. if not category_acl["can_hide_events"]:
  998. raise PermissionDenied(_("You can't hide events in this category."))
  999. if not category_acl["can_close_threads"]:
  1000. if target.category.is_closed:
  1001. raise PermissionDenied(
  1002. _("This category is closed. You can't hide events in it.")
  1003. )
  1004. if target.thread.is_closed:
  1005. raise PermissionDenied(
  1006. _("This thread is closed. You can't hide events in it.")
  1007. )
  1008. can_hide_event = return_boolean(allow_hide_event)
  1009. def allow_delete_event(user_acl, target):
  1010. if user_acl["is_anonymous"]:
  1011. raise PermissionDenied(_("You have to sign in to delete events."))
  1012. category_acl = user_acl["categories"].get(
  1013. target.category_id, {"can_hide_events": 0}
  1014. )
  1015. if category_acl["can_hide_events"] != 2:
  1016. raise PermissionDenied(_("You can't delete events in this category."))
  1017. if not category_acl["can_close_threads"]:
  1018. if target.category.is_closed:
  1019. raise PermissionDenied(
  1020. _("This category is closed. You can't delete events in it.")
  1021. )
  1022. if target.thread.is_closed:
  1023. raise PermissionDenied(
  1024. _("This thread is closed. You can't delete events in it.")
  1025. )
  1026. can_delete_event = return_boolean(allow_delete_event)
  1027. def can_change_owned_thread(user_acl, target):
  1028. if user_acl["is_anonymous"] or user_acl["user_id"] != target.starter_id:
  1029. return False
  1030. if target.category.is_closed or target.is_closed:
  1031. return False
  1032. return has_time_to_edit_thread(user_acl, target)
  1033. def has_time_to_edit_thread(user_acl, target):
  1034. edit_time = (
  1035. user_acl["categories"].get(target.category_id, {}).get("thread_edit_time", 0)
  1036. )
  1037. if edit_time:
  1038. diff = timezone.now() - target.started_on
  1039. diff_minutes = int(diff.total_seconds() / 60)
  1040. return diff_minutes < edit_time
  1041. else:
  1042. return True
  1043. def has_time_to_edit_post(user_acl, target):
  1044. edit_time = (
  1045. user_acl["categories"].get(target.category_id, {}).get("post_edit_time", 0)
  1046. )
  1047. if edit_time:
  1048. diff = timezone.now() - target.posted_on
  1049. diff_minutes = int(diff.total_seconds() / 60)
  1050. return diff_minutes < edit_time
  1051. else:
  1052. return True
  1053. def exclude_invisible_threads(user_acl, categories, queryset):
  1054. show_all = []
  1055. show_accepted_visible = []
  1056. show_accepted = []
  1057. show_visible = []
  1058. show_owned = []
  1059. show_owned_visible = []
  1060. for category in categories:
  1061. add_acl_to_obj(user_acl, category)
  1062. if not (category.acl["can_see"] and category.acl["can_browse"]):
  1063. continue
  1064. can_hide = category.acl["can_hide_threads"]
  1065. if category.acl["can_see_all_threads"]:
  1066. can_mod = category.acl["can_approve_content"]
  1067. if can_mod and can_hide:
  1068. show_all.append(category)
  1069. elif user_acl["is_authenticated"]:
  1070. if not can_mod and not can_hide:
  1071. show_accepted_visible.append(category)
  1072. elif not can_mod:
  1073. show_accepted.append(category)
  1074. elif not can_hide:
  1075. show_visible.append(category)
  1076. else:
  1077. show_accepted_visible.append(category)
  1078. elif user_acl["is_authenticated"]:
  1079. if can_hide:
  1080. show_owned.append(category)
  1081. else:
  1082. show_owned_visible.append(category)
  1083. conditions = None
  1084. if show_all:
  1085. conditions = Q(category__in=show_all)
  1086. if show_accepted_visible:
  1087. if user_acl["is_authenticated"]:
  1088. condition = Q(
  1089. Q(starter_id=user_acl["user_id"]) | Q(is_unapproved=False),
  1090. category__in=show_accepted_visible,
  1091. is_hidden=False,
  1092. )
  1093. else:
  1094. condition = Q(
  1095. category__in=show_accepted_visible, is_hidden=False, is_unapproved=False
  1096. )
  1097. if conditions:
  1098. conditions = conditions | condition
  1099. else:
  1100. conditions = condition
  1101. if show_accepted:
  1102. condition = Q(
  1103. Q(starter_id=user_acl["user_id"]) | Q(is_unapproved=False),
  1104. category__in=show_accepted,
  1105. )
  1106. if conditions:
  1107. conditions = conditions | condition
  1108. else:
  1109. conditions = condition
  1110. if show_visible:
  1111. condition = Q(category__in=show_visible, is_hidden=False)
  1112. if conditions:
  1113. conditions = conditions | condition
  1114. else:
  1115. conditions = condition
  1116. if show_owned:
  1117. condition = Q(category__in=show_owned, starter_id=user_acl["user_id"])
  1118. if conditions:
  1119. conditions = conditions | condition
  1120. else:
  1121. conditions = condition
  1122. if show_owned_visible:
  1123. condition = Q(
  1124. category__in=show_owned_visible,
  1125. starter_id=user_acl["user_id"],
  1126. is_hidden=False,
  1127. )
  1128. if conditions:
  1129. conditions = conditions | condition
  1130. else:
  1131. conditions = condition
  1132. if conditions:
  1133. return queryset.filter(conditions)
  1134. else:
  1135. return Thread.objects.none()
  1136. def exclude_invisible_posts(user_acl, categories, queryset):
  1137. if hasattr(categories, "__iter__"):
  1138. return exclude_invisible_posts_in_categories(user_acl, categories, queryset)
  1139. else:
  1140. return exclude_invisible_posts_in_category(user_acl, categories, queryset)
  1141. def exclude_invisible_posts_in_categories(user_acl, categories, queryset):
  1142. show_all = []
  1143. show_approved = []
  1144. show_approved_owned = []
  1145. hide_invisible_events = []
  1146. for category in categories:
  1147. add_acl_to_obj(user_acl, category)
  1148. if category.acl["can_approve_content"]:
  1149. show_all.append(category.pk)
  1150. else:
  1151. if user_acl["is_authenticated"]:
  1152. show_approved_owned.append(category.pk)
  1153. else:
  1154. show_approved.append(category.pk)
  1155. if not category.acl["can_hide_events"]:
  1156. hide_invisible_events.append(category.pk)
  1157. conditions = None
  1158. if show_all:
  1159. conditions = Q(category__in=show_all)
  1160. if show_approved:
  1161. condition = Q(category__in=show_approved, is_unapproved=False)
  1162. if conditions:
  1163. conditions = conditions | condition
  1164. else:
  1165. conditions = condition
  1166. if show_approved_owned:
  1167. condition = Q(
  1168. Q(poster_id=user_acl["user_id"]) | Q(is_unapproved=False),
  1169. category__in=show_approved_owned,
  1170. )
  1171. if conditions:
  1172. conditions = conditions | condition
  1173. else:
  1174. conditions = condition
  1175. if hide_invisible_events:
  1176. queryset = queryset.exclude(
  1177. category__in=hide_invisible_events, is_event=True, is_hidden=True
  1178. )
  1179. if conditions:
  1180. return queryset.filter(conditions)
  1181. else:
  1182. return Post.objects.none()
  1183. def exclude_invisible_posts_in_category(user_acl, category, queryset):
  1184. add_acl_to_obj(user_acl, category)
  1185. if not category.acl["can_approve_content"]:
  1186. if user_acl["is_authenticated"]:
  1187. queryset = queryset.filter(
  1188. Q(is_unapproved=False) | Q(poster_id=user_acl["user_id"])
  1189. )
  1190. else:
  1191. queryset = queryset.exclude(is_unapproved=True)
  1192. if not category.acl["can_hide_events"]:
  1193. queryset = queryset.exclude(is_event=True, is_hidden=True)
  1194. return queryset