participants.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.translation import gettext as _, ngettext
  5. from misago.acl import useracl
  6. from misago.categories import PRIVATE_THREADS_ROOT_NAME
  7. from misago.threads.participants import add_participants, set_owner
  8. from misago.threads.permissions import allow_message_user
  9. from . import PostingEndpoint, PostingMiddleware
  10. UserModel = get_user_model()
  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(
  18. data=self.request.data,
  19. context={
  20. 'request': self.request,
  21. 'user': self.user,
  22. 'user_acl': self.user_acl,
  23. },
  24. )
  25. def save(self, serializer):
  26. set_owner(self.thread, self.user)
  27. add_participants(self.request, self.thread, serializer.users_cache)
  28. class ParticipantsSerializer(serializers.Serializer):
  29. to = serializers.ListField(child=serializers.CharField(), required=True)
  30. def validate_to(self, usernames):
  31. clean_usernames = self.clean_usernames(usernames)
  32. self.users_cache = self.get_users(clean_usernames)
  33. def clean_usernames(self, usernames):
  34. clean_usernames = []
  35. for name in usernames:
  36. clean_name = name.strip().lower()
  37. if clean_name == self.context['user'].slug:
  38. raise serializers.ValidationError(
  39. _("You can't include yourself on the list of users to invite to new thread.")
  40. )
  41. if clean_name and clean_name not in clean_usernames:
  42. clean_usernames.append(clean_name)
  43. if not clean_usernames:
  44. raise serializers.ValidationError(_("You have to enter user names."))
  45. max_participants = self.context['user_acl']['max_private_thread_participants']
  46. if max_participants and len(clean_usernames) > max_participants:
  47. message = ngettext(
  48. "You can't add more than %(users)s user to private thread (you've added %(added)s).",
  49. "You can't add more than %(users)s users to private thread (you've added %(added)s).",
  50. max_participants,
  51. )
  52. raise serializers.ValidationError(
  53. message % {
  54. 'users': max_participants,
  55. 'added': len(clean_usernames),
  56. }
  57. )
  58. return list(set(clean_usernames))
  59. def get_users(self, usernames):
  60. users = []
  61. for user in UserModel.objects.filter(slug__in=usernames):
  62. try:
  63. user_acl = useracl.get_user_acl(user, self.context["request"].cache_versions)
  64. allow_message_user(self.context['user_acl'], user, user_acl)
  65. except PermissionDenied as e:
  66. raise serializers.ValidationError(str(e))
  67. users.append(user)
  68. if len(usernames) != len(users):
  69. invalid_usernames = set(usernames) - set([u.slug for u in users])
  70. sorted_usernames = sorted(invalid_usernames)
  71. message = _("One or more users could not be found: %(usernames)s")
  72. raise serializers.ValidationError(message % {'usernames': ', '.join(sorted_usernames)})
  73. return users