patch.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. from django.core.exceptions import PermissionDenied
  2. from django.utils.translation import gettext as _
  3. from misago.acl import add_acl
  4. from misago.categories.models import Category
  5. from misago.categories.permissions import (
  6. allow_see_category, allow_browse_category)
  7. from misago.categories.serializers import CategorySerializer
  8. from misago.core.apipatch import ApiPatch
  9. from misago.core.shortcuts import get_int_or_404, get_object_or_404
  10. from misago.threads.moderation import threads as moderation
  11. from misago.threads.permissions import allow_start_thread
  12. from misago.threads.utils import add_categories_to_threads
  13. thread_patch_endpoint = ApiPatch()
  14. def patch_weight(request, thread, value):
  15. message = _("You don't have permission to change this thread's weight.")
  16. if not thread.acl.get('can_pin'):
  17. raise PermissionDenied(message)
  18. elif thread.weight > thread.acl.get('can_pin'):
  19. raise PermissionDenied(message)
  20. if value == 2:
  21. if thread.acl.get('can_pin') == 2:
  22. moderation.pin_thread_globally(request, thread)
  23. else:
  24. raise PermissionDenied(
  25. _("You don't have permission to pin this thread globally."))
  26. elif value == 1:
  27. moderation.pin_thread_locally(request, thread)
  28. elif value == 0:
  29. moderation.unpin_thread(request, thread)
  30. return {'weight': thread.weight}
  31. thread_patch_endpoint.replace('weight', patch_weight)
  32. def patch_move(request, thread, value):
  33. if thread.acl.get('can_move'):
  34. category_pk = get_int_or_404(value)
  35. new_category = get_object_or_404(
  36. Category.objects.all_categories().select_related('parent'),
  37. pk=category_pk
  38. )
  39. add_acl(request.user, new_category)
  40. allow_see_category(request.user, new_category)
  41. allow_browse_category(request.user, new_category)
  42. allow_start_thread(request.user, new_category)
  43. moderation.move_thread(request, thread, new_category)
  44. return {'category': CategorySerializer(new_category).data}
  45. else:
  46. raise PermissionDenied(
  47. _("You don't have permission to move this thread."))
  48. thread_patch_endpoint.replace('category', patch_move)
  49. def patch_top_category(request, thread, value):
  50. category_pk = get_int_or_404(value)
  51. root_category = get_object_or_404(
  52. Category.objects.all_categories(include_root=True),
  53. pk=category_pk
  54. )
  55. categories = list(Category.objects.all_categories().filter(
  56. id__in=request.user.acl['visible_categories']
  57. ))
  58. add_categories_to_threads(root_category, categories, [thread])
  59. return {'top_category': CategorySerializer(thread.top_category).data}
  60. thread_patch_endpoint.add('top-category', patch_top_category)
  61. def patch_flatten_categories(request, thread, value):
  62. try:
  63. return {
  64. 'category': thread.category_id,
  65. 'top_category': thread.top_category.pk,
  66. }
  67. except AttributeError as e:
  68. return {
  69. 'category': thread.category_id,
  70. 'top_category': None
  71. }
  72. thread_patch_endpoint.replace('flatten-categories', patch_flatten_categories)
  73. def patch_is_unapproved(request, thread, value):
  74. if thread.acl.get('can_approve'):
  75. if value:
  76. raise PermissionDenied(_("Content approval can't be reversed."))
  77. moderation.approve_thread(request, thread)
  78. return {
  79. 'is_unapproved': thread.is_unapproved,
  80. 'has_unapproved_posts': thread.has_unapproved_posts,
  81. }
  82. else:
  83. raise PermissionDenied(
  84. _("You don't have permission to approve this thread."))
  85. thread_patch_endpoint.replace('is-unapproved', patch_is_unapproved)
  86. def patch_is_closed(request, thread, value):
  87. if thread.acl.get('can_close'):
  88. if value:
  89. moderation.close_thread(request, thread)
  90. else:
  91. moderation.open_thread(request, thread)
  92. return {'is_closed': thread.is_closed}
  93. else:
  94. if value:
  95. raise PermissionDenied(
  96. _("You don't have permission to close this thread."))
  97. else:
  98. raise PermissionDenied(
  99. _("You don't have permission to open this thread."))
  100. thread_patch_endpoint.replace('is-closed', patch_is_closed)
  101. def patch_is_hidden(request, thread, value):
  102. if thread.acl.get('can_hide'):
  103. if value:
  104. moderation.hide_thread(request, thread)
  105. else:
  106. moderation.unhide_thread(request, thread)
  107. return {'is_hidden': thread.is_hidden}
  108. else:
  109. raise PermissionDenied(
  110. _("You don't have permission to hide this thread."))
  111. thread_patch_endpoint.replace('is-hidden', patch_is_hidden)
  112. def patch_subscribtion(request, thread, value):
  113. request.user.subscription_set.filter(thread=thread).delete()
  114. if value == 'notify':
  115. thread.subscription = request.user.subscription_set.create(
  116. thread=thread,
  117. category=thread.category,
  118. last_read_on=thread.last_post_on,
  119. send_email=False,
  120. )
  121. return {'subscription': False}
  122. elif value == 'email':
  123. thread.subscription = request.user.subscription_set.create(
  124. thread=thread,
  125. category=thread.category,
  126. last_read_on=thread.last_post_on,
  127. send_email=True,
  128. )
  129. return {'subscription': True}
  130. else:
  131. return {'subscription': None}
  132. thread_patch_endpoint.replace('subscription', patch_subscribtion)