Commit 1005fb28 authored by David Trattnig's avatar David Trattnig
Browse files

Initial commit of track service tables.

parent dec1fd08
......@@ -43,130 +43,76 @@ from modules.base.simpleutil import SimpleUtil
class AuraDatabaseModel():
"""
AuraDataBaseModel.
Holding all tables and relationships for the engine.
"""
logger = None
def __init__(self):
"""
Constructor.
"""
self.logger = logging.getLogger("AuraEngine")
def store(self, add=False, commit=False):
"""
Store to the database
"""
if add:
DB.session.add(self)
if commit:
DB.session.commit()
def delete(self, commit=False):
"""
Delete from the database
"""
DB.session.delete(self)
# current_db_sessions = DB.session.object_session(self)
# current_db_sessions.delete(self)
# return
# DB.session.delete(self)
if commit:
DB.session.commit()
def _asdict(self):
return self.__dict__
@staticmethod
def recreate_db(systemexit = False):
"""
Re-creates the database for developments purposes.
"""
manualschedule = Schedule()
manualschedule.schedule_id = 0
manualschedule.show_name = "Manual Show"
# self.logger.debug("Recreating Database...")
self.logger.debug("Recreating Database...")
DB.drop_all()
# self.logger.debug("all dropped. creating...")
self.logger.debug("All dropped. creating...")
DB.create_all()
# self.logger.debug("inserting manual scheduling possibility and fallback trackservice schedule")
# DB.session.add(manualschedule)
# db.session.add(fallback_trackservice_schedule)
# self.logger.debug("all created. commiting...")
DB.session.commit()
# self.logger.debug("Database recreated!")
self.logger.debug("Database recreated!")
if systemexit:
sys.exit(0)
# ------------------------------------------------------------------------------------------ #
# class Schedule(DB.Model, AuraDatabaseModel):
# """
# One specific Schedule for a show on a timeslot
# """
# __tablename__ = 'schedule'
#
# # primary and foreign keys
# schedule_start = Column(DateTime, primary_key=True)
#
# schedule_end = Column(DateTime)
# schedule_id = Column(Integer) #, primary_key=True, autoincrement=False)
# show_id = Column(Integer) # well, in fact not needed..
# show_name = Column(String(256))
# show_hosts = Column(String(256))
# funding_category = Column(String(256))
# comment = Column(String(512))
# languages = Column(String(256))
# type = Column(String(256))
# category = Column(String(256))
# topic = Column(String(256))
# musicfocus = Column(String(256))
#
# is_repetition = Column(Boolean())
#
# playlist_id = Column(Integer, ForeignKey("playlist.playlist_id"))
# timeslot_fallback_id = Column(Integer)
# show_fallback_id = Column(Integer)
# station_fallback_id = Column(Integer)
#
# playlist = relationship("Playlist", foreign_keys=[playlist_id], lazy="joined")
# # timeslot_fallback = relationship("Playlist", foreign_keys=[timeslot_fallback_id], lazy="joined")
# # show_fallback = relationship("Playlist", foreign_keys=[show_fallback_id], lazy="joined")
# # station_fallback = relationship("Playlist", foreign_keys=[station_fallback_id], lazy="joined")
#
# @staticmethod
# def select_all():
# # fetching all entries
# all_entries = DB.session.query(Schedule).filter().order_by(Schedule.schedule_start).all()
# return all_entries
#
# @staticmethod
# def select_by_id(id):
# entry = DB.session.query(Schedule).filter(Schedule.schedule_id == id).first()
# return entry
# @staticmethod
# def select_act_programme():
# #DB.session.query(Schedule).filter
# # fetching all from today to ..
# today = datetime.date.today()
# all_entries = DB.session.query(Schedule).filter(Schedule.schedule_start >= today).order_by(Schedule.schedule_start).all()
#
# return all_entries
#
#
# @staticmethod
# def drop_the_future(timedelta):
# then = datetime.datetime.now() + timedelta
# SCHEDULES & PLAYLISTS
#
# # is this really necessary?
# future_entries = DB.session.query(Schedule).filter(Schedule.schedule_start > then)
# for e in future_entries:
# e.delete()
# DB.session.commit()
#
# def get_length(self):
# sec1 = int(datetime.datetime.strptime(self.start[0:16].replace(" ", "T"), "%Y-%m-%dT%H:%M").strftime("%s"))
# sec2 = int(datetime.datetime.strptime(self.end[0:16].replace(" ", "T"), "%Y-%m-%dT%H:%M").strftime("%s"))
# len = sec2 - sec1
# return len
#
# # ------------------------------------------------------------------------------------------ #
# def __str__(self):
# return "ScheduleID: #" + str(self.schedule_id) + " Showname: " + self.show_name + " starts @ " + str(self.schedule_start)
# ------------------------------------------------------------------------------------------ #
class Schedule(DB.Model, AuraDatabaseModel):
"""
One specific Schedule for a show on a timeslot
One specific Schedule for a show on a timeslot.
Holding references to playlists and fallback-playlists.
"""
__tablename__ = 'schedule'
......@@ -246,8 +192,10 @@ class Schedule(DB.Model, AuraDatabaseModel):
# ------------------------------------------------------------------------------------------ #
class Playlist(DB.Model, AuraDatabaseModel):
"""
The playlist containing playlist entries.
"""
__tablename__ = 'playlist'
# pk,fk
......@@ -262,9 +210,12 @@ class Playlist(DB.Model, AuraDatabaseModel):
fallback_type = Column(Integer)
entry_count = Column(Integer)
@staticmethod
def select_all():
# fetching all entries
"""
Fetches all entries
"""
all_entries = DB.session.query(Playlist).filter(Playlist.fallback_type == 0).all()
cnt = 0
......@@ -274,6 +225,7 @@ class Playlist(DB.Model, AuraDatabaseModel):
return all_entries
@staticmethod
def select_playlist_for_schedule(datetime, playlist_id):
"""
......@@ -360,9 +312,10 @@ class Playlist(DB.Model, AuraDatabaseModel):
# ------------------------------------------------------------------------------------------ #
class PlaylistEntry(DB.Model, AuraDatabaseModel):
"""
Playlist entries are the individual items of a playlist such as audio files.
"""
__tablename__ = 'playlist_entry'
# primary keys
......@@ -440,8 +393,11 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel):
# ------------------------------------------------------------------------------------------ #
class PlaylistEntryMetaData(DB.Model, AuraDatabaseModel):
"""
Metadata for a playlist entry such as the artist and track name.
"""
__tablename__ = "playlist_entry_metadata"
artificial_id = Column(Integer, primary_key=True)
......@@ -458,6 +414,211 @@ class PlaylistEntryMetaData(DB.Model, AuraDatabaseModel):
return DB.session.query(PlaylistEntry).filter(PlaylistEntryMetaData.artificial_entry_id == artificial_playlistentry_id).first()
#
# TRACK SERVICE
#
class TrackService(DB.Model, AuraDatabaseModel):
"""
TrackService holding track-service items consisting of
"""
__tablename__ = 'trackservice'
trackservice_id = Column(Integer, primary_key=True, autoincrement=True)
schedule_entry_id = Column(Integer, ForeignKey("schedule_entry.id"))
playlist_id = Column(Integer, nullable=False)
entry_num = Column(Integer, nullable=False)
source = Column(String(255), nullable=False)
start = Column(DateTime, nullable=False, default=func.now())
__table_args__ = (
ForeignKeyConstraint(['playlist_id', 'entry_num'], ['schedule_entry.playlist_id', 'schedule_entry.entry_num']),
)
schedule_entry = relationship("ScheduleEntry", primaryjoin="and_(TrackService.playlist_id==ScheduleEntry.playlist_id, TrackService.entry_num==ScheduleEntry.entry_num)", lazy="joined")
schedule = relationship("Schedule", foreign_keys=[schedule_id], lazy="joined")
trackservice_entry = relationship("ScheduleEntry", foreign_keys=[playlist_id, entry_num], lazy="joined")
schedule_entry = relationship("ScheduleEntry", primaryjoin="and_(TrackService.schedule_entry_id==ScheduleEntry.id)", lazy="joined")
@staticmethod
def select_one(trackservice_id):
"""
Select one specific track-service item by ID.
"""
return DB.session.query(TrackService).filter(TrackService.trackservice_id == trackservice_id).first()
@staticmethod
def select_by_day(day):
"""
Select the track-service items for a day.
"""
day_plus_one = day + datetime.timedelta(days=1)
tracks = DB.session.query(TrackService).filter(TrackService.start >= str(day), TrackService.start < str(day_plus_one)).all()
return tracks
@staticmethod
def select_by_range(from_day, to_day):
"""
Selects the track-service items for a day range.
"""
tracks = DB.session.query(TrackService).filter(TrackService.start >= str(from_day),
TrackService.start < str(to_day)).all()
return tracks
def __str__(self):
"""
Convert to String.
"""
return "TrackServiceID: #" + str(self.trackservice_id) + " playlist_id: " + str(self.playlist_id) + " started @ " + str(self.start) + " and played " + self.source
class TrackServiceSchedule(db.Model, AuraDatabaseModel):
"""
Trackservice is tracking every schedule.
"""
__tablename__ = 'trackservice_schedule'
# primary and foreign keys
ts_schedule_id = Column(Integer, primary_key=True, autoincrement=True)
schedule_id = Column(Integer, ForeignKey("schedule.schedule_id"))
schedule = relationship("Schedule", foreign_keys=[schedule_id], lazy="joined")
# ------------------------------------------------------------------------------------------ #
@staticmethod
def select_one(schedule_id):
# damn BAND-AID
# db.session.commit()
return db.session.query(ScheduleEntry).filter(TrackServiceSchedule.schedule_id == schedule_id).first()
class TrackServiceScheduleEntry(db.Model, AuraDatabaseModel):
"""
And a schedule can have multiple entries
"""
__tablename__ = 'trackservice_entry'
# primary and foreign keys. the foreign keys here can be null, because of fallback stuff
ts_entry_id = Column(Integer, primary_key=True, autoincrement=True)
ts_schedule_id = Column(Integer, ForeignKey("trackservice_schedule.ts_schedule_id"), nullable=True)
playlist_id = Column(Integer, nullable=True)
entry_num = Column(Integer, nullable=True)
fallback = Column(Boolean, default=False)
fallback_start = Column(DateTime, nullable=True, default=None)
source = Column(String(256), nullable=True, default=None)
# foreign key definitions
__table_args__ = (
ForeignKeyConstraint(['playlist_id', 'entry_num'], ['schedule_entry.playlist_id', 'schedule_entry.entry_num']),
)
trackservice_schedule = relationship("TrackServiceSchedule", foreign_keys=[ts_schedule_id], lazy="joined")
#trackservice_entry = relationship("ScheduleEntry", foreign_keys=[playlist_id, entry_num], lazy="joined")
trackservice_entry = relationship("ScheduleEntry", primaryjoin="and_(TrackServiceScheduleEntry.playlist_id==ScheduleEntry.playlist_id, TrackServiceScheduleEntry.entry_num==ScheduleEntry.entry_num)" , lazy="joined")
@staticmethod
def select_all():
return db.session.query(TrackServiceScheduleEntry).filter().all()
#
# LEGACY CLASSES
#
# ------------------------------------------------------------------------------------------ #
# class Schedule(DB.Model, AuraDatabaseModel):
# """
# One specific Schedule for a show on a timeslot
# """
# __tablename__ = 'schedule'
#
# # primary and foreign keys
# schedule_start = Column(DateTime, primary_key=True)
#
# schedule_end = Column(DateTime)
# schedule_id = Column(Integer) #, primary_key=True, autoincrement=False)
# show_id = Column(Integer) # well, in fact not needed..
# show_name = Column(String(256))
# show_hosts = Column(String(256))
# funding_category = Column(String(256))
# comment = Column(String(512))
# languages = Column(String(256))
# type = Column(String(256))
# category = Column(String(256))
# topic = Column(String(256))
# musicfocus = Column(String(256))
#
# is_repetition = Column(Boolean())
#
# playlist_id = Column(Integer, ForeignKey("playlist.playlist_id"))
# timeslot_fallback_id = Column(Integer)
# show_fallback_id = Column(Integer)
# station_fallback_id = Column(Integer)
#
# playlist = relationship("Playlist", foreign_keys=[playlist_id], lazy="joined")
# # timeslot_fallback = relationship("Playlist", foreign_keys=[timeslot_fallback_id], lazy="joined")
# # show_fallback = relationship("Playlist", foreign_keys=[show_fallback_id], lazy="joined")
# # station_fallback = relationship("Playlist", foreign_keys=[station_fallback_id], lazy="joined")
#
# @staticmethod
# def select_all():
# # fetching all entries
# all_entries = DB.session.query(Schedule).filter().order_by(Schedule.schedule_start).all()
# return all_entries
#
# @staticmethod
# def select_by_id(id):
# entry = DB.session.query(Schedule).filter(Schedule.schedule_id == id).first()
# return entry
# @staticmethod
# def select_act_programme():
# #DB.session.query(Schedule).filter
# # fetching all from today to ..
# today = datetime.date.today()
# all_entries = DB.session.query(Schedule).filter(Schedule.schedule_start >= today).order_by(Schedule.schedule_start).all()
#
# return all_entries
#
#
# @staticmethod
# def drop_the_future(timedelta):
# then = datetime.datetime.now() + timedelta
#
# # is this really necessary?
# future_entries = DB.session.query(Schedule).filter(Schedule.schedule_start > then)
# for e in future_entries:
# e.delete()
# DB.session.commit()
#
# def get_length(self):
# sec1 = int(datetime.datetime.strptime(self.start[0:16].replace(" ", "T"), "%Y-%m-%dT%H:%M").strftime("%s"))
# sec2 = int(datetime.datetime.strptime(self.end[0:16].replace(" ", "T"), "%Y-%m-%dT%H:%M").strftime("%s"))
# len = sec2 - sec1
# return len
#
# # ------------------------------------------------------------------------------------------ #
# def __str__(self):
# return "ScheduleID: #" + str(self.schedule_id) + " Showname: " + self.show_name + " starts @ " + str(self.schedule_start)
# ------------------------------------------------------------------------------------------ #
# ------------------------------------------------------------------------------------------ #
#class PlaylistEntry(DB.Model, AuraDatabaseModel):
# __tablename__ = 'playlist_entry'
......
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