From 0e71690bb87137ded18a47b0a2693c56f51fdccc Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Thu, 19 Nov 2020 13:25:02 +0100
Subject: [PATCH] Fixed remote deletion of timeslots. #41

---
 src/scheduling/programme.py | 68 +++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 30 deletions(-)

diff --git a/src/scheduling/programme.py b/src/scheduling/programme.py
index f4b24dc3..eea9a5c4 100644
--- a/src/scheduling/programme.py
+++ b/src/scheduling/programme.py
@@ -283,8 +283,6 @@ class ProgrammePersistence(threading.Thread):
             Timeslot ([]):  An arrar of retrieved timeslots passed via `self.queue`
         """
         result = []
-        now_unix = SU.timestamp()
-        scheduling_window_start = self.config.get("scheduling_window_start")
 
         try:
             fetched_timeslot_data = self.api_fetcher.fetch()
@@ -296,30 +294,7 @@ class ProgrammePersistence(threading.Thread):
                 return
             
             # Check if existing timeslots have been deleted
-            local_timeslots = Timeslot.get_timeslots(datetime.now())
-            for local_timeslot in local_timeslots:
-
-                # Only allow deletion of timeslots which are deleted before the start of the scheduling window
-                if local_timeslot.start_unix > now_unix:
-                    if (local_timeslot.start_unix - scheduling_window_start) > now_unix:
-
-                        # Filter the local timeslot from the fetched ones
-                        existing_timeslot = list(filter(lambda new_timeslot: \
-                            new_timeslot["timeslot_id"] == local_timeslot.timeslot_id, fetched_timeslot_data))
-                        
-                        if existing_timeslot:
-                            # self.logger.debug("Timeslot #%s is still existing remotely!" % (local_timeslot.timeslot_id))
-                            pass
-                        else:
-                            self.logger.info("Timeslot #%s has been deleted remotely, hence also delete it locally [%s]" % \
-                                (local_timeslot.timeslot_id, str(local_timeslot)))
-                            local_timeslot.delete(commit=True)
-                            self.logger.info("Deleted local timeslot #%s from database" % local_timeslot.timeslot_id)
-
-                    else:
-                        msg = "Timeslot #%s has been deleted remotely. Since the scheduling window has already started, it won't be deleted locally." % \
-                            local_timeslot.timeslot_id
-                        self.logger.error(SU.red(msg))
+            self.update_deleted_timeslots(fetched_timeslot_data)
 
             # Process fetched timeslots    
             for timeslot in fetched_timeslot_data:
@@ -358,6 +333,43 @@ class ProgrammePersistence(threading.Thread):
 
 
 
+    def update_deleted_timeslots(self, fetched_timeslot_data):
+        """
+        Checks if some timeslot has been deleted remotely, so delete it locally too.
+        
+        Attention: This method has no effect if only a single timeslot got deleted, 
+        because this could simply indicate a issue with the API/Steering, since that 
+        means no data got retrieved.
+
+        Args:
+            fetched_timeslot_data ([dict]): List of timeslot dictionaries from the API
+        """
+        now_unix = SU.timestamp()
+        scheduling_window_start = self.config.get("scheduling_window_start")
+
+        local_timeslots = Timeslot.get_timeslots(datetime.now())
+        for local_timeslot in local_timeslots:
+
+            # Ignore timeslots which have already started
+            if local_timeslot.start_unix > now_unix:
+
+                # Filter the local timeslot from the fetched ones
+                existing_remotely = list(filter(lambda new_timeslot: \
+                    new_timeslot["timeslot_id"] == local_timeslot.timeslot_id, fetched_timeslot_data))
+                
+                if not existing_remotely:
+                    # Only allow deletion of timeslots which are deleted before the start of the scheduling window
+                    if (local_timeslot.start_unix - scheduling_window_start) > now_unix:                            
+                        self.logger.info("Timeslot #%s has been deleted remotely, hence also delete it locally too [%s]" % \
+                            (local_timeslot.timeslot_id, str(local_timeslot)))
+                        local_timeslot.delete(commit=True)
+                        self.logger.info("Remotely deleted timeslot #%s from local database" % local_timeslot.timeslot_id)
+                    else:
+                        msg = "Timeslot #%s has been deleted remotely. Since the scheduling window has already started, it won't be deleted locally." % local_timeslot.timeslot_id
+                        self.logger.error(SU.red(msg))
+
+
+
     def store_timeslot(self, timeslot):
         """
         Stores the given timeslot to the database.
@@ -373,7 +385,6 @@ class ProgrammePersistence(threading.Thread):
             timeslot_db = Timeslot()
             havetoadd = True
 
-
         timeslot_db.show_id = timeslot["show_id"]
         timeslot_db.timeslot_id = timeslot["timeslot_id"]
         timeslot_db.timeslot_start = timeslot["start"]
@@ -387,19 +398,16 @@ class ProgrammePersistence(threading.Thread):
         timeslot_db.category = timeslot["show_categories"]
         timeslot_db.topic = timeslot["show_topics"]
         timeslot_db.musicfocus = timeslot["show_musicfocus"]
-
         timeslot_db.playlist_id = timeslot["playlist_id"]
         timeslot_db.schedule_fallback_id = timeslot["schedule_fallback_id"]
         timeslot_db.show_fallback_id = timeslot["show_fallback_id"]
         timeslot_db.station_fallback_id = timeslot["station_fallback_id"]
 
         timeslot_db.store(add=havetoadd, commit=True)
-
         return timeslot_db
 
 
 
-    # def store_playlist(self, timeslot_db, playlist_id, fetched_playlist, fallbackplaylist_type=0):
     def store_playlist(self, timeslot_db, playlist_id, fetched_playlist):
         """
         Stores the Playlist to the database.
-- 
GitLab