Skip to content
Snippets Groups Projects
Commit f782bc0d authored by David Trattnig's avatar David Trattnig
Browse files

Extended DB layer for date/time operations & more.

parent 4df7b2fc
No related branches found
No related tags found
No related merge requests found
...@@ -217,6 +217,13 @@ class Schedule(DB.Model, AuraDatabaseModel): ...@@ -217,6 +217,13 @@ class Schedule(DB.Model, AuraDatabaseModel):
return all_entries return all_entries
def __str__(self):
"""
String representation of the object.
"""
return "ID#%s [Show: %s, ShowID: %s]" % (str(self.schedule_id), self.show_name, str(self.show_id))
# ------------------------------------------------------------------------------------------ # # ------------------------------------------------------------------------------------------ #
class Playlist(DB.Model, AuraDatabaseModel): class Playlist(DB.Model, AuraDatabaseModel):
__tablename__ = 'playlist' __tablename__ = 'playlist'
...@@ -247,18 +254,84 @@ class Playlist(DB.Model, AuraDatabaseModel): ...@@ -247,18 +254,84 @@ class Playlist(DB.Model, AuraDatabaseModel):
@staticmethod @staticmethod
def select_playlist_for_schedule(datetime, playlist_id): def select_playlist_for_schedule(datetime, playlist_id):
return DB.session.query(Playlist).filter(Playlist.schedule_start == datetime, Playlist.playlist_id == playlist_id).first() """
Retrieves the playlist for the given schedule identified by `start_date` and `playlist_id`
Args:
start_date (datetime): Date and time when the playlist is scheduled
playlist_id (Integer): The ID of the playlist
Returns:
(Playlist): The playlist, if existing for schedule
Raises:
Exception: In case there a inconsistent database state, such es multiple playlists for given date/time.
"""
playlists = DB.session.query(Playlist).filter(Playlist.schedule_start == datetime and Playlist.playlist_id == playlist_id).all()
if playlists and len(playlists) > 1:
raise Exception("Inconsistent Database State: Multiple playlists for given schedule '%s' and playlist id#%d available!" % (str(datetime), playlist_id))
if not playlists:
return None
return playlists[0]
@staticmethod @staticmethod
def select_playlist(playlist_id): def select_playlist(playlist_id):
# TODO Returns multiple playlists - Hence, order playlists also by "latest version". Maybe cleanup others before? """
Retrieves all paylists for that given playlist ID.
Args:
playlist_id (Integer): The ID of the playlist
Returns:
(Array<Playlist>): An array holding the playlists
"""
return DB.session.query(Playlist).filter(Playlist.playlist_id == playlist_id).order_by(Playlist.schedule_start).all() return DB.session.query(Playlist).filter(Playlist.playlist_id == playlist_id).order_by(Playlist.schedule_start).all()
@hybrid_property @hybrid_property
def start_unix(self): def start_unix(self):
"""
Start time of the playlist in UNIX time.
"""
return time.mktime(self.schedule_start.timetuple()) return time.mktime(self.schedule_start.timetuple())
@hybrid_property
def end_unix(self):
"""
End time of the playlist in UNIX time.
"""
return time.mktime(self.schedule_start.timetuple()) + self.duration
@hybrid_property
def duration(self):
"""
Returns the total length of the playlist in seconds.
Returns:
(Integer): Length in seconds
"""
total = 0
for entry in self.entries:
total += entry.duration
return total
def __str__(self):
"""
String representation of the object.
"""
time_start = self.fmt_time(self.start_unix)
time_end = self.fmt_time(self.end_unix)
return "ID#%s [items: %s, start: %s, end: %s]" % (str(self.playlist_id), str(self.entry_count), str(time_start), str(time_end))
def fmt_time(self, timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')
# ------------------------------------------------------------------------------------------ # # ------------------------------------------------------------------------------------------ #
class PlaylistEntry(DB.Model, AuraDatabaseModel): class PlaylistEntry(DB.Model, AuraDatabaseModel):
__tablename__ = 'playlist_entry' __tablename__ = 'playlist_entry'
...@@ -273,6 +346,7 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel): ...@@ -273,6 +346,7 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel):
uri = Column(String(1024)) uri = Column(String(1024))
duration = Column(BigInteger) duration = Column(BigInteger)
filename = Column(String(1024)) filename = Column(String(1024))
entry_start = Column(DateTime)
# relationships # relationships
playlist = relationship("Playlist", uselist=False, back_populates="entries") playlist = relationship("Playlist", uselist=False, back_populates="entries")
...@@ -282,9 +356,13 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel): ...@@ -282,9 +356,13 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel):
def select_playlistentry_for_playlist(artificial_playlist_id, entry_num): def select_playlistentry_for_playlist(artificial_playlist_id, entry_num):
return DB.session.query(PlaylistEntry).filter(PlaylistEntry.artificial_playlist_id == artificial_playlist_id, PlaylistEntry.entry_num == entry_num).first() return DB.session.query(PlaylistEntry).filter(PlaylistEntry.artificial_playlist_id == artificial_playlist_id, PlaylistEntry.entry_num == entry_num).first()
# @hybrid_property @hybrid_property
# def start_unix(self): def start_unix(self):
# return time.mktime(self.entry_start.timetuple()) return time.mktime(self.entry_start.timetuple())
@hybrid_property
def end_unix(self):
return time.mktime(self.entry_start.timetuple()) + self.duration
@hybrid_property @hybrid_property
def volume(self): def volume(self):
...@@ -309,6 +387,18 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel): ...@@ -309,6 +387,18 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel):
return ScheduleEntryType.LIVE_4 return ScheduleEntryType.LIVE_4
def __str__(self):
"""
String representation of the object.
"""
time_start = self.fmt_time(self.start_unix)
time_end = self.fmt_time(self.end_unix)
track = self.filename[-15:]
return "PlaylistEntry ID#%s [%s - %s | %ssec | Track: ...%s]" % (str(self.artificial_id), time_start, time_end, self.duration, track)
def fmt_time(self, timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')
# ------------------------------------------------------------------------------------------ # # ------------------------------------------------------------------------------------------ #
class PlaylistEntryMetaData(DB.Model, AuraDatabaseModel): class PlaylistEntryMetaData(DB.Model, AuraDatabaseModel):
__tablename__ = "playlist_entry_metadata" __tablename__ = "playlist_entry_metadata"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment