split.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from rest_framework import serializers
  2. from rest_framework.response import Response
  3. from django.core.exceptions import PermissionDenied
  4. from django.utils.translation import ugettext as _
  5. from django.utils.translation import ungettext
  6. from misago.conf import settings
  7. from misago.threads.events import record_event
  8. from misago.threads.models import Thread
  9. from misago.threads.moderation import threads as moderation
  10. from misago.threads.permissions.threads import exclude_invisible_posts
  11. from misago.threads.serializers import NewThreadSerializer
  12. SPLIT_LIMIT = settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL
  13. class SplitError(Exception):
  14. def __init__(self, msg):
  15. self.msg = msg
  16. def posts_split_endpoint(request, thread):
  17. if not thread.acl['can_move_posts']:
  18. raise PermissionDenied(_("You can't split posts from this thread."))
  19. try:
  20. posts = clean_posts_for_split(request, thread)
  21. except SplitError as e:
  22. return Response({'detail': e.msg}, status=400)
  23. serializer = NewThreadSerializer(context=request.user, data=request.data)
  24. if serializer.is_valid():
  25. split_posts_to_new_thread(request, thread, serializer.validated_data, posts)
  26. return Response({})
  27. else:
  28. return Response(serializer.errors, status=400)
  29. def clean_posts_for_split(request, thread):
  30. try:
  31. posts_ids = list(map(int, request.data.get('posts', [])))
  32. except (ValueError, TypeError):
  33. raise SplitError(_("One or more post ids received were invalid."))
  34. if not posts_ids:
  35. raise SplitError(_("You have to specify at least one post to split."))
  36. elif len(posts_ids) > SPLIT_LIMIT:
  37. message = ungettext(
  38. "No more than %(limit)s post can be split at single time.",
  39. "No more than %(limit)s posts can be split at single time.",
  40. SPLIT_LIMIT)
  41. raise SplitError(message % {'limit': SPLIT_LIMIT})
  42. posts_queryset = exclude_invisible_posts(request.user, thread.category, thread.post_set)
  43. posts_queryset = posts_queryset.select_for_update().filter(id__in=posts_ids).order_by('id')
  44. posts = []
  45. for post in posts_queryset:
  46. if post.is_event:
  47. raise SplitError(_("Events can't be split."))
  48. if post.pk == thread.first_post_id:
  49. raise SplitError(_("You can't split thread's first post."))
  50. if post.is_hidden and not thread.category.acl['can_hide_posts']:
  51. raise SplitError(_("You can't split posts the content you can't see."))
  52. posts.append(post)
  53. if len(posts) != len(posts_ids):
  54. raise SplitError(_("One or more posts to split could not be found."))
  55. return posts
  56. def split_posts_to_new_thread(request, thread, validated_data, posts):
  57. new_thread = Thread(
  58. category=validated_data['category'],
  59. started_on=thread.started_on,
  60. last_post_on=thread.last_post_on
  61. )
  62. new_thread.set_title(validated_data['title'])
  63. new_thread.save()
  64. for post in posts:
  65. post.move(new_thread)
  66. post.save()
  67. thread.synchronize()
  68. thread.save()
  69. new_thread.synchronize()
  70. new_thread.save()
  71. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  72. moderation.pin_thread_globally(request, new_thread)
  73. elif validated_data.get('weight'):
  74. moderation.pin_thread_locally(request, new_thread)
  75. if validated_data.get('is_hidden', False):
  76. moderation.hide_thread(request, new_thread)
  77. if validated_data.get('is_closed', False):
  78. moderation.close_thread(request, new_thread)
  79. thread.category.synchronize()
  80. thread.category.save()
  81. if new_thread.category != thread.category:
  82. new_thread.category.synchronize()
  83. new_thread.category.save()