participants.py 3.2 KB

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