patch.py 5.4 KB

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