From 226713d835cd89bacf523c15c3195dbb43958d30 Mon Sep 17 00:00:00 2001
From: Chris Pastl <chris@crispybits.app>
Date: Mon, 15 Apr 2024 23:12:23 +0200
Subject: [PATCH] Refactor: replace func with APIDayScheduleViewSet for
 /program/y/m/d

---
 program/views.py | 58 ++++++++++++++++++++++++++----------------------
 steering/urls.py |  5 +++--
 2 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/program/views.py b/program/views.py
index 79406df5..41fc0f7e 100644
--- a/program/views.py
+++ b/program/views.py
@@ -143,32 +143,38 @@ 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",
-    )
+class APIDayScheduleViewSet(
+    mixins.ListModelMixin,
+    viewsets.GenericViewSet,
+):
+    queryset = Schedule.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(
diff --git a/steering/urls.py b/steering/urls.py
index aa1ca7e6..f77382d4 100644
--- a/steering/urls.py
+++ b/steering/urls.py
@@ -43,7 +43,7 @@ from program.views import (
     APITopicViewSet,
     APITypeViewSet,
     APIUserViewSet,
-    json_day_schedule,
+    APIDayScheduleViewSet,
     APIPlayoutViewSet
 )
 
@@ -69,11 +69,12 @@ router.register(r"images", APIImageViewSet, basename="image")
 router.register(r"settings", APIRadioSettingsViewSet, basename="settings")
 router.register(r"playout", APIPlayoutViewSet, basename="playout")
 router.register(r"program/week", APIPlayoutViewSet, basename="program/week")
+# router.register(r"program/<int:year>/<int:month>/<int:day>", APIDayScheduleViewSet, basename="program")
 
 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/",
-- 
GitLab