broadcasts.py 7.6 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
        curr_db_sessions = db.session.object_session(self)
15

16
        print("WARNING: Source of the BAND-AID problem! Multiple sessions, where when something is deleted a transaction is pending")
17
18
19
20
21
22
23
24
25
26
27
28
29
        if curr_db_sessions is None:
            db.session.add(self)
            if commit:
                db.session.commit()
        else:
            curr_db_sessions.add(self)
            if commit:
                curr_db_sessions.commit()

    def delete(self, commit=False):
        curr_db_sessions = db.session.object_session(self)
        curr_db_sessions.delete(self)

30
        if commit:
31
            curr_db_sessions.commit()
32

33
34
    @staticmethod
    def commit():
35
36
        db.session.commit()

37
38
    def _asdict(self):
        return self.__dict__
39

40
41
42
43
44
45
46
47
48
49
50
    @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)

51

52
# ------------------------------------------------------------------------------------------ #
53
class ScheduleModel(Model):
54
55
56
57
58
59
60
    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


61
62
# ------------------------------------------------------------------------------------------ #
class ScheduleEntryModel(Model):
63
64
65
    # ------------------------------------------------------------------------------------------ #
    @staticmethod
    def upcoming(datefrom=datetime.datetime.now()):
66
        upcomingtracks = ScheduleEntry.query.filter(ScheduleEntry.start > datefrom).all()
67
68
69
70
71
        return upcomingtracks

    # ------------------------------------------------------------------------------------------ #
    @staticmethod
    def select_all():
72
73
        # when deleting all entries, and fetching new programmes, the entries are stored and commited in the code.
        # but sqlalchemy thinks somehow it is not commit and returns an empty set
74
75

        print("WARNING: This commit before SELECT is a BAND-AID. There is a transaction pending and not commited")
76
77
78
        db.session.commit()

        # fetching all
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
79
80
        all_entries = ScheduleEntry.query.filter().all()

81
        cnt = 0
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
82
        for entry in all_entries:
83
            entry.programme_index = cnt
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
84
85
            entry.entry_start_unix = time.mktime(entry.entry_start.timetuple())
            entry.entry_end_unix = time.mktime(entry.entry_end.timetuple())
86
            cnt = cnt + 1
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
87

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
88
        return all_entries
89
90
91
92

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

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

97

98
# ------------------------------------------------------------------------------------------ #
99
class Schedule(db.Model, ScheduleModel):
100
    """
101
    One specific Schedule for a show on a timeslot
102
    """
103
104
    __tablename__ = 'schedule'
    schedule_id = Column(Integer, primary_key=True, autoincrement=False)
105
106
    show_id = Column(Integer)

107
108
109
110
111
112
113
114
115
116
117
    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))
118
119
120

    is_repetition = Column(Boolean())

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    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
        }
141
142


143
144
# ------------------------------------------------------------------------------------------ #
class ScheduleEntry(db.Model, ScheduleEntryModel):
145
    """
146
    One schedule can have multiple entries
147
    """
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    __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
162
163
    year = Column(Integer())
    volume = Column(Integer, default=100)
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
164
165
    entry_start_unix = 0
    entry_end_unix = 0
166
    programme_index = -1
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
167

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

#    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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    @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
200
        return e
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
201
202
203
204
205
206
207
208
209
210

#    @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
#        }
211
212
213
214
215
216
217
218
219
220
221

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



222
#Model.recreate_db()