split.py 3.4 KB

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