Skip to content
Snippets Groups Projects
Commit fd5686a7 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

Merge branch 'refactor-playout-endpoint-210' into 'main'

Refactor endpoint /program/year/month/day

See merge request !41
parents 9751a41a 00bad372
No related branches found
No related tags found
1 merge request!41Refactor endpoint /program/year/month/day
Pipeline #7897 passed
......@@ -143,34 +143,65 @@ def gap_entry(*, gap_start: datetime, gap_end: datetime) -> dict:
}
def json_day_schedule(request, year=None, month=None, day=None):
# datetime.combine returns a timezone naive datetime object
if year is None and month is None and day is None:
start = timezone.make_aware(datetime.combine(timezone.now(), time(0, 0)))
else:
start = timezone.make_aware(datetime.combine(date(year, month, day), time(0, 0)))
end = start + timedelta(hours=24)
timeslots = get_timerange_timeslots(start, end).select_related("schedule")
schedule = []
for ts in timeslots:
entry = {
"start": ts.start.strftime("%Y-%m-%d_%H:%M:%S"),
"end": ts.end.strftime("%Y-%m-%d_%H:%M:%S"),
"title": ts.show.name,
"id": ts.show.id,
}
schedule.append(entry)
return HttpResponse(
json.dumps(schedule, ensure_ascii=False).encode("utf8"),
content_type="application/json; charset=utf-8",
)
@extend_schema_view(
create=extend_schema(summary="List schedule for a specific date."),
list=extend_schema(
summary="List schedule for a specific date.",
description=(
"Returns a list of the schedule for a specific date."
"Expects parameters `year` (int), `month` (int), and `day` (int) as url components."
"e.g. /program/2024/01/31/"
),
),
)
class APIDayScheduleViewSet(
mixins.ListModelMixin,
viewsets.GenericViewSet,
):
queryset = TimeSlot.objects.all()
def list(self, request, year=None, month=None, day=None):
# datetime.combine returns a timezone naive datetime object
if year is None and month is None and day is None:
start = timezone.make_aware(datetime.combine(timezone.now(), time(0, 0)))
else:
start = timezone.make_aware(datetime.combine(date(year, month, day), time(0, 0)))
end = start + timedelta(hours=24)
timeslots = get_timerange_timeslots(start, end).select_related("schedule")
schedule = []
for ts in timeslots:
entry = {
"start": ts.start.strftime("%Y-%m-%d_%H:%M:%S"),
"end": ts.end.strftime("%Y-%m-%d_%H:%M:%S"),
"title": ts.show.name,
"id": ts.show.id,
}
schedule.append(entry)
return HttpResponse(
json.dumps(schedule, ensure_ascii=False).encode("utf8"),
content_type="application/json; charset=utf-8",
)
@extend_schema_view(
create=extend_schema(summary="List playout."),
list=extend_schema(
summary="List scheduled playout.",
description=(
"Returns a list of the scheduled playout."
"Expects parameters `start` (date), `end` (date), and `includeVirtual` (boolean)."
"- `start` is today by default."
"- `end` is one week after the start date by default."
"- `includeVirtual` is false by default."
"The schedule will include virtual timeslots to fill unscheduled gaps if requested."
),
),
)
class APIPlayoutViewSet(
mixins.ListModelMixin,
viewsets.GenericViewSet,
......@@ -180,15 +211,6 @@ class APIPlayoutViewSet(
def list(self, request, *args, **kwargs):
"""
Return a JSON representation of the scheduled playout.
Expects GET parameters `start` (date), `end` (date), and `includeVirtual` (boolean).
- `start` is today by default.
- `end` is one week after the start date by default.
- `includeVirtual` is false by default.
The schedule will include virtual timeslots to fill unscheduled gaps if requested.
Called by
- engine (playout) to retrieve timeslots within a given timerange
- internal calendar to retrieve all timeslots for a week
......
......@@ -43,7 +43,7 @@ from program.views import (
APITopicViewSet,
APITypeViewSet,
APIUserViewSet,
json_day_schedule,
APIDayScheduleViewSet,
APIPlayoutViewSet
)
......@@ -73,7 +73,7 @@ router.register(r"program/week", APIPlayoutViewSet, basename="program/week")
urlpatterns = [
path("openid/", include("oidc_provider.urls", namespace="oidc_provider")),
path("api/v1/", include(router.urls)),
path("api/v1/program/<int:year>/<int:month>/<int:day>/", json_day_schedule),
path("api/v1/program/<int:year>/<int:month>/<int:day>/", APIDayScheduleViewSet.as_view({'get': 'list'}), name="program"),
path("api/v1/schema/", SpectacularAPIView.as_view(), name="schema"),
path(
"api/v1/schema/swagger-ui/",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment