oncall-engine/engine/apps/api/serializers/schedule_polymorphic.py
Joey Orlando 9dde1805aa
add mypy static type checker to backend codebase (#2151)
# What this PR does

- Adds [`mypy` static type checking](https://mypy-lang.org/) to our CI
pipeline. Currently there is still a **ton** of errors being returned by
the tool, as we'll need to fix pre-existing errors. I think we can
slowly chip away at these errors in small PRs, doing them all in one
large PR is likely very risky.
- Also, this PR starts chipping away at one of the main type errors that
we have which is accessing the `datetime` class (from the `datetime`
library) or `timedelta` function on the `django.utils.timezone` module.
Basically we should be instead accessing these two objects from the
native `datetime` module. This makes sense because the [`__all__`
attribute](https://github.com/django/django/blob/main/django/utils/timezone.py#L14-L30)
in `django.utils.timezone` does not re-export `datetime` or `timedelta`.
- splits `engine` dependencies out into `requirements.txt` and
`requirements-dev.txt`

## Checklist

- [ ] Unit, integration, and e2e (if applicable) tests updated (N/A)
- [ ] Documentation added (or `pr:no public docs` PR label added if not
required) (N/A)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required) (N/A)
2023-06-12 12:50:33 -04:00

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"]
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,
}