From b8c32d53a360f9814f9fc490280a15e74c678956 Mon Sep 17 00:00:00 2001
From: David Trattnig <david@subsquare.at>
Date: Fri, 21 Jan 2022 12:28:10 +0100
Subject: [PATCH] Datetime conversions for SQLite. #89

---
 src/base/utils.py           | 22 ++++++++++++++++------
 src/scheduling/models.py    |  6 +++++-
 src/scheduling/programme.py |  7 ++++---
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/src/base/utils.py b/src/base/utils.py
index 8bba00d7..3e9c0179 100644
--- a/src/base/utils.py
+++ b/src/base/utils.py
@@ -22,7 +22,7 @@ import time
 
 from enum import Enum
 
-                                                                    
+
 
 class SimpleUtil:
     """
@@ -40,7 +40,7 @@ class SimpleUtil:
             data (dict):    The dictionary
 
         Returns:
-            (dict): 
+            (dict):
         """
         for key, value in list(data.items()):
             if value is None:
@@ -50,6 +50,16 @@ class SimpleUtil:
         return data
 
 
+    @staticmethod
+    def to_datetime(datetime_str:str):
+        """
+        Converts a timezone aware date-time string into `datetime`.
+        """
+        if datetime_str:
+            return datetime.datetime.fromisoformat(datetime_str)
+        return None
+
+
     @staticmethod
     def fmt_time(timestamp):
         """
@@ -75,7 +85,7 @@ class SimpleUtil:
         Returns:
             (Float): seconds
         """
-        return float(nanoseconds / 1000000000) 
+        return float(nanoseconds / 1000000000)
 
 
     @staticmethod
@@ -97,11 +107,11 @@ class SimpleUtil:
         """
         Transforms the given `datetime` into a UNIX epoch timestamp.
         If no parameter is passed, the current timestamp is returned.
-        
+
         Args:
             (Datetime) date_and_time:    the date and time to transform.
 
-        Returns: 
+        Returns:
             (Integer): timestamp in seconds.
         """
         if not date_and_time:
@@ -180,7 +190,7 @@ class SimpleUtil:
         Creates a cyan version of the given text.
         """
         return TerminalColors.CYAN.value + text + TerminalColors.ENDC.value
-        
+
 
 
 class TerminalColors(Enum):
diff --git a/src/scheduling/models.py b/src/scheduling/models.py
index e8c9dc5b..4b2ccffa 100644
--- a/src/scheduling/models.py
+++ b/src/scheduling/models.py
@@ -402,7 +402,7 @@ class Playlist(DB.Model, AuraDatabaseModel):
 
 
     @staticmethod
-    def select_playlist_for_timeslot(start_date, playlist_id):
+    def select_playlist_for_timeslot(start_date:datetime.datetime, playlist_id):
         """
         Retrieves the playlist for the given timeslot identified by `start_date` and `playlist_id`
 
@@ -698,3 +698,7 @@ class PlaylistEntryMetaData(DB.Model, AuraDatabaseModel):
                 .filter(PlaylistEntryMetaData.artificial_entry_id == artificial_playlistentry_id)
                 .first()
             )
+
+
+
+Base.metadata.create_all(engine)
\ No newline at end of file
diff --git a/src/scheduling/programme.py b/src/scheduling/programme.py
index a7a61721..c64af2a2 100644
--- a/src/scheduling/programme.py
+++ b/src/scheduling/programme.py
@@ -358,7 +358,8 @@ class ProgrammeStore():
         Args:
             timeslot (Timeslot):    The timeslot
         """
-        timeslot_db = Timeslot.for_datetime(timeslot["start"])
+        timeslot_start = SU.to_datetime(timeslot["start"])
+        timeslot_db = Timeslot.for_datetime(timeslot_start)
         havetoadd = False
 
         if not timeslot_db:
@@ -368,8 +369,8 @@ class ProgrammeStore():
 
         timeslot_db.show_id = timeslot["show_id"]
         timeslot_db.timeslot_id = timeslot["timeslot_id"]
-        timeslot_db.timeslot_start = timeslot["start"]
-        timeslot_db.timeslot_end = timeslot["end"]
+        timeslot_db.timeslot_start = SU.to_datetime(timeslot["start"])
+        timeslot_db.timeslot_end = SU.to_datetime(timeslot["end"])
         timeslot_db.show_name = timeslot["show_name"]
         timeslot_db.show_hosts = timeslot["show_hosts"]
         timeslot_db.is_repetition = timeslot["is_repetition"]
-- 
GitLab