patch.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. from django.core.exceptions import PermissionDenied, ValidationError
  2. from django.utils import six
  3. from django.utils.translation import gettext as _
  4. from misago.acl import add_acl
  5. from misago.categories.models import Category
  6. from misago.categories.permissions import allow_browse_category, allow_see_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 ...moderation import threads as moderation
  11. from ...permissions import allow_start_thread
  12. from ...utils import add_categories_to_threads
  13. from ...validators import validate_title
  14. thread_patch_dispatcher = ApiPatch()
  15. def patch_title(request, thread, value):
  16. try:
  17. value_cleaned = six.text_type(value).strip()
  18. except (TypeError, ValueError):
  19. raise PermissionDenied(_("Invalid thread title."))
  20. try:
  21. validate_title(value_cleaned)
  22. except ValidationError as e:
  23. raise PermissionDenied(e.args[0])
  24. if not thread.acl.get('can_edit'):
  25. raise PermissionDenied(_("You don't have permission to edit this thread."))
  26. moderation.change_thread_title(request, thread, value_cleaned)
  27. return {'title': thread.title}
  28. thread_patch_dispatcher.replace('title', patch_title)
  29. def patch_weight(request, thread, value):
  30. message = _("You don't have permission to change this thread's weight.")
  31. if not thread.acl.get('can_pin'):
  32. raise PermissionDenied(message)
  33. elif thread.weight > thread.acl.get('can_pin'):
  34. raise PermissionDenied(message)
  35. if value == 2:
  36. if thread.acl.get('can_pin') == 2:
  37. moderation.pin_thread_globally(request, thread)
  38. else:
  39. raise PermissionDenied(_("You don't have permission to pin this thread globally."))
  40. elif value == 1:
  41. moderation.pin_thread_locally(request, thread)
  42. elif value == 0:
  43. moderation.unpin_thread(request, thread)
  44. return {'weight': thread.weight}
  45. thread_patch_dispatcher.replace('weight', patch_weight)
  46. def patch_move(request, thread, value):
  47. if thread.acl.get('can_move'):
  48. category_pk = get_int_or_404(value)
  49. new_category = get_object_or_404(
  50. Category.objects.all_categories().select_related('parent'),
  51. pk=category_pk
  52. )
  53. add_acl(request.user, new_category)
  54. allow_see_category(request.user, new_category)
  55. allow_browse_category(request.user, new_category)
  56. allow_start_thread(request.user, new_category)
  57. moderation.move_thread(request, thread, new_category)
  58. return {'category': CategorySerializer(new_category).data}
  59. else:
  60. raise PermissionDenied(_("You don't have permission to move this thread."))
  61. thread_patch_dispatcher.replace('category', patch_move)
  62. def patch_top_category(request, thread, value):
  63. category_pk = get_int_or_404(value)
  64. root_category = get_object_or_404(
  65. Category.objects.all_categories(include_root=True),
  66. pk=category_pk
  67. )
  68. categories = list(Category.objects.all_categories().filter(
  69. id__in=request.user.acl['visible_categories']
  70. ))
  71. add_categories_to_threads(root_category, categories, [thread])
  72. return {'top_category': CategorySerializer(thread.top_category).data}
  73. thread_patch_dispatcher.add('top-category', patch_top_category)
  74. def patch_flatten_categories(request, thread, value):
  75. try:
  76. return {
  77. 'category': thread.category_id,
  78. 'top_category': thread.top_category.pk,
  79. }
  80. except AttributeError as e:
  81. return {
  82. 'category': thread.category_id,
  83. 'top_category': None
  84. }
  85. thread_patch_dispatcher.replace('flatten-categories', patch_flatten_categories)
  86. def patch_is_unapproved(request, thread, value):
  87. if thread.acl.get('can_approve'):
  88. if value:
  89. raise PermissionDenied(_("Content approval can't be reversed."))
  90. moderation.approve_thread(request, thread)
  91. return {
  92. 'is_unapproved': thread.is_unapproved,
  93. 'has_unapproved_posts': thread.has_unapproved_posts,
  94. }
  95. else:
  96. raise PermissionDenied(_("You don't have permission to approve this thread."))
  97. thread_patch_dispatcher.replace('is-unapproved', patch_is_unapproved)
  98. def patch_is_closed(request, thread, value):
  99. if thread.acl.get('can_close'):
  100. if value:
  101. moderation.close_thread(request, thread)
  102. else:
  103. moderation.open_thread(request, thread)
  104. return {'is_closed': thread.is_closed}
  105. else:
  106. if value:
  107. raise PermissionDenied(_("You don't have permission to close this thread."))
  108. else:
  109. raise PermissionDenied(_("You don't have permission to open this thread."))
  110. thread_patch_dispatcher.replace('is-closed', patch_is_closed)
  111. def patch_is_hidden(request, thread, value):
  112. if thread.acl.get('can_hide'):
  113. if value:
  114. moderation.hide_thread(request, thread)
  115. else:
  116. moderation.unhide_thread(request, thread)
  117. return {'is_hidden': thread.is_hidden}
  118. else:
  119. raise PermissionDenied(_("You don't have permission to hide this thread."))
  120. thread_patch_dispatcher.replace('is-hidden', patch_is_hidden)
  121. def patch_subscribtion(request, thread, value):
  122. request.user.subscription_set.filter(thread=thread).delete()
  123. if value == 'notify':
  124. thread.subscription = request.user.subscription_set.create(
  125. thread=thread,
  126. category=thread.category,
  127. last_read_on=thread.last_post_on,
  128. send_email=False,
  129. )
  130. return {'subscription': False}
  131. elif value == 'email':
  132. thread.subscription = request.user.subscription_set.create(
  133. thread=thread,
  134. category=thread.category,
  135. last_read_on=thread.last_post_on,
  136. send_email=True,
  137. )
  138. return {'subscription': True}
  139. else:
  140. return {'subscription': None}
  141. thread_patch_dispatcher.replace('subscription', patch_subscribtion)
  142. def thread_patch_endpoint(request, thread):
  143. old_title = thread.title
  144. old_is_hidden = thread.is_hidden
  145. old_is_unapproved = thread.is_unapproved
  146. old_category = thread.category
  147. response = thread_patch_dispatcher.dispatch(request, thread)
  148. # diff thread's state against pre-patch and resync category if necessary
  149. hidden_changed = old_is_hidden != thread.is_hidden
  150. unapproved_changed = old_is_unapproved != thread.is_unapproved
  151. category_changed = old_category != thread.category
  152. title_changed = old_is_hidden != thread.is_hidden
  153. if thread.category.last_thread_id != thread.pk:
  154. title_changed = False # don't trigger resync on simple title change
  155. if hidden_changed or unapproved_changed or category_changed:
  156. thread.category.synchronize()
  157. thread.category.save()
  158. if category_changed:
  159. old_category.synchronize()
  160. old_category.save()
  161. elif title_changed:
  162. thread.category.last_thread_title = thread.title
  163. thread.category.last_thread_slug = thread.slug
  164. thread.category.save(update_fields=['last_thread_title', 'last_thread_slug'])
  165. return response