From 939188e4a35b268060f31a1fc0e4c5a0242b441e Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Wed, 30 Aug 2023 11:28:49 -0300 Subject: [PATCH] Expand users details in filter swaps endpoint (#2921) Include additional information about beneficiary/benefactor users to avoid extra requests when listing swap requests details. --- CHANGELOG.md | 1 + engine/apps/api/serializers/shift_swap.py | 30 ++++++++++++++++++++++- engine/apps/api/tests/test_schedules.py | 13 ++++++++-- engine/apps/api/views/schedule.py | 4 +-- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1229b142f..6ce5ae0d4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Performance and UX tweaks to integrations page ([#2869](https://github.com/grafana/oncall/pull/2869)) +- Expand users details in filter swaps internal endpoint ([#2921](https://github.com/grafana/oncall/pull/2921)) ## v1.3.29 (2023-08-29) diff --git a/engine/apps/api/serializers/shift_swap.py b/engine/apps/api/serializers/shift_swap.py index cfcf46e6f5..355751a5b4 100644 --- a/engine/apps/api/serializers/shift_swap.py +++ b/engine/apps/api/serializers/shift_swap.py @@ -1,4 +1,5 @@ import datetime +import typing from django.utils import timezone from rest_framework import serializers @@ -7,8 +8,11 @@ from common.api_helpers.custom_fields import OrganizationFilteredPrimaryKeyRelatedField, TimeZoneAwareDatetimeField from common.api_helpers.mixins import EagerLoadingMixin +if typing.TYPE_CHECKING: + from apps.user_management.models import User -class ShiftSwapRequestListSerializer(EagerLoadingMixin, serializers.ModelSerializer): + +class BaseShiftSwapRequestListSerializer(EagerLoadingMixin, serializers.ModelSerializer): id = serializers.CharField(read_only=True, source="public_primary_key") schedule = OrganizationFilteredPrimaryKeyRelatedField(queryset=OnCallSchedule.objects) @@ -45,6 +49,8 @@ class Meta: "status", ] + +class ShiftSwapRequestListSerializer(BaseShiftSwapRequestListSerializer): def get_benefactor(self, obj: ShiftSwapRequest) -> str | None: return obj.benefactor.public_primary_key if obj.benefactor else None @@ -88,3 +94,25 @@ def validate(self, data): # between swap_start and swap_end return data + + +class ShiftSwapRequestExpandedUsersSerializer(BaseShiftSwapRequestListSerializer): + beneficiary = serializers.SerializerMethodField(read_only=True) + benefactor = serializers.SerializerMethodField(read_only=True) + + def _serialize_user(self, user: "User") -> dict | None: + user_data = None + if user: + user_data = { + "display_name": user.username, + "email": user.email, + "pk": user.public_primary_key, + "avatar_full": user.avatar_full_url, + } + return user_data + + def get_benefactor(self, obj: ShiftSwapRequest) -> dict | None: + return self._serialize_user(obj.benefactor) + + def get_beneficiary(self, obj: ShiftSwapRequest) -> dict | None: + return self._serialize_user(obj.beneficiary) diff --git a/engine/apps/api/tests/test_schedules.py b/engine/apps/api/tests/test_schedules.py index 8925d3e748..8b9e983d70 100644 --- a/engine/apps/api/tests/test_schedules.py +++ b/engine/apps/api/tests/test_schedules.py @@ -1310,13 +1310,22 @@ def test_filter_swap_requests( response = client.get(url, format="json", **make_user_auth_headers(admin, token)) assert response.status_code == status.HTTP_200_OK + def _serialized_user(u): + if u: + return { + "display_name": u.username, + "email": u.email, + "pk": u.public_primary_key, + "avatar_full": u.avatar_full_url, + } + expected = [ { "pk": swap.public_primary_key, "swap_start": serialize_datetime_as_utc_timestamp(swap.swap_start), "swap_end": serialize_datetime_as_utc_timestamp(swap.swap_end), - "beneficiary": swap.beneficiary.public_primary_key, - "benefactor": swap.benefactor.public_primary_key if swap.benefactor else None, + "beneficiary": _serialized_user(swap.beneficiary), + "benefactor": _serialized_user(swap.benefactor), } for swap in (swap_a, swap_b) ] diff --git a/engine/apps/api/views/schedule.py b/engine/apps/api/views/schedule.py index 3488a8392e..38720ac30e 100644 --- a/engine/apps/api/views/schedule.py +++ b/engine/apps/api/views/schedule.py @@ -28,7 +28,7 @@ PolymorphicScheduleSerializer, PolymorphicScheduleUpdateSerializer, ) -from apps.api.serializers.shift_swap import ShiftSwapRequestSerializer +from apps.api.serializers.shift_swap import ShiftSwapRequestExpandedUsersSerializer from apps.api.serializers.user import ScheduleUserSerializer from apps.auth_token.auth import PluginAuthentication from apps.auth_token.constants import SCHEDULE_EXPORT_TOKEN_NAME @@ -356,7 +356,7 @@ def filter_shift_swaps(self, request: Request, pk: str) -> Response: swap_requests = schedule.filter_swap_requests(datetime_start, datetime_end) - serialized_swap_requests = ShiftSwapRequestSerializer(swap_requests, many=True) + serialized_swap_requests = ShiftSwapRequestExpandedUsersSerializer(swap_requests, many=True) result = {"shift_swaps": serialized_swap_requests.data} return Response(result, status=status.HTTP_200_OK)