Expand users details in filter swaps endpoint (#2921)
Include additional information about beneficiary/benefactor users to avoid extra requests when listing swap requests details.
This commit is contained in:
parent
44c2e6a1d1
commit
939188e4a3
4 changed files with 43 additions and 5 deletions
|
|
@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Performance and UX tweaks to integrations page ([#2869](https://github.com/grafana/oncall/pull/2869))
|
- 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)
|
## v1.3.29 (2023-08-29)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
import typing
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
@ -7,8 +8,11 @@ from apps.schedules.models import OnCallSchedule, ShiftSwapRequest
|
||||||
from common.api_helpers.custom_fields import OrganizationFilteredPrimaryKeyRelatedField, TimeZoneAwareDatetimeField
|
from common.api_helpers.custom_fields import OrganizationFilteredPrimaryKeyRelatedField, TimeZoneAwareDatetimeField
|
||||||
from common.api_helpers.mixins import EagerLoadingMixin
|
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")
|
id = serializers.CharField(read_only=True, source="public_primary_key")
|
||||||
schedule = OrganizationFilteredPrimaryKeyRelatedField(queryset=OnCallSchedule.objects)
|
schedule = OrganizationFilteredPrimaryKeyRelatedField(queryset=OnCallSchedule.objects)
|
||||||
|
|
||||||
|
|
@ -45,6 +49,8 @@ class ShiftSwapRequestListSerializer(EagerLoadingMixin, serializers.ModelSeriali
|
||||||
"status",
|
"status",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class ShiftSwapRequestListSerializer(BaseShiftSwapRequestListSerializer):
|
||||||
def get_benefactor(self, obj: ShiftSwapRequest) -> str | None:
|
def get_benefactor(self, obj: ShiftSwapRequest) -> str | None:
|
||||||
return obj.benefactor.public_primary_key if obj.benefactor else None
|
return obj.benefactor.public_primary_key if obj.benefactor else None
|
||||||
|
|
||||||
|
|
@ -88,3 +94,25 @@ class ShiftSwapRequestSerializer(ShiftSwapRequestListSerializer):
|
||||||
# between swap_start and swap_end
|
# between swap_start and swap_end
|
||||||
|
|
||||||
return data
|
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)
|
||||||
|
|
|
||||||
|
|
@ -1310,13 +1310,22 @@ def test_filter_swap_requests(
|
||||||
response = client.get(url, format="json", **make_user_auth_headers(admin, token))
|
response = client.get(url, format="json", **make_user_auth_headers(admin, token))
|
||||||
assert response.status_code == status.HTTP_200_OK
|
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 = [
|
expected = [
|
||||||
{
|
{
|
||||||
"pk": swap.public_primary_key,
|
"pk": swap.public_primary_key,
|
||||||
"swap_start": serialize_datetime_as_utc_timestamp(swap.swap_start),
|
"swap_start": serialize_datetime_as_utc_timestamp(swap.swap_start),
|
||||||
"swap_end": serialize_datetime_as_utc_timestamp(swap.swap_end),
|
"swap_end": serialize_datetime_as_utc_timestamp(swap.swap_end),
|
||||||
"beneficiary": swap.beneficiary.public_primary_key,
|
"beneficiary": _serialized_user(swap.beneficiary),
|
||||||
"benefactor": swap.benefactor.public_primary_key if swap.benefactor else None,
|
"benefactor": _serialized_user(swap.benefactor),
|
||||||
}
|
}
|
||||||
for swap in (swap_a, swap_b)
|
for swap in (swap_a, swap_b)
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ from apps.api.serializers.schedule_polymorphic import (
|
||||||
PolymorphicScheduleSerializer,
|
PolymorphicScheduleSerializer,
|
||||||
PolymorphicScheduleUpdateSerializer,
|
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.api.serializers.user import ScheduleUserSerializer
|
||||||
from apps.auth_token.auth import PluginAuthentication
|
from apps.auth_token.auth import PluginAuthentication
|
||||||
from apps.auth_token.constants import SCHEDULE_EXPORT_TOKEN_NAME
|
from apps.auth_token.constants import SCHEDULE_EXPORT_TOKEN_NAME
|
||||||
|
|
@ -356,7 +356,7 @@ class ScheduleView(
|
||||||
|
|
||||||
swap_requests = schedule.filter_swap_requests(datetime_start, datetime_end)
|
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}
|
result = {"shift_swaps": serialized_swap_requests.data}
|
||||||
|
|
||||||
return Response(result, status=status.HTTP_200_OK)
|
return Response(result, status=status.HTTP_200_OK)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue