move.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from django.conf import settings
  2. from django.core.exceptions import PermissionDenied
  3. from django.http import Http404
  4. from django.utils.translation import ugettext as _, ungettext
  5. from rest_framework.response import Response
  6. from ...permissions.threads import exclude_invisible_posts
  7. from ...utils import get_thread_id_from_url
  8. MOVE_LIMIT = settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL
  9. class MoveError(Exception):
  10. def __init__(self, msg):
  11. self.msg = msg
  12. def posts_move_endpoint(request, thread, viewmodel):
  13. if not thread.acl['can_move_posts']:
  14. raise PermissionDenied(_("You can't move posts in this thread."))
  15. try:
  16. new_thread = clean_thread_for_move(request, thread, viewmodel)
  17. posts = clean_posts_for_move(request, thread)
  18. except MoveError as e:
  19. return Response({'detail': e.msg}, status=400)
  20. for post in posts:
  21. post.move(new_thread)
  22. post.save()
  23. thread.synchronize()
  24. thread.save()
  25. new_thread.synchronize()
  26. new_thread.save()
  27. thread.category.synchronize()
  28. thread.category.save()
  29. if thread.category != new_thread.category:
  30. new_thread.category.synchronize()
  31. new_thread.category.save()
  32. return Response({})
  33. def clean_thread_for_move(request, thread, viewmodel):
  34. new_thread_id = get_thread_id_from_url(request, request.data.get('thread_url', None))
  35. if not new_thread_id:
  36. raise MoveError(_("This is not a valid thread link."))
  37. if new_thread_id == thread.pk:
  38. raise MoveError(_("Thread to move posts to is same as current one."))
  39. try:
  40. new_thread = viewmodel(request, new_thread_id, select_for_update=True).model
  41. except PermissionDenied as e:
  42. raise MoveError(e.args[0])
  43. except Http404:
  44. raise MoveError(_("The thread you have entered link to doesn't exist or you don't have permission to see it."))
  45. if not new_thread.acl['can_reply']:
  46. raise MoveError(_("You can't move posts to threads you can't reply."))
  47. return new_thread
  48. def clean_posts_for_move(request, thread):
  49. try:
  50. posts_ids = list(map(int, request.data.get('posts', [])))
  51. except (ValueError, TypeError):
  52. raise MoveError(_("One or more post ids received were invalid."))
  53. if not posts_ids:
  54. raise MoveError(_("You have to specify at least one post to move."))
  55. elif len(posts_ids) > MOVE_LIMIT:
  56. message = ungettext(
  57. "No more than %(limit)s post can be moved at single time.",
  58. "No more than %(limit)s posts can be moved at single time.",
  59. MOVE_LIMIT)
  60. raise MoveError(message % {'limit': MOVE_LIMIT})
  61. posts_queryset = exclude_invisible_posts(request.user, thread.category, thread.post_set)
  62. posts_queryset = posts_queryset.select_for_update().filter(id__in=posts_ids).order_by('id')
  63. posts = []
  64. for post in posts_queryset:
  65. if post.is_event:
  66. raise MoveError(_("Events can't be moved."))
  67. if post.pk == thread.first_post_id:
  68. raise MoveError(_("You can't move thread's first post."))
  69. if post.is_hidden and not thread.category.acl['can_hide_posts']:
  70. raise MoveError(_("You can't move posts the content you can't see."))
  71. posts.append(post)
  72. if len(posts) != len(posts_ids):
  73. raise MoveError(_("One or more posts to move could not be found."))
  74. return posts