diff --git a/src/models.py b/src/models.py index 43f3497a06bb325ced317b2a087fbc9b5cceb04d..c5f0b1c0026ee04c42dd244139ed32dafdaa06d7 100644 --- a/src/models.py +++ b/src/models.py @@ -120,19 +120,34 @@ class PlayLog(db.Model): def select_for_timeslot(timeslot_id): """ Selects all playlogs which appear to be in the current timeslot. - Limited to 10 records in the last 24hours. + If playlogs without a valid timeslot are queried (-1), then only + the last 50 total playlogs are respected. """ db.session.commit() now = datetime.datetime.now() - before24h = datetime.datetime.now() - datetime.timedelta(hours=24) - - tracks = db.session.query(PlayLog).\ - order_by(PlayLog.track_start.desc()).\ - filter(PlayLog.track_start >= str(before24h)).\ - filter(PlayLog.timeslot_id == timeslot_id).\ - limit(10) + before12h = datetime.datetime.now() - datetime.timedelta(hours=12) + playlogs = None + + # Invalid Timeslot ID + if timeslot_id == -1: + playlogs = [] + result = db.session.query(PlayLog).\ + order_by(PlayLog.track_start.desc()).\ + filter(PlayLog.track_start >= str(before12h)).\ + limit(50) + for playlog in result.all(): + if playlog.timeslot_id != -1: + break + playlogs.append(playlog) + + # Valid Timeslot ID + else: + result = db.session.query(PlayLog).\ + order_by(PlayLog.track_start.desc()).\ + filter(PlayLog.timeslot_id == timeslot_id).\ + playlogs = result.all() - return tracks.all() + return playlogs @staticmethod