serializers.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from rest_framework import serializers, status
  2. from rest_framework.exceptions import APIException
  3. from projects.serializers.base_serializers import ProjectReferenceWithMemberSerializer
  4. from users.serializers import UserSerializer
  5. from teams.serializers import TeamSerializer
  6. from teams.models import Team
  7. from .base_serializers import OrganizationReferenceSerializer
  8. from ..models import OrganizationUser, OrganizationUserRole, ROLES
  9. class OrganizationSerializer(OrganizationReferenceSerializer):
  10. pass
  11. class OrganizationDetailSerializer(OrganizationSerializer):
  12. projects = ProjectReferenceWithMemberSerializer(many=True)
  13. teams = TeamSerializer(many=True)
  14. openMembership = serializers.BooleanField(source="open_membership")
  15. scrubIPAddresses = serializers.BooleanField(source="scrub_ip_addresses")
  16. class Meta(OrganizationSerializer.Meta):
  17. fields = OrganizationSerializer.Meta.fields + (
  18. "projects",
  19. "openMembership",
  20. "scrubIPAddresses",
  21. "teams",
  22. )
  23. class HTTP409APIException(APIException):
  24. status_code = status.HTTP_409_CONFLICT
  25. class OrganizationUserSerializer(serializers.ModelSerializer):
  26. user = UserSerializer(required=False, read_only=True)
  27. role = serializers.CharField(source="get_role")
  28. roleName = serializers.CharField(source="get_role_display", read_only=True)
  29. dateCreated = serializers.DateTimeField(source="created", read_only=True)
  30. teams = serializers.SlugRelatedField(
  31. many=True, write_only=True, slug_field="slug", queryset=Team.objects.none()
  32. )
  33. class Meta:
  34. model = OrganizationUser
  35. fields = (
  36. "role",
  37. "id",
  38. "user",
  39. "roleName",
  40. "dateCreated",
  41. "email",
  42. "teams",
  43. "pending",
  44. )
  45. def __init__(self, *args, request_user=None, **kwargs):
  46. super().__init__(*args, **kwargs)
  47. if "request" in self.context:
  48. organization_slug = self.context["view"].kwargs.get("organization_slug")
  49. self.fields["teams"].child_relation.queryset = Team.objects.filter(
  50. organization__slug=organization_slug
  51. )
  52. def get_extra_kwargs(self):
  53. """ email should be read only when updating """
  54. extra_kwargs = super().get_extra_kwargs()
  55. if self.instance is not None:
  56. extra_kwargs["email"] = {"read_only": True}
  57. extra_kwargs["user"] = {"read_only": True}
  58. return extra_kwargs
  59. def create(self, validated_data):
  60. role = OrganizationUserRole.from_string(validated_data.get("get_role"))
  61. email = validated_data.get("email")
  62. organization = validated_data.get("organization")
  63. teams = validated_data.get("teams")
  64. if organization.organization_users.filter(email=email).exists():
  65. raise HTTP409APIException(f"The user {email} is already invited", "email")
  66. if organization.organization_users.filter(user__email=email).exists():
  67. raise HTTP409APIException(f"The user {email} is already a member", "email")
  68. org_user = super().create(
  69. {"role": role, "email": email, "organization": organization}
  70. )
  71. org_user.team_set.add(*teams)
  72. return org_user
  73. def update(self, instance, validated_data):
  74. get_role = validated_data.pop("get_role", None)
  75. if get_role:
  76. role = OrganizationUserRole.from_string(get_role)
  77. validated_data["role"] = role
  78. return super().update(instance, validated_data)
  79. def to_representation(self, obj):
  80. """ Override email for representation to potientially show user's email """
  81. self.fields["email"] = serializers.SerializerMethodField()
  82. return super().to_representation(obj)
  83. def get_email(self, obj):
  84. """ Prefer user primary email over org user email (which is only for invites) """
  85. if obj.user:
  86. return obj.user.email
  87. return obj.email
  88. class OrganizationUserDetailSerializer(OrganizationUserSerializer):
  89. teams = serializers.SlugRelatedField(
  90. source="team_set", slug_field="slug", read_only=True, many=True
  91. )
  92. roles = serializers.SerializerMethodField()
  93. class Meta(OrganizationUserSerializer.Meta):
  94. fields = OrganizationUserSerializer.Meta.fields + ("roles",)
  95. def get_roles(self, obj):
  96. return ROLES
  97. class OrganizationUserProjectsSerializer(OrganizationUserSerializer):
  98. projects = serializers.SerializerMethodField()
  99. class Meta(OrganizationUserSerializer.Meta):
  100. fields = OrganizationUserSerializer.Meta.fields + ("projects",)
  101. def get_projects(self, obj):
  102. return obj.organization.projects.filter(team__members=obj).values_list(
  103. "slug", flat=True
  104. )
  105. class ReinviteSerializer(serializers.Serializer):
  106. reinvite = serializers.IntegerField()
  107. def update(self, instance, validated_data):
  108. if validated_data.get("reinvite"):
  109. pass
  110. # Send email
  111. return instance
  112. class OrganizationUserOrganizationSerializer(OrganizationUserSerializer):
  113. """ Organization User Serializer with Organization info """
  114. organization = OrganizationSerializer()
  115. class Meta(OrganizationUserSerializer.Meta):
  116. fields = OrganizationUserSerializer.Meta.fields + ("organization",)
  117. class AcceptInviteSerializer(serializers.Serializer):
  118. accept_invite = serializers.BooleanField()
  119. org_user = OrganizationUserOrganizationSerializer(read_only=True)