patch.py 5.3 KB

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