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

4
import datetime, os, urllib, sys
5
6
7
8
import pprint



9
#from modules.web import db
10

11
12
from sqlalchemy import Boolean, Column, Date, DateTime, Float, Integer, String, Text, Time, ForeignKey
from libraries.database.database import db
13

14

15
class Model:
16
17
18
19
20
21
22
23
    def store(self, commit=False):
        db.session.add(self)
        if commit:
            self.commit()

    def commit(self):
        db.session.commit()

24
25


26
# ------------------------------------------------------------------------------------------ #
27
class TimeSlotModel(Model):
28
29
30
31
32
33
34
35
36
    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 pretty_print(self):
        return pprint.pprint(self)

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class TimeSlotEntryModel(Model):
    # ------------------------------------------------------------------------------------------ #
    @staticmethod
    def upcoming(datefrom=datetime.datetime.now()):
        upcomingtracks = TimeSlotEntry.query.filter(TimeSlotEntry.start > datefrom).all()
        return upcomingtracks

    # ------------------------------------------------------------------------------------------ #
    @staticmethod
    def select_all():
        return TimeSlotEntry.query.filter().all()

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

52
# ------------------------------------------------------------------------------------------ #
53
class TimeSlot(db.Model, TimeSlotModel):
54
    """
55
    One specific TimeSlot for a show
56
    """
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    __tablename__ = 'timeslot'
    timeslot_id = Column(Integer, primary_key=True)
    show_id = Column(Integer)

    timeslot_start = Column(DateTime, nullable=False)
    timeslot_end = Column(DateTime, nullable=False)
    show_name = Column(String(255))
    show_hosts = Column(String(255))

    is_repetition = Column(Boolean())

    fallback_playlist = Column(String(255))
    fallback_pool = Column(String(255))
    station_fallback_pool = Column(String(255))

#    entry_start = Column(DateTime, nullable=False)
#    entry_end = Column(DateTime, nullable=False)
#    source = Column(String(255))

76
77
78
79
80
    def print(self):
        return "TimeSlot starts @ "+self.timeslot_start+" and ends @ "+self.timeslot_end+" and plays "+self.source


class TimeSlotEntry(db.Model, TimeSlotEntryModel):
81
82
83
84
    """
    One show can have multiple entries
    """
    #entry_id = Column(Integer, primary_key=True, autoincrement=True)
85
    __tablename__ = 'timeslotentry'
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    timeslot_id = Column(Integer, ForeignKey("timeslot.timeslot_id"))
    entry_start = Column(DateTime, primary_key=True, nullable=False)
    entry_end = Column(DateTime, primary_key=True, nullable=False)
    source = Column(String(255))

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


def recreateDB():
    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)

#recreateDB()