database.py 5.7 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 44 45 46 47
    
    def __init__(self, name):
        self.name = name
    
    def __repr__(self):
        return "<Event(id={}, name={})>".format(self.id, self.name)
Robin Sonnabend's avatar
Robin Sonnabend committed
48 49 50
    
    def sorted_topics(self):
        return sorted(self.topics, key=lambda tp: tp.get_index())
YSelf Tool's avatar
YSelf Tool committed
51

Robin Sonnabend's avatar
Robin Sonnabend committed
52
class Topic(db.Model):
Hinrikus Wolf's avatar
Hinrikus Wolf committed
53
    __tablename__ = "topics"
54 55
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, unique=True)
56
    mode = db.Column(db.String)
57
    event_id = db.Column(db.Integer, db.ForeignKey("events.id"), nullable=False)
Robin Sonnabend's avatar
Robin Sonnabend committed
58 59
    index = db.Column(db.Integer)
    
60 61
    event = relationship("Event", backref=backref("topics",order_by=id))
    
62
    def __init__(self, name, mode, event_id):
63
        self.name = name
64
        self.mode = mode
65
        self.event_id = event_id
Robin Sonnabend's avatar
Robin Sonnabend committed
66
        self.index = None
67 68
    
    def __repr__(self):
Robin Sonnabend's avatar
Robin Sonnabend committed
69
        return "<Topic(id={}, name='{}', mode='{}', event_id={}, index={})>".format(
70 71
            self.id, 
            self.name, 
72
            self.mode,
Robin Sonnabend's avatar
Robin Sonnabend committed
73 74
            self.event_id,
            self.index
75
        )
76
    
77 78 79 80 81 82 83 84 85
    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
86 87 88 89 90 91 92 93 94 95 96 97 98 99
    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
100

YSelf Tool's avatar
YSelf Tool committed
101 102 103
class Speaker(db.Model):
    __tablename__ = "speakers"
    id = db.Column(db.Integer, primary_key=True)
104
    name = db.Column(db.String)
105
    number = db.Column(db.Integer)
106 107
    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
108
    
109
    def __init__(self, name, number, event_id):
YSelf Tool's avatar
YSelf Tool committed
110
        self.name = name
111
        self.number = number
112
        self.event_id = event_id
YSelf Tool's avatar
YSelf Tool committed
113 114
    
    def __repr__(self):
115
        return "<Speaker(id={}, name='{}', event_id={})>".format(
116 117
            self.id, 
            self.name,
118
            self.event_id
119
        )
120 121 122 123 124 125 126 127 128 129 130 131 132 133
    
    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
134 135 136 137 138


class Statement(db.Model):
    __tablename__ = "statements"
    id = db.Column(db.Integer, primary_key=True)
139
    speaker_id = db.Column(db.Integer, db.ForeignKey("speakers.id"), nullable=False)
140
    topic_id = db.Column(db.Integer, db.ForeignKey("topics.id"), nullable=False)
YSelf Tool's avatar
YSelf Tool committed
141 142 143
    insertion_time = db.Column(db.DateTime)
    executed = db.Column(db.Boolean)
    execution_time = db.Column(db.DateTime)
144 145

    speaker = relationship("Speaker", backref=backref("statements",order_by=id))
146
    topic = relationship("Topic", backref=backref("statements",order_by=id))
YSelf Tool's avatar
YSelf Tool committed
147
    
148
    def __init__(self, speaker_id, topic_id, insertion_time=None, executed=False, execution_time=None):
149
        self.speaker_id = speaker_id
150
        self.topic_id = topic_id
YSelf Tool's avatar
YSelf Tool committed
151 152 153
        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
154 155
    
    def __repr__(self):
156
        return "<Statement(id={}, speaker={}, topic_id={}, insertion_time={}, executed={}, execution_time={})>".format(
YSelf Tool's avatar
YSelf Tool committed
157 158
            self.id, 
            self.speaker,
159
            self.topic_id,
YSelf Tool's avatar
YSelf Tool committed
160 161 162 163 164 165 166 167 168 169 170 171
            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
    
172 173 174 175 176
    def undo(self):
        if not self.executed:
            return False
        self.executed = False
        self.execution_time = datetime(1970, 1, 1)