From e469e9309980afa20e9f98ecf5b8915f13ab5bfe Mon Sep 17 00:00:00 2001 From: David Trattnig <david@subsquare.at> Date: Mon, 13 Dec 2021 15:34:22 +0100 Subject: [PATCH] Handle 'None' duration. --- src/models.py | 52 ++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/models.py b/src/models.py index fb9e737..dac2568 100644 --- a/src/models.py +++ b/src/models.py @@ -50,7 +50,7 @@ class PlayLog(db.Model): playlist_id = Column(Integer) timeslot_id = Column(Integer) show_id = Column(Integer) - show_name = Column(String(256)) + show_name = Column(String(256)) log_source = Column(Integer) # The play-out source which this log is coming from (e.g. engine1, engine2) is_synced = Column(Boolean) # Only relevant for main nodes, in a multi-node setup @@ -71,8 +71,10 @@ class PlayLog(db.Model): self.timeslot_id = data.timeslot_id self.show_id = data.show_id self.show_name = data.show_name - self.log_source = data.log_source - self.is_synced = False + self.log_source = data.log_source + self.is_synced = False + if not self.track_duration: + self.track_duration = 0 def save(self): @@ -99,13 +101,13 @@ class PlayLog(db.Model): """ db.session.commit() now = datetime.datetime.now() - + track = db.session.query(PlayLog).\ order_by(PlayLog.track_start.desc()).\ filter(PlayLog.track_start <= str(now)).first() - + return track - + @staticmethod @@ -115,18 +117,18 @@ class PlayLog(db.Model): """ db.session.commit() now = datetime.datetime.now() - + track = PlayLog.select_recent() - - if track: - # Preferably only get playlogs which are known for still being on air - if track.track_start + datetime.timedelta(0, track.track_duration) > now: - return track + if track: # Station fallback may not provide a duration. Take this one, it's better than nothing. if track.track_duration == 0: return track + # Preferably only get playlogs which are known for still being on air + if track.track_start + datetime.timedelta(0, track.track_duration) > now: + return track + return None @@ -160,7 +162,7 @@ class PlayLog(db.Model): order_by(PlayLog.track_start.desc()).\ filter(PlayLog.timeslot_id == timeslot_id) playlogs = result.all() - + return playlogs @@ -174,7 +176,7 @@ class PlayLog(db.Model): if not to_time: to_time = datetime.datetime.now() - def q(page=0, page_size=None): + def q(page=0, page_size=None): query = db.session.query(PlayLog).order_by(PlayLog.track_start.desc()) if isinstance(from_time, datetime.datetime): query = query.filter(PlayLog.track_start >= from_time.isoformat(' ', 'seconds')) @@ -221,7 +223,7 @@ class PlayLog(db.Model): filter(PlayLog.track_start >= str(day), PlayLog.track_start < str(day_plus_one)).\ order_by(PlayLog.track_start.desc()).all() return tracks - + @staticmethod def select_by_range(from_day, to_day): @@ -398,7 +400,7 @@ class ClockInfo(db.Model): # Primary Key log_source = Column(Integer, primary_key=True) # The source this entry was updated from ("1" for engine1, "2" for engine2) - + # Columns log_time = Column(DateTime) current_track = None # Populated live from within `get_info(..)` @@ -426,7 +428,7 @@ class ClockInfo(db.Model): if current_timeslot: self.current_timeslot = json.dumps(current_timeslot.to_dict(), default=str) else: - self.current_timeslot = None + self.current_timeslot = None if next_timeslot: self.next_timeslot = json.dumps(next_timeslot.to_dict(), default=str) else: @@ -452,19 +454,19 @@ class ClockInfo(db.Model): current_track = PlayLog.select_current() planned_playlist_id = -1 playlogs = None - + # Construct the clock `info` object if data: info["log_source"] = data.log_source info["log_time"] = data.log_time - + # Get the track currently playing if current_track: info["current_track"] = track_schema.dump(current_track) - + # Append the missing planned playlist items to the ones played if data.planned_playlist: - info["planned_playlist"] = json.loads(data.planned_playlist) + info["planned_playlist"] = json.loads(data.planned_playlist) else: info["planned_playlist"] = {} @@ -481,14 +483,14 @@ class ClockInfo(db.Model): # Get the actual playlogs of the current timeslot, until now playlog_schema = PlayLogSchema(many=True) playlogs = PlayLog.select_for_timeslot(most_recent_track.timeslot_id) - playlogs.sort(key=lambda track: track.track_start, reverse=False) - info["current_playlogs"] = playlog_schema.dump(playlogs) + playlogs.sort(key=lambda track: track.track_start, reverse=False) + info["current_playlogs"] = playlog_schema.dump(playlogs) if info["current_playlogs"] == None: info["current_playlogs"] = {} # Invalid timeslots (e.g. in fallback scenarios) get a virtual start date of the first fallback track if info["current_timeslot"]["timeslot_id"] == -1: - if playlogs and playlogs[0]: + if playlogs and playlogs[0]: info["current_timeslot"]["timeslot_start"] = playlogs[0].track_start # Get the next timeslot @@ -528,4 +530,4 @@ class ClockInfoSchema(ma.SQLAlchemySchema): "current_playlogs", "current_timeslot", "next_timeslot" - ) \ No newline at end of file + ) \ No newline at end of file -- GitLab