patch.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. from django.contrib.auth import get_user_model
  2. from django.core.exceptions import PermissionDenied, ValidationError
  3. from django.shortcuts import get_object_or_404
  4. from django.utils import six
  5. from django.utils.translation import ugettext as _
  6. from misago.acl import add_acl
  7. from misago.categories.models import Category
  8. from misago.categories.permissions import allow_browse_category, allow_see_category
  9. from misago.categories.serializers import CategorySerializer
  10. from misago.core.apipatch import ApiPatch
  11. from misago.core.shortcuts import get_int_or_404
  12. from misago.threads.moderation import threads as moderation
  13. from misago.threads.participants import (
  14. add_participant, change_owner, make_participants_aware, remove_participant)
  15. from misago.threads.permissions import (
  16. allow_add_participant, allow_add_participants, allow_change_owner, allow_edit_thread,
  17. allow_remove_participant, allow_start_thread)
  18. from misago.threads.serializers import ThreadParticipantSerializer
  19. from misago.threads.validators import validate_title
  20. UserModel = get_user_model()
  21. thread_patch_dispatcher = ApiPatch()
  22. def patch_acl(request, thread, value):
  23. """useful little op that updates thread acl to current state"""
  24. if value:
  25. add_acl(request.user, thread)
  26. return {'acl': thread.acl}
  27. else:
  28. return {'acl': None}
  29. thread_patch_dispatcher.add('acl', patch_acl)
  30. def patch_title(request, thread, value):
  31. try:
  32. value_cleaned = six.text_type(value).strip()
  33. except (TypeError, ValueError):
  34. raise PermissionDenied(_("Invalid thread title."))
  35. try:
  36. validate_title(value_cleaned)
  37. except ValidationError as e:
  38. raise PermissionDenied(e.args[0])
  39. allow_edit_thread(request.user, thread)
  40. moderation.change_thread_title(request, thread, value_cleaned)
  41. return {'title': thread.title}
  42. thread_patch_dispatcher.replace('title', patch_title)
  43. def patch_weight(request, thread, value):
  44. message = _("You don't have permission to change this thread's weight.")
  45. if not thread.acl.get('can_pin'):
  46. raise PermissionDenied(message)
  47. elif thread.weight > thread.acl.get('can_pin'):
  48. raise PermissionDenied(message)
  49. if value == 2:
  50. if thread.acl.get('can_pin') == 2:
  51. moderation.pin_thread_globally(request, thread)
  52. else:
  53. raise PermissionDenied(_("You don't have permission to pin this thread globally."))
  54. elif value == 1:
  55. moderation.pin_thread_locally(request, thread)
  56. elif value == 0:
  57. moderation.unpin_thread(request, thread)
  58. return {'weight': thread.weight}
  59. thread_patch_dispatcher.replace('weight', patch_weight)
  60. def patch_move(request, thread, value):
  61. if not thread.acl.get('can_move'):
  62. raise PermissionDenied(_("You don't have permission to move this thread."))
  63. category_pk = get_int_or_404(value)
  64. new_category = get_object_or_404(
  65. Category.objects.all_categories().select_related('parent'), pk=category_pk
  66. )
  67. add_acl(request.user, new_category)
  68. allow_see_category(request.user, new_category)
  69. allow_browse_category(request.user, new_category)
  70. allow_start_thread(request.user, new_category)
  71. if new_category == thread.category:
  72. raise PermissionDenied(_("You can't move thread to the category it's already in."))
  73. moderation.move_thread(request, thread, new_category)
  74. return {'category': CategorySerializer(new_category).data}
  75. thread_patch_dispatcher.replace('category', patch_move)
  76. def patch_flatten_categories(request, thread, value):
  77. try:
  78. return {'category': thread.category_id}
  79. except AttributeError:
  80. return {'category': thread.category_id}
  81. thread_patch_dispatcher.replace('flatten-categories', patch_flatten_categories)
  82. def patch_is_unapproved(request, thread, value):
  83. if thread.acl.get('can_approve'):
  84. if value:
  85. raise PermissionDenied(_("Content approval can't be reversed."))
  86. moderation.approve_thread(request, thread)
  87. return {
  88. 'is_unapproved': thread.is_unapproved,
  89. 'has_unapproved_posts': thread.has_unapproved_posts,
  90. }
  91. else:
  92. raise PermissionDenied(_("You don't have permission to approve this thread."))
  93. thread_patch_dispatcher.replace('is-unapproved', patch_is_unapproved)
  94. def patch_is_closed(request, thread, value):
  95. if thread.acl.get('can_close'):
  96. if value:
  97. moderation.close_thread(request, thread)
  98. else:
  99. moderation.open_thread(request, thread)
  100. return {'is_closed': thread.is_closed}
  101. else:
  102. if value:
  103. raise PermissionDenied(_("You don't have permission to close this thread."))
  104. else:
  105. raise PermissionDenied(_("You don't have permission to open this thread."))
  106. thread_patch_dispatcher.replace('is-closed', patch_is_closed)
  107. def patch_is_hidden(request, thread, value):
  108. if thread.acl.get('can_hide'):
  109. if value:
  110. moderation.hide_thread(request, thread)
  111. else:
  112. moderation.unhide_thread(request, thread)
  113. return {'is_hidden': thread.is_hidden}
  114. else:
  115. raise PermissionDenied(_("You don't have permission to hide this thread."))
  116. thread_patch_dispatcher.replace('is-hidden', patch_is_hidden)
  117. def patch_subscription(request, thread, value):
  118. request.user.subscription_set.filter(thread=thread).delete()
  119. if value == 'notify':
  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=False,
  125. )
  126. return {'subscription': False}
  127. elif value == 'email':
  128. thread.subscription = request.user.subscription_set.create(
  129. thread=thread,
  130. category=thread.category,
  131. last_read_on=thread.last_post_on,
  132. send_email=True,
  133. )
  134. return {'subscription': True}
  135. else:
  136. return {'subscription': None}
  137. thread_patch_dispatcher.replace('subscription', patch_subscription)
  138. def patch_add_participant(request, thread, value):
  139. allow_add_participants(request.user, thread)
  140. try:
  141. username = six.text_type(value).strip().lower()
  142. if not username:
  143. raise PermissionDenied(_("You have to enter new participant's username."))
  144. participant = UserModel.objects.get(slug=username)
  145. except UserModel.DoesNotExist:
  146. raise PermissionDenied(_("No user with such name exists."))
  147. if participant in [p.user for p in thread.participants_list]:
  148. raise PermissionDenied(_("This user is already thread participant."))
  149. allow_add_participant(request.user, participant)
  150. add_participant(request, thread, participant)
  151. make_participants_aware(request.user, thread)
  152. participants = ThreadParticipantSerializer(thread.participants_list, many=True)
  153. return {'participants': participants.data}
  154. thread_patch_dispatcher.add('participants', patch_add_participant)
  155. def patch_remove_participant(request, thread, value):
  156. try:
  157. user_id = int(value)
  158. except (ValueError, TypeError):
  159. user_id = 0
  160. for participant in thread.participants_list:
  161. if participant.user_id == user_id:
  162. break
  163. else:
  164. raise PermissionDenied(_("Participant doesn't exist."))
  165. allow_remove_participant(request.user, thread, participant.user)
  166. remove_participant(request, thread, participant.user)
  167. if len(thread.participants_list) == 1:
  168. return {'deleted': True}
  169. else:
  170. make_participants_aware(request.user, thread)
  171. participants = ThreadParticipantSerializer(thread.participants_list, many=True)
  172. return {
  173. 'deleted': False,
  174. 'participants': participants.data,
  175. }
  176. thread_patch_dispatcher.remove('participants', patch_remove_participant)
  177. def patch_replace_owner(request, thread, value):
  178. try:
  179. user_id = int(value)
  180. except (ValueError, TypeError):
  181. user_id = 0
  182. for participant in thread.participants_list:
  183. if participant.user_id == user_id:
  184. if participant.is_owner:
  185. raise PermissionDenied(_("This user already is thread owner."))
  186. else:
  187. break
  188. else:
  189. raise PermissionDenied(_("Participant doesn't exist."))
  190. allow_change_owner(request.user, thread)
  191. change_owner(request, thread, participant.user)
  192. make_participants_aware(request.user, thread)
  193. participants = ThreadParticipantSerializer(thread.participants_list, many=True)
  194. return {'participants': participants.data}
  195. thread_patch_dispatcher.replace('owner', patch_replace_owner)
  196. def thread_patch_endpoint(request, thread):
  197. old_title = thread.title
  198. old_is_hidden = thread.is_hidden
  199. old_is_unapproved = thread.is_unapproved
  200. old_category = thread.category
  201. response = thread_patch_dispatcher.dispatch(request, thread)
  202. # diff thread's state against pre-patch and resync category if necessary
  203. hidden_changed = old_is_hidden != thread.is_hidden
  204. unapproved_changed = old_is_unapproved != thread.is_unapproved
  205. category_changed = old_category != thread.category
  206. title_changed = old_title != thread.title
  207. if thread.category.last_thread_id != thread.pk:
  208. title_changed = False # don't trigger resync on simple title change
  209. if hidden_changed or unapproved_changed or category_changed:
  210. thread.category.synchronize()
  211. thread.category.save()
  212. if category_changed:
  213. old_category.synchronize()
  214. old_category.save()
  215. elif title_changed:
  216. thread.category.last_thread_title = thread.title
  217. thread.category.last_thread_slug = thread.slug
  218. thread.category.save(update_fields=['last_thread_title', 'last_thread_slug'])
  219. return response