split.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 ...models import THREAD_WEIGHT_DEFAULT, THREAD_WEIGHT_GLOBAL
  7. from ...permissions.threads import exclude_invisible_posts
  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. # HERE run serializer to validate if split thread stuff
  18. # create thread
  19. # move posts to it
  20. # moderate new thread
  21. # sync old and new thread/categories
  22. def clean_posts_for_split(request, thread):
  23. try:
  24. posts_ids = list(map(int, request.data.get('posts', [])))
  25. except (ValueError, TypeError):
  26. raise SplitError(_("One or more post ids received were invalid."))
  27. if not posts_ids:
  28. raise SplitError(_("You have to specify at least one post to split."))
  29. elif len(posts_ids) > SPLIT_LIMIT:
  30. message = ungettext(
  31. "No more than %(limit)s post can be split at single time.",
  32. "No more than %(limit)s posts can be split at single time.",
  33. SPLIT_LIMIT)
  34. raise SplitError(message % {'limit': SPLIT_LIMIT})
  35. posts_queryset = exclude_invisible_posts(request.user, thread.category, thread.post_set)
  36. posts_queryset = posts_queryset.select_for_update().filter(id__in=posts_ids).order_by('id')
  37. posts = []
  38. for post in posts_queryset:
  39. if post.is_event:
  40. raise SplitError(_("Events can't be split."))
  41. if post.pk == thread.first_post_id:
  42. raise SplitError(_("You can't split thread's first post."))
  43. if post.is_hidden and not thread.category.acl['can_hide_posts']:
  44. raise SplitError(_("You can't split posts the content you can't see."))
  45. posts.append(post)
  46. if len(posts) != len(posts_ids):
  47. raise SplitError(_("One or more posts to split could not be found."))
  48. return posts
  49. class SplitPostsSerializer(serializers.Serializer):
  50. title = serializers.CharField()
  51. category = serializers.IntegerField()
  52. weight = serializers.IntegerField(
  53. required=False,
  54. allow_null=True,
  55. max_value=THREAD_WEIGHT_GLOBAL,
  56. min_value=THREAD_WEIGHT_DEFAULT,
  57. )
  58. is_hidden = serializers.NullBooleanField(required=False)
  59. is_closed = serializers.NullBooleanField(required=False)
  60. def validate_title(self, title):
  61. return validate_title(title)
  62. def validate_category(self, category_id):
  63. self.category = validate_category(self.context, category_id)
  64. return self.category
  65. def validate_weight(self, weight):
  66. try:
  67. add_acl(self.context, self.category)
  68. except AttributeError:
  69. return weight # don't validate weight further if category failed
  70. if weight > self.category.acl.get('can_pin_threads', 0):
  71. if weight == 2:
  72. raise ValidationError(_("You don't have permission to pin threads globally in this category."))
  73. else:
  74. raise ValidationError(_("You don't have permission to pin threads in this category."))
  75. return weight
  76. def validate_is_hidden(self, is_hidden):
  77. try:
  78. add_acl(self.context, self.category)
  79. except AttributeError:
  80. return is_hidden # don't validate closed further if category failed
  81. if is_hidden and not self.category.acl.get('can_hide_threads'):
  82. raise ValidationError(_("You don't have permission to hide threads in this category."))
  83. return is_hidden
  84. def validate_is_closed(self, is_closed):
  85. try:
  86. add_acl(self.context, self.category)
  87. except AttributeError:
  88. return is_closed # don't validate closed further if category failed
  89. if is_closed and not self.category.acl.get('can_close_threads'):
  90. raise ValidationError(_("You don't have permission to close threads in this category."))
  91. return is_closed