database.py 4.06 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 28 29 30 31 32 33 34 35 36
        self.temp_key = ""
        self.temp_key_timestamp = datetime(1970, 1, 1, 0, 0)
    
    def __repr__(self):
        return "<User(id={}, fullname='{}', username='{}', password'{}', roles{}, temp_key='{}', temp_key_timestamp={})>".format(
            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)
Robin Sonnabend's avatar
Robin Sonnabend committed
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)
YSelf Tool's avatar
YSelf Tool committed
48

Robin Sonnabend's avatar
Robin Sonnabend committed
49
class Topic(db.Model):
Hinrikus Wolf's avatar
Hinrikus Wolf committed
50
    __tablename__ = "topics"
YSelf Tool's avatar
YSelf Tool committed
51 52
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, unique=True)
53
    mode = db.Column(db.String)
54 55 56
    event_id = db.Column(db.Integer, db.ForeignKey("events.id"), nullable=False)
    event = relationship("Event", backref=backref("topics",order_by=id))
    
YSelf Tool's avatar
YSelf Tool committed
57
    
Robin Sonnabend's avatar
Robin Sonnabend committed
58
    def __init__(self, name, mode, event_id):
YSelf Tool's avatar
YSelf Tool committed
59
        self.name = name
60
        self.mode = mode
Robin Sonnabend's avatar
Robin Sonnabend committed
61
        self.event_id = event_id
YSelf Tool's avatar
YSelf Tool committed
62 63
    
    def __repr__(self):
Robin Sonnabend's avatar
Robin Sonnabend committed
64
        return "<Topic(id={}, name='{}', mode='{}', event_id={})>".format(
YSelf Tool's avatar
YSelf Tool committed
65 66
            self.id, 
            self.name, 
Robin Sonnabend's avatar
Robin Sonnabend committed
67 68
            self.mode,
            self.event_id
YSelf Tool's avatar
YSelf Tool committed
69
        )
70
    
YSelf Tool's avatar
YSelf Tool committed
71

YSelf Tool's avatar
YSelf Tool committed
72 73 74
class Speaker(db.Model):
    __tablename__ = "speakers"
    id = db.Column(db.Integer, primary_key=True)
YSelf Tool's avatar
YSelf Tool committed
75
    name = db.Column(db.String)
76 77
    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
78
    
Robin Sonnabend's avatar
Robin Sonnabend committed
79
    def __init__(self, name, event_id):
YSelf Tool's avatar
YSelf Tool committed
80
        self.name = name
Robin Sonnabend's avatar
Robin Sonnabend committed
81
        self.event_id = event_id
YSelf Tool's avatar
YSelf Tool committed
82 83
    
    def __repr__(self):
Robin Sonnabend's avatar
Robin Sonnabend committed
84
        return "<Speaker(id={}, name='{}', event_id={})>".format(
YSelf Tool's avatar
YSelf Tool committed
85 86
            self.id, 
            self.name,
Robin Sonnabend's avatar
Robin Sonnabend committed
87
            self.event_id
YSelf Tool's avatar
YSelf Tool committed
88
        )
YSelf Tool's avatar
YSelf Tool committed
89 90 91 92 93


class Statement(db.Model):
    __tablename__ = "statements"
    id = db.Column(db.Integer, primary_key=True)
94 95
    speaker_id = db.Column(db.Integer, db.ForeignKey("speakers.id"), nullable=False)
    event_id = db.Column(db.Integer, db.ForeignKey("events.id"), nullable=False)
YSelf Tool's avatar
YSelf Tool committed
96 97 98
    insertion_time = db.Column(db.DateTime)
    executed = db.Column(db.Boolean)
    execution_time = db.Column(db.DateTime)
Robin Sonnabend's avatar
Robin Sonnabend committed
99 100 101

    speaker = relationship("Speaker", backref=backref("statements",order_by=id))
    event = relationship("Event", backref=backref("statements",order_by=id))
YSelf Tool's avatar
YSelf Tool committed
102
    
Robin Sonnabend's avatar
Robin Sonnabend committed
103 104 105
    def __init__(self, speaker_id, event_id, insertion_time=None, executed=False, execution_time=None):
        self.speaker_id = speaker_id
        self.event_id = event_id
YSelf Tool's avatar
YSelf Tool committed
106 107 108
        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
109 110
    
    def __repr__(self):
Robin Sonnabend's avatar
Robin Sonnabend committed
111
        return "<Statement(id={}, speaker={}, event_id={}, topic_id={}, insertion_time={}, executed={}, execution_time={})>".format(
YSelf Tool's avatar
YSelf Tool committed
112 113
            self.id, 
            self.speaker,
Robin Sonnabend's avatar
Robin Sonnabend committed
114 115
            self.event_id,
            self.topic_id,
YSelf Tool's avatar
YSelf Tool committed
116 117 118 119 120 121 122 123 124 125 126 127
            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