patch.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_hidden(request, thread, value):
  86. if thread.acl.get('can_hide'):
  87. if value:
  88. moderation.hide_thread(request.user, thread)
  89. else:
  90. moderation.unhide_thread(request.user, thread)
  91. return {'is_hidden': thread.is_hidden}
  92. else:
  93. raise PermissionDenied(
  94. _("You don't have permission to hide this thread."))
  95. thread_patch_endpoint.replace('is-hidden', patch_is_hidden)
  96. def patch_subscribtion(request, thread, value):
  97. request.user.subscription_set.filter(thread=thread).delete()
  98. if value == 'notify':
  99. thread.subscription = request.user.subscription_set.create(
  100. thread=thread,
  101. category=thread.category,
  102. last_read_on=thread.last_post_on,
  103. send_email=False,
  104. )
  105. return {'subscription': False}
  106. elif value == 'email':
  107. thread.subscription = request.user.subscription_set.create(
  108. thread=thread,
  109. category=thread.category,
  110. last_read_on=thread.last_post_on,
  111. send_email=True,
  112. )
  113. return {'subscription': True}
  114. else:
  115. return {'subscription': None}
  116. thread_patch_endpoint.replace('subscription', patch_subscribtion)