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

More serialization methods.

parent 6a4b971b
......@@ -213,6 +213,39 @@ class Schedule(DB.Model, AuraDatabaseModel):
return time.mktime(self.schedule_end.timetuple())
def as_dict(self):
"""
Returns the schedule as a dictionary for serialization.
"""
playlist = self.playlist
return {
"schedule_id": self.schedule_id,
"schedule_start": self.schedule_start.isoformat(),
"schedule_end": self.schedule_end.isoformat(),
"topic": self.topic,
"musicfocus": self.musicfocus,
"funding_category": self.funding_category,
"is_repetition": self.is_repetition,
"category": self.category,
"languages": self.languages,
"comment": self.comment,
"playlist_id": self.playlist_id,
"schedule_fallback_id": self.schedule_fallback_id,
"show_fallback_id": self.show_fallback_id,
"station_fallback_id": self.station_fallback_id,
"show": {
"name": self.show_name,
"type": self.type,
"host": self.show_hosts
},
"playlist": playlist
}
def __str__(self):
"""
String representation of the object.
......@@ -347,6 +380,23 @@ class Playlist(DB.Model, AuraDatabaseModel):
return None
def as_dict(self):
"""
Returns the playlist as a dictionary for serialization.
"""
entries = []
for e in self.entries:
entries.append(e.as_dict())
playlist = {
"playlist_id": self.playlist_id,
"fallback_type": self.fallback_type,
"entry_count": self.entry_count,
"entries": entries
}
return playlist
def __str__(self):
"""
String representation of the object.
......@@ -508,7 +558,7 @@ class TrackService(DB.Model, AuraDatabaseModel):
# Foreign keys
track_start = Column(DateTime)
track_end = Column(DateTime)
track_end = Column(DateTime) # Currently not used, maybe later for timing checks and multi-entry avoidance.
artificial_schedule_id = Column(Integer, ForeignKey("schedule.id"))
artificial_playlist_entry_id = Column(Integer, ForeignKey("playlist_entry.artificial_id"), nullable=True)
single_entry_id = Column(Integer, ForeignKey("single_entry.id"), nullable=True)
......@@ -526,8 +576,8 @@ class TrackService(DB.Model, AuraDatabaseModel):
Initializes a trackservice entry based on a playlist entry.
"""
self.track_start = datetime.datetime.now()
if entry.duration:
self.track_end = self.track_start + datetime.timedelta(seconds=entry.duration)
# if entry.duration:
# self.track_end = self.track_start + datetime.timedelta(seconds=entry.duration)
self.fallback_type = fallback_type
if fallback_type < 4:
......@@ -570,7 +620,7 @@ class TrackService(DB.Model, AuraDatabaseModel):
if self.schedule:
show_info["name"] = self.schedule.show_name
show_info["type"] = self.schedule.type
show_info["host"] = self.show_hosts.show_name
show_info["host"] = self.schedule.show_hosts
elif self.fallback_type == 4:
show_info["name"] = config.get("fallback_show_name")
show_info["type"] = config.get("fallback_show_type")
......@@ -597,7 +647,7 @@ class TrackService(DB.Model, AuraDatabaseModel):
now = datetime.datetime.now()
DB.session.commit() # Required since independend session is used.
track = DB.session.query(TrackService).\
filter(TrackService.track_start <= str(now), TrackService.track_end > str(now)).\
filter(TrackService.track_start <= str(now)).\
order_by(TrackService.track_start.desc()).first()
return track
......
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