patch.py 5.3 KB

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