# What this PR does Reduces number of calls to db for `/schedules`, `/alertgroups` and `/users` endpoints. Fixes the issue when there was an additional call to db to get organization url to build user avatar full link. ## Which issue(s) this PR closes Related to [issue link here] <!-- *Note*: If you want the issue to be auto-closed once the PR is merged, change "Related to" to "Closes" in the line above. If you have more than one GitHub issue that this PR closes, be sure to preface each issue link with a [closing keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue). This ensures that the issue(s) are auto-closed once the PR has been merged. --> ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes.
45 lines
1.8 KiB
Python
45 lines
1.8 KiB
Python
from rest_polymorphic.serializers import PolymorphicSerializer
|
|
|
|
from apps.api.serializers.schedule_calendar import ScheduleCalendarCreateSerializer, ScheduleCalendarSerializer
|
|
from apps.api.serializers.schedule_ical import (
|
|
ScheduleICalCreateSerializer,
|
|
ScheduleICalSerializer,
|
|
ScheduleICalUpdateSerializer,
|
|
)
|
|
from apps.api.serializers.schedule_web import ScheduleWebCreateSerializer, ScheduleWebSerializer
|
|
from apps.schedules.models import OnCallScheduleCalendar, OnCallScheduleICal, OnCallScheduleWeb
|
|
from common.api_helpers.mixins import EagerLoadingMixin
|
|
|
|
|
|
class PolymorphicScheduleSerializer(EagerLoadingMixin, PolymorphicSerializer):
|
|
SELECT_RELATED = ["organization", "user_group", "team"]
|
|
|
|
resource_type_field_name = "type"
|
|
|
|
model_serializer_mapping = {
|
|
OnCallScheduleICal: ScheduleICalSerializer,
|
|
OnCallScheduleCalendar: ScheduleCalendarSerializer,
|
|
OnCallScheduleWeb: ScheduleWebSerializer,
|
|
}
|
|
|
|
SCHEDULE_CLASS_TO_TYPE = {OnCallScheduleCalendar: 0, OnCallScheduleICal: 1, OnCallScheduleWeb: 2}
|
|
|
|
def to_resource_type(self, model_or_instance):
|
|
return self.SCHEDULE_CLASS_TO_TYPE.get(model_or_instance._meta.model)
|
|
|
|
|
|
class PolymorphicScheduleCreateSerializer(PolymorphicScheduleSerializer):
|
|
model_serializer_mapping = {
|
|
OnCallScheduleICal: ScheduleICalCreateSerializer,
|
|
OnCallScheduleCalendar: ScheduleCalendarCreateSerializer,
|
|
OnCallScheduleWeb: ScheduleWebCreateSerializer,
|
|
}
|
|
|
|
|
|
class PolymorphicScheduleUpdateSerializer(PolymorphicScheduleSerializer):
|
|
model_serializer_mapping = {
|
|
OnCallScheduleICal: ScheduleICalUpdateSerializer,
|
|
# There is no difference between create and Update serializers for ScheduleCalendar
|
|
OnCallScheduleCalendar: ScheduleCalendarCreateSerializer,
|
|
OnCallScheduleWeb: ScheduleWebCreateSerializer,
|
|
}
|