patch.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. from rest_framework import serializers
  2. from rest_framework.response import Response
  3. from django.contrib.auth import get_user_model
  4. from django.core.exceptions import PermissionDenied, ValidationError
  5. from django.http import Http404
  6. from django.shortcuts import get_object_or_404
  7. from django.utils import six
  8. from django.utils.translation import ugettext as _
  9. from misago.acl import add_acl
  10. from misago.api.patch import ApiPatch
  11. from misago.categories.models import Category
  12. from misago.categories.permissions import allow_browse_category, allow_see_category
  13. from misago.categories.serializers import CategorySerializer
  14. from misago.conf import settings
  15. from misago.core.shortcuts import get_int_or_404
  16. from misago.threads.moderation import threads as moderation
  17. from misago.threads.participants import (
  18. add_participant, change_owner, make_participants_aware, remove_participant)
  19. from misago.threads.permissions import (
  20. allow_add_participant, allow_add_participants, allow_approve_thread, allow_change_owner, allow_edit_thread,
  21. allow_pin_thread, allow_hide_thread, allow_move_thread, allow_remove_participant, allow_start_thread,
  22. allow_unhide_thread)
  23. from misago.threads.serializers import ThreadParticipantSerializer
  24. from misago.threads.validators import validate_title
  25. PATCH_LIMIT = settings.MISAGO_THREADS_PER_PAGE + settings.MISAGO_THREADS_TAIL
  26. UserModel = get_user_model()
  27. thread_patch_dispatcher = ApiPatch()
  28. def patch_acl(request, thread, value):
  29. """useful little op that updates thread acl to current state"""
  30. if value:
  31. add_acl(request.user, thread)
  32. return {'acl': thread.acl}
  33. else:
  34. return {'acl': None}
  35. thread_patch_dispatcher.add('acl', patch_acl)
  36. def patch_title(request, thread, value):
  37. try:
  38. value_cleaned = six.text_type(value).strip()
  39. except (TypeError, ValueError):
  40. raise ValidationError(_("Invalid thread title."))
  41. validate_title(value_cleaned)
  42. allow_edit_thread(request.user, thread)
  43. moderation.change_thread_title(request, thread, value_cleaned)
  44. return {'title': thread.title}
  45. thread_patch_dispatcher.replace('title', patch_title)
  46. def patch_weight(request, thread, value):
  47. allow_pin_thread(request.user, thread)
  48. if not thread.acl.get('can_pin_globally') and thread.weight == 2:
  49. raise PermissionDenied(_("You can't change globally pinned threads weights in this category."))
  50. if value == 2:
  51. if thread.acl.get('can_pin_globally'):
  52. moderation.pin_thread_globally(request, thread)
  53. else:
  54. raise PermissionDenied(_("You can't pin threads globally in this category."))
  55. elif value == 1:
  56. moderation.pin_thread_locally(request, thread)
  57. elif value == 0:
  58. moderation.unpin_thread(request, thread)
  59. return {'weight': thread.weight}
  60. thread_patch_dispatcher.replace('weight', patch_weight)
  61. def patch_move(request, thread, value):
  62. allow_move_thread(request.user, 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 ValidationError(_("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. allow_approve_thread(request.user, thread)
  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. thread_patch_dispatcher.replace('is-unapproved', patch_is_unapproved)
  92. def patch_is_closed(request, thread, value):
  93. if thread.acl.get('can_close'):
  94. if value:
  95. moderation.close_thread(request, thread)
  96. else:
  97. moderation.open_thread(request, thread)
  98. return {'is_closed': thread.is_closed}
  99. else:
  100. if value:
  101. raise PermissionDenied(_("You don't have permission to close this thread."))
  102. else:
  103. raise PermissionDenied(_("You don't have permission to open this thread."))
  104. thread_patch_dispatcher.replace('is-closed', patch_is_closed)
  105. def patch_is_hidden(request, thread, value):
  106. if value:
  107. allow_hide_thread(request.user, thread)
  108. moderation.hide_thread(request, thread)
  109. else:
  110. allow_unhide_thread(request.user, thread)
  111. moderation.unhide_thread(request, thread)
  112. return {'is_hidden': thread.is_hidden}
  113. thread_patch_dispatcher.replace('is-hidden', patch_is_hidden)
  114. def patch_subscription(request, thread, value):
  115. request.user.subscription_set.filter(thread=thread).delete()
  116. if value == 'notify':
  117. thread.subscription = request.user.subscription_set.create(
  118. thread=thread,
  119. category=thread.category,
  120. last_read_on=thread.last_post_on,
  121. send_email=False,
  122. )
  123. return {'subscription': False}
  124. elif value == 'email':
  125. thread.subscription = request.user.subscription_set.create(
  126. thread=thread,
  127. category=thread.category,
  128. last_read_on=thread.last_post_on,
  129. send_email=True,
  130. )
  131. return {'subscription': True}
  132. else:
  133. return {'subscription': None}
  134. thread_patch_dispatcher.replace('subscription', patch_subscription)
  135. def patch_add_participant(request, thread, value):
  136. allow_add_participants(request.user, thread)
  137. try:
  138. username = six.text_type(value).strip().lower()
  139. if not username:
  140. raise ValidationError(_("You have to enter new participant's username."))
  141. participant = UserModel.objects.get(slug=username)
  142. except UserModel.DoesNotExist:
  143. raise ValidationError(_("No user with such name exists."))
  144. if participant in [p.user for p in thread.participants_list]:
  145. raise ValidationError(_("This user is already thread participant."))
  146. max_participants = request.user.acl_cache['max_private_thread_participants']
  147. if max_participants:
  148. current_participants = len(thread.participants_list)
  149. if current_participants >= max_participants:
  150. raise ValidationError(_("You can't add any more new users to this thread."))
  151. try:
  152. allow_add_participant(request.user, participant)
  153. except PermissionDenied as e:
  154. raise ValidationError(six.text_type(e))
  155. add_participant(request, thread, participant)
  156. make_participants_aware(request.user, thread)
  157. participants = ThreadParticipantSerializer(thread.participants_list, many=True)
  158. return {'participants': participants.data}
  159. thread_patch_dispatcher.add('participants', patch_add_participant)
  160. def patch_remove_participant(request, thread, value):
  161. try:
  162. user_id = int(value)
  163. except (ValueError, TypeError):
  164. user_id = 0
  165. for participant in thread.participants_list:
  166. if participant.user_id == user_id:
  167. break
  168. else:
  169. raise ValidationError(_("Participant doesn't exist."))
  170. allow_remove_participant(request.user, thread, participant.user)
  171. remove_participant(request, thread, participant.user)
  172. if len(thread.participants_list) == 1:
  173. return {'deleted': True}
  174. else:
  175. make_participants_aware(request.user, thread)
  176. participants = ThreadParticipantSerializer(thread.participants_list, many=True)
  177. return {
  178. 'deleted': False,
  179. 'participants': participants.data,
  180. }
  181. thread_patch_dispatcher.remove('participants', patch_remove_participant)
  182. def patch_replace_owner(request, thread, value):
  183. try:
  184. user_id = int(value)
  185. except (ValueError, TypeError):
  186. user_id = 0
  187. for participant in thread.participants_list:
  188. if participant.user_id == user_id:
  189. if participant.is_owner:
  190. raise ValidationError(_("This user already is thread owner."))
  191. else:
  192. break
  193. else:
  194. raise ValidationError(_("Participant doesn't exist."))
  195. allow_change_owner(request.user, thread)
  196. change_owner(request, thread, participant.user)
  197. make_participants_aware(request.user, thread)
  198. participants = ThreadParticipantSerializer(thread.participants_list, many=True)
  199. return {'participants': participants.data}
  200. thread_patch_dispatcher.replace('owner', patch_replace_owner)
  201. def thread_patch_endpoint(request, thread):
  202. old_title = thread.title
  203. old_is_hidden = thread.is_hidden
  204. old_is_unapproved = thread.is_unapproved
  205. old_category = thread.category
  206. response = thread_patch_dispatcher.dispatch(request, thread)
  207. # diff thread's state against pre-patch and resync category if necessary
  208. hidden_changed = old_is_hidden != thread.is_hidden
  209. unapproved_changed = old_is_unapproved != thread.is_unapproved
  210. category_changed = old_category != thread.category
  211. title_changed = old_title != thread.title
  212. if thread.category.last_thread_id != thread.pk:
  213. title_changed = False # don't trigger resync on simple title change
  214. if hidden_changed or unapproved_changed or category_changed:
  215. thread.category.synchronize()
  216. thread.category.save()
  217. if category_changed:
  218. old_category.synchronize()
  219. old_category.save()
  220. elif title_changed:
  221. thread.category.last_thread_title = thread.title
  222. thread.category.last_thread_slug = thread.slug
  223. thread.category.save(update_fields=['last_thread_title', 'last_thread_slug'])
  224. return response
  225. def bulk_patch_endpoint(request, viewmodel):
  226. serializer = BulkPatchSerializer(data=request.data)
  227. if not serializer.is_valid():
  228. return Response(serializer.errors, status=400)
  229. threads = clean_threads_for_patch(request, viewmodel, serializer.data['ids'])
  230. old_titles = [t.title for t in threads]
  231. old_is_hidden = [t.is_hidden for t in threads]
  232. old_is_unapproved = [t.is_unapproved for t in threads]
  233. old_category = [t.category_id for t in threads]
  234. response = thread_patch_dispatcher.dispatch_bulk(request, threads)
  235. new_titles = [t.title for t in threads]
  236. new_is_hidden = [t.is_hidden for t in threads]
  237. new_is_unapproved = [t.is_unapproved for t in threads]
  238. new_category = [t.category_id for t in threads]
  239. # sync titles
  240. if new_titles != old_titles:
  241. for i, t in enumerate(threads):
  242. if t.title != old_titles[i] and t.category.last_thread_id == t.pk:
  243. t.category.last_thread_title = t.title
  244. t.category.last_thread_slug = t.slug
  245. t.category.save(update_fields=['last_thread_title', 'last_thread_slug'])
  246. # sync categories
  247. sync_categories = []
  248. if new_is_hidden != old_is_hidden:
  249. for i, t in enumerate(threads):
  250. if t.is_hidden != old_is_hidden[i] and t.category_id not in sync_categories:
  251. sync_categories.append(t.category_id)
  252. if new_is_unapproved != old_is_unapproved:
  253. for i, t in enumerate(threads):
  254. if t.is_unapproved != old_is_unapproved[i] and t.category_id not in sync_categories:
  255. sync_categories.append(t.category_id)
  256. if new_category != old_category:
  257. for i, t in enumerate(threads):
  258. if t.category_id != old_category[i]:
  259. if t.category_id not in sync_categories:
  260. sync_categories.append(t.category_id)
  261. if old_category[i] not in sync_categories:
  262. sync_categories.append(old_category[i])
  263. if sync_categories:
  264. for category in Category.objects.filter(id__in=sync_categories):
  265. category.synchronize()
  266. category.save()
  267. return response
  268. def clean_threads_for_patch(request, viewmodel, threads_ids):
  269. threads = []
  270. for thread_id in sorted(set(threads_ids), reverse=True):
  271. threads.append(viewmodel(request, thread_id).unwrap())
  272. return threads
  273. class BulkPatchSerializer(serializers.Serializer):
  274. ids = serializers.ListField(
  275. child=serializers.IntegerField(min_value=1),
  276. max_length=PATCH_LIMIT,
  277. min_length=1,
  278. )
  279. ops = serializers.ListField(
  280. child=serializers.DictField(),
  281. min_length=1,
  282. max_length=10,
  283. )