move.py 3.4 KB

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