Commit 0e71690b authored by David Trattnig's avatar David Trattnig
Browse files

Fixed remote deletion of timeslots. #41

parent 02a549b2
......@@ -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.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment