database.py 6.17 KB
Newer Older
YSelf Tool's avatar
YSelf Tool committed
1
from flask.ext.login import UserMixin
YSelf Tool's avatar
YSelf Tool committed
2

YSelf Tool's avatar
YSelf Tool committed
3 4
from datetime import datetime

YSelf Tool's avatar
YSelf Tool committed
5 6
from shared import db

7 8
from sqlalchemy.orm import relationship, backref

YSelf Tool's avatar
YSelf Tool committed
9 10 11 12 13 14 15
class User(db.Model, UserMixin):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    fullname = db.Column(db.String)
    username = db.Column(db.String, unique=True)
    password = db.Column(db.String)
    roles = db.Column(db.PickleType)
YSelf Tool's avatar
YSelf Tool committed
16 17
    temp_key = db.Column(db.String)
    temp_key_timestamp = db.Column(db.DateTime)
YSelf Tool's avatar
YSelf Tool committed
18 19 20 21 22 23
    
    def __init__(self, fullname, username, password, roles=None):
        self.fullname = fullname
        self.username = username
        self.password = password
        self.roles = roles
YSelf Tool's avatar
YSelf Tool committed
24 25 26 27
        self.temp_key = ""
        self.temp_key_timestamp = datetime(1970, 1, 1, 0, 0)
    
    def __repr__(self):
28
        return "<User(id={}, fullname='{}', username='{}', password='{}', roles={}, temp_key='{}', temp_key_timestamp={})>".format(
YSelf Tool's avatar
YSelf Tool committed
29 30 31 32 33 34 35 36
            self.id, 
            self.fullname, 
            self.username, 
            self.password, 
            self.roles,
            self.temp_key,
            self.temp_key_timestamp
        )
37 38 39 40 41
        
class Event(db.Model):
    __tablename__ = "events"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, unique=True)
42 43
    paused = db.Column(db.Boolean)
    paused_until = db.Column(db.DateTime)
44
    
45
    def __init__(self, name, paused=False):
46
        self.name = name
47 48
        self.paused = paused
        self.paused_until = datetime(1970, 1, 1)
49 50
    
    def __repr__(self):
51 52 53 54 55 56
        return "<Event(id={}, name={}, paused={}, paused_until={})>".format(
            self.id, 
            self.name, 
            self.paused,
            self.paused_until
        )
Robin Sonnabend's avatar
Robin Sonnabend committed
57 58 59
    
    def sorted_topics(self):
        return sorted(self.topics, key=lambda tp: tp.get_index())
YSelf Tool's avatar
YSelf Tool committed
60

Robin Sonnabend's avatar
Robin Sonnabend committed
61
class Topic(db.Model):
Hinrikus Wolf's avatar
Hinrikus Wolf committed
62
    __tablename__ = "topics"
63 64
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, unique=True)
65
    mode = db.Column(db.String)
66
    event_id = db.Column(db.Integer, db.ForeignKey("events.id"), nullable=False)
Robin Sonnabend's avatar
Robin Sonnabend committed
67 68
    index = db.Column(db.Integer)
    
69 70
    event = relationship("Event", backref=backref("topics",order_by=id))
    
71
    def __init__(self, name, mode, event_id):
72
        self.name = name
73
        self.mode = mode
74
        self.event_id = event_id
Robin Sonnabend's avatar
Robin Sonnabend committed
75
        self.index = None
76 77
    
    def __repr__(self):
Robin Sonnabend's avatar
Robin Sonnabend committed
78
        return "<Topic(id={}, name='{}', mode='{}', event_id={}, index={})>".format(
79 80
            self.id, 
            self.name, 
81
            self.mode,
Robin Sonnabend's avatar
Robin Sonnabend committed
82 83
            self.event_id,
            self.index
84
        )
85
    
86 87 88 89 90 91 92 93 94
    def sorted_statements(self):
        statements = [statement for statement in self.statements if not statement.executed]
        if self.mode == "fifo":
            return sorted(statements, key=lambda st: st.id)
        elif self.mode == "balanced":
            return sorted(statements, key=lambda st: st.speaker.count(self))
        else:
            return statements
    
Robin Sonnabend's avatar
Robin Sonnabend committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108
    def swap_topics(self, other):
        other.index, self.index = self.get_index(), other.get_index()

    def get_index(self):
        if self.index == None:
            return self.id
        return self.index
    
    def get_next_index(self):
        topics = self.event.sorted_topics()
        i = topics.index(self) + 1
        if i >= len(topics):
            i = -1
        return topics[i].id
Robin Sonnabend's avatar
Robin Sonnabend committed
109 110 111 112 113 114 115
    
    def get_previous_index(self):
        topics = self.event.sorted_topics()
        i = topics.index(self) - 1
        if i >= len(topics):
            i = 0
        return topics[i].id
116

YSelf Tool's avatar
YSelf Tool committed
117 118 119
class Speaker(db.Model):
    __tablename__ = "speakers"
    id = db.Column(db.Integer, primary_key=True)
120
    name = db.Column(db.String)
121
    number = db.Column(db.Integer)
122 123
    event_id = db.Column(db.Integer, db.ForeignKey("events.id"), nullable=False)
    event = relationship("Event", backref=backref("speakers",order_by=id))
YSelf Tool's avatar
YSelf Tool committed
124
    
125
    def __init__(self, name, number, event_id):
YSelf Tool's avatar
YSelf Tool committed
126
        self.name = name
127
        self.number = number
128
        self.event_id = event_id
YSelf Tool's avatar
YSelf Tool committed
129 130
    
    def __repr__(self):
131
        return "<Speaker(id={}, name='{}', event_id={})>".format(
132 133
            self.id, 
            self.name,
134
            self.event_id
135
        )
136 137 138 139 140 141 142 143 144 145 146 147 148 149
    
    def identifier(self):
        if self.number == 0:
            return self.name
        elif self.name == "":
            return self.number
        else:
            return "{} ({})".format(self.name, self.number)
    
    def count(self, topic):
        return len([statement for statement in self.statements if statement.topic == topic])
    
    def count_active(self, topic):
        return len([statement for statement in self.statements if statement.topic == topic and not statement.executed])
YSelf Tool's avatar
YSelf Tool committed
150 151 152 153 154


class Statement(db.Model):
    __tablename__ = "statements"
    id = db.Column(db.Integer, primary_key=True)
155
    speaker_id = db.Column(db.Integer, db.ForeignKey("speakers.id"), nullable=False)
156
    topic_id = db.Column(db.Integer, db.ForeignKey("topics.id"), nullable=False)
YSelf Tool's avatar
YSelf Tool committed
157 158 159
    insertion_time = db.Column(db.DateTime)
    executed = db.Column(db.Boolean)
    execution_time = db.Column(db.DateTime)
160 161

    speaker = relationship("Speaker", backref=backref("statements",order_by=id))
162
    topic = relationship("Topic", backref=backref("statements",order_by=id))
YSelf Tool's avatar
YSelf Tool committed
163
    
164
    def __init__(self, speaker_id, topic_id, insertion_time=None, executed=False, execution_time=None):
165
        self.speaker_id = speaker_id
166
        self.topic_id = topic_id
YSelf Tool's avatar
YSelf Tool committed
167 168 169
        self.insertion_time = insertion_time or datetime.now()
        self.executed = executed
        self.execution_time = execution_time or datetime.now()
YSelf Tool's avatar
YSelf Tool committed
170 171
    
    def __repr__(self):
172
        return "<Statement(id={}, speaker={}, topic_id={}, insertion_time={}, executed={}, execution_time={})>".format(
YSelf Tool's avatar
YSelf Tool committed
173 174
            self.id, 
            self.speaker,
175
            self.topic_id,
YSelf Tool's avatar
YSelf Tool committed
176 177 178 179 180 181 182 183 184 185 186 187
            self.insertion_time,
            self.executed,
            self.execution_time
        )
    
    def done(self):
        if self.executed:
            return False
        self.executed = True
        self.execution_time = datetime.now()
        return True
    
188 189 190 191 192
    def undo(self):
        if not self.executed:
            return False
        self.executed = False
        self.execution_time = datetime(1970, 1, 1)