Skip to content

Commit

Permalink
Expand users details in filter swaps endpoint (#2921)
Browse files Browse the repository at this point in the history
Include additional information about beneficiary/benefactor users to
avoid extra requests when listing swap requests details.
  • Loading branch information
matiasb authored Aug 30, 2023
1 parent 44c2e6a commit 939188e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
30 changes: 29 additions & 1 deletion engine/apps/api/serializers/shift_swap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import typing

from django.utils import timezone
from rest_framework import serializers
Expand All @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
13 changes: 11 additions & 2 deletions engine/apps/api/tests/test_schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
Expand Down
4 changes: 2 additions & 2 deletions engine/apps/api/views/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 939188e

Please sign in to comment.