oncall-engine/engine/apps/public_api/serializers/schedules_calendar.py
Vadim Stepanov 1d6767ae74
Remove unnecessary team checks (#2606)
# What this PR does

Removes unnecessary team checks enforced by public API, as they seem to
be outdated and not aligned with the web UI and docs.

From public
[docs](https://grafana.com/docs/oncall/latest/user-and-team-management/#manage-teams-in-grafana-oncall):
> Resources from different teams can be connected with one another. For
instance, you can create an integration in one team, set up multiple
routes for the integration, and utilize escalation chains from other
teams. Users, schedules, and outgoing webhooks from other teams can also
be included in the escalation chain.

## 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] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-07-21 15:55:57 +01:00

92 lines
3.1 KiB
Python

from apps.public_api.serializers.schedules_base import ScheduleBaseSerializer
from apps.schedules.models import CustomOnCallShift, OnCallScheduleCalendar
from apps.schedules.tasks import (
drop_cached_ical_task,
schedule_notify_about_empty_shifts_in_schedule,
schedule_notify_about_gaps_in_schedule,
)
from common.api_helpers.custom_fields import UsersFilteredByOrganizationField
from common.api_helpers.exceptions import BadRequest
from common.timezones import TimeZoneField
class ScheduleCalendarSerializer(ScheduleBaseSerializer):
time_zone = TimeZoneField(required=True)
shifts = UsersFilteredByOrganizationField(
queryset=CustomOnCallShift.objects,
required=False,
source="custom_on_call_shifts",
)
class Meta:
model = OnCallScheduleCalendar
fields = [
"id",
"team_id",
"name",
"time_zone",
"slack",
"on_call_now",
"shifts",
"ical_url_overrides",
]
extra_kwargs = {
"ical_url_overrides": {"required": False, "allow_null": True},
}
def validate_shifts(self, shifts):
for shift in shifts:
if shift.type == CustomOnCallShift.TYPE_OVERRIDE:
raise BadRequest(detail="Shifts of type override are not supported in this schedule")
return shifts
def to_internal_value(self, data):
if data.get("shifts", []) is None: # terraform case
data["shifts"] = []
result = super().to_internal_value(data)
return result
class ScheduleCalendarUpdateSerializer(ScheduleCalendarSerializer):
time_zone = TimeZoneField(required=False)
class Meta:
model = OnCallScheduleCalendar
fields = [
"id",
"team_id",
"name",
"time_zone",
"slack",
"on_call_now",
"shifts",
"ical_url_overrides",
]
extra_kwargs = {
"name": {"required": False},
"ical_url_overrides": {"required": False, "allow_null": True},
}
def update(self, instance, validated_data):
validated_data = self._correct_validated_data(validated_data)
new_time_zone = validated_data.get("time_zone", instance.time_zone)
new_shifts = validated_data.get("shifts", [])
existing_shifts = instance.custom_on_call_shifts.all()
ical_changed = False
if new_time_zone != instance.time_zone or set(existing_shifts) != set(new_shifts):
ical_changed = True
if (
"ical_url_overrides" in validated_data
and validated_data["ical_url_overrides"] != instance.ical_url_overrides
):
ical_changed = True
if ical_changed:
drop_cached_ical_task.apply_async(
(instance.pk,),
)
schedule_notify_about_empty_shifts_in_schedule.apply_async((instance.pk,))
schedule_notify_about_gaps_in_schedule.apply_async((instance.pk,))
return super().update(instance, validated_data)