participants.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from rest_framework import serializers
  2. from django.contrib.auth import get_user_model
  3. from django.core.exceptions import PermissionDenied
  4. from django.utils import six
  5. from django.utils.translation import ugettext as _
  6. from django.utils.translation import ungettext
  7. from misago.categories import PRIVATE_THREADS_ROOT_NAME
  8. from misago.threads.participants import add_participants, set_owner
  9. from misago.threads.permissions import allow_message_user
  10. from . import PostingEndpoint, PostingMiddleware
  11. UserModel = get_user_model()
  12. class ParticipantsMiddleware(PostingMiddleware):
  13. def use_this_middleware(self):
  14. if self.mode == PostingEndpoint.START:
  15. return self.tree_name == PRIVATE_THREADS_ROOT_NAME
  16. return False
  17. def get_serializer(self):
  18. return ParticipantsSerializer(data=self.request.data, context={'user': self.user})
  19. def save(self, serializer):
  20. set_owner(self.thread, self.user)
  21. add_participants(self.request, self.thread, serializer.users_cache)
  22. class ParticipantsSerializer(serializers.Serializer):
  23. to = serializers.ListField(child=serializers.CharField(), required=True)
  24. def validate_to(self, usernames):
  25. clean_usernames = self.clean_usernames(usernames)
  26. self.users_cache = self.get_users(clean_usernames)
  27. def clean_usernames(self, usernames):
  28. clean_usernames = []
  29. for name in usernames:
  30. clean_name = name.strip().lower()
  31. if clean_name == self.context['user'].slug:
  32. raise serializers.ValidationError(
  33. _("You can't include yourself on the list of users to invite to new thread.")
  34. )
  35. if clean_name and clean_name not in clean_usernames:
  36. clean_usernames.append(clean_name)
  37. if not clean_usernames:
  38. raise serializers.ValidationError(_("You have to enter user names."))
  39. max_participants = self.context['user'].acl_cache['max_private_thread_participants']
  40. if max_participants and len(clean_usernames) > max_participants:
  41. message = ungettext(
  42. "You can't add more than %(users)s user to private thread (you've added %(added)s).",
  43. "You can't add more than %(users)s users to private thread (you've added %(added)s).",
  44. max_participants
  45. )
  46. raise serializers.ValidationError(
  47. message % {'users': max_participants,
  48. 'added': len(clean_usernames)}
  49. )
  50. return list(set(clean_usernames))
  51. def get_users(self, usernames):
  52. users = []
  53. for user in UserModel.objects.filter(slug__in=usernames):
  54. try:
  55. allow_message_user(self.context['user'], user)
  56. except PermissionDenied as e:
  57. raise serializers.ValidationError(six.text_type(e))
  58. users.append(user)
  59. if len(usernames) != len(users):
  60. invalid_usernames = set(usernames) - set([u.slug for u in users])
  61. sorted_usernames = sorted(invalid_usernames)
  62. message = _("One or more users could not be found: %(usernames)s")
  63. raise serializers.ValidationError(message % {'usernames': ', '.join(sorted_usernames)})
  64. return users