broadcasts.py 6.79 KB
Newer Older
1
2
3
__author__ = 'michel'
# -*- coding: utf-8 -*-

4
import datetime, os, urllib, sys
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
5
import time
6
import decimal
7

8
9
from sqlalchemy import Boolean, Column, Date, DateTime, Float, Integer, String, Text, Time, ForeignKey
from libraries.database.database import db
10

11

12
class Model:
13
    def store(self, commit=False):
14
15
16
        curr_db_sessions = db.session.object_session(self)
        curr_db_sessions.add(self)
        #db.session.add(self)
17
18
19
        if commit:
            self.commit()

20
21
    @staticmethod
    def commit():
22
23
        db.session.commit()

24
25
    def _asdict(self):
        return self.__dict__
26

27
28
29
30
31
32
33
34
35
36
37
    @staticmethod
    def recreate_db():
        print("Recreating Database...")
        db.drop_all()
        print("all dropped. creating...")
        db.create_all()
        print("all created. commiting...")
        db.session.commit()
        print("Database recreated!")
        # sys.exit(0)

38

39
# ------------------------------------------------------------------------------------------ #
40
class ScheduleModel(Model):
41
42
43
44
45
46
47
    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


48
49
# ------------------------------------------------------------------------------------------ #
class ScheduleEntryModel(Model):
50
51
52
    # ------------------------------------------------------------------------------------------ #
    @staticmethod
    def upcoming(datefrom=datetime.datetime.now()):
53
        upcomingtracks = ScheduleEntry.query.filter(ScheduleEntry.start > datefrom).all()
54
55
56
57
58
        return upcomingtracks

    # ------------------------------------------------------------------------------------------ #
    @staticmethod
    def select_all():
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
59
60
        all_entries = ScheduleEntry.query.filter().all()

61
        cnt = 0
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
62
        for entry in all_entries:
63
            entry.programme_index = cnt
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
64
65
            entry.entry_start_unix = time.mktime(entry.entry_start.timetuple())
            entry.entry_end_unix = time.mktime(entry.entry_end.timetuple())
66
            cnt = cnt + 1
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
67

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
68
        return all_entries
69
70
71
72

    @staticmethod
    def select_one(playlist_id, entry_num):
        return ScheduleEntry.query.filter(ScheduleEntry.playlist_id == playlist_id, ScheduleEntry.entry_num == entry_num).first()
73
74

    def __str__(self):
75
76
        return "ScheduleEntry starts @ " + ScheduleEntry.entry_start + " and ends @ " + ScheduleEntry.entry_end + " and plays " + ScheduleEntry.source

77

78
# ------------------------------------------------------------------------------------------ #
79
class Schedule(db.Model, ScheduleModel):
80
    """
81
    One specific Schedule for a show on a timeslot
82
    """
83
84
    __tablename__ = 'schedule'
    schedule_id = Column(Integer, primary_key=True, autoincrement=False)
85
86
    show_id = Column(Integer)

87
88
89
90
91
92
93
94
95
96
97
    schedule_start = Column(DateTime, nullable=False)
    schedule_end = Column(DateTime, nullable=False)
    show_name = Column(String(256))
    show_hosts = Column(String(256))
    rtr_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))
98
99
100

    is_repetition = Column(Boolean())

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    playlist_id = Column(Integer)
    timeslot_fallback_id = Column(Integer)
    show_fallback_id = Column(Integer)
    station_fallback_id = Column(Integer)

    @property
    def serialize(self):
        """Return object data in easily serializeable format"""
        return {
            'schedule_id': self.schedule_id,
            'show_id': self.show_id,
            'schedule_start': self.dump_datetime(self.schedule_start),
            'schedule_end': self.dump_datetime(self.schedule_end),
            'show_name': self.show_name,
            'show_hosts': self.show_hosts,
            'is_repetition': self.is_repetition,
            'fallback_playlist': self.fallback_playlist,
            'fallback_pool': self.fallback_pool,
            'station_fallback_pool': self.station_fallback_pool
        }
121
122


123
124
# ------------------------------------------------------------------------------------------ #
class ScheduleEntry(db.Model, ScheduleEntryModel):
125
    """
126
    One schedule can have multiple entries
127
    """
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    __tablename__ = 'schedule_entry'

    playlist_id = Column(Integer, primary_key=True, nullable=False, autoincrement=False)
    entry_num = Column(Integer, primary_key=True, nullable=False, autoincrement=False)
    schedule_id = Column(Integer, ForeignKey("schedule.schedule_id"))
    entry_start = Column(DateTime, nullable=False)
    entry_end = Column(DateTime, nullable=False)
    source = Column(String(256))
    artist = Column(String(256))
    track = Column(String(256))
    albumname = Column(String(256))
    genre = Column(String(256))
    tracknum = Column(String(256))
    cdnum = Column(String(256))
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
142
143
    year = Column(Integer())
    volume = Column(Integer, default=100)
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
144
145
    entry_start_unix = 0
    entry_end_unix = 0
146
    programme_index = -1
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
147

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

#    def __init__(self, playlist_id, entry_num, schedule_id, entry_start, entry_end, source, artist, track, albumname, genre, tracknum, cdnum, year):
#        self.playlist_id = playlist_id
#        self.entry_num = entry_num
#        self.schedule_id = schedule_id
#        self.entry_start = entry_start
#        self.entry_end = entry_end
#        self.source = source
#        self.artist = artist
#        self.track = track
#        self.albumname = albumname
#        self.genre = genre
#        self.tracknum = tracknum
#        self.cdnum = cdnum
#        self.year = year

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    @staticmethod
    def create_from_json(playlist_id, entry_num, schedule_id, entry_start, entry_end, source, artist, track, albumname, genre, tracknum, cdnum, year):
        e = ScheduleEntry()
        e.playlist_id = playlist_id
        e.entry_num = entry_num
        e.schedule_id = schedule_id
        e.entry_start = entry_start
        e.entry_end = entry_end
        e.source = source
        e.artist = artist
        e.track = track
        e.albumname = albumname
        e.genre = genre
        e.tracknum = tracknum
        e.cdnum = cdnum
        e.year = year
180
        return e
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
181
182
183
184
185
186
187
188
189
190

#    @property
#    def serialize(self):
#        """Return object data in easily serializeable format"""
#        return {
#            'schedule_id': self.schedule_id,
#            'entry_start': self.dump_datetime(self.entry_start),
#            'entry_end': self.dump_datetime(self.entry_end),
#            'source': self.source
#        }
191
192
193
194
195
196
197
198
199
200
201

#class Trackservice(db.Model, Model):
#    """
#    Trackservice and Logging
#    """
#    __tablename__ = 'trackservice'
#    id = Column(Integer, primary_key=True)
#    show_id = Column(Integer, ForeignKey("timeslot.id"))



202
#Model.recreate_db()