-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
118 lines (71 loc) · 3.42 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
111
112
113
114
115
116
117
118
"""Models and database functions for Final project."""
from flask_sqlalchemy import SQLAlchemy
import time
import calendar
# This is the connection to the SQLite database; we're getting this through
# the Flask-SQLAlchemy helper library. On this, we can find the `session`
# object, where we do most of our interactions (like committing, etc.)
db = SQLAlchemy()
##############################################################################
# Model definitions
class Recording(db.Model):
"""Recording for alarm"""
__tablename__ = "recordings"
recording_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
question_id = db.Column(db.Integer, db.ForeignKey('questions.question_id'))
file_path = db.Column(db.String(50))
file_size = db.Column(db.Integer)
user = db.relationship("User",
backref=db.backref("recordings", order_by=recording_id))
question = db.relationship("Question",
backref=db.backref("recordings", order_by=recording_id))
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Recording recording_id=%s wav_file=%s>" % (self.recording_id, self.blob)
#
class User(db.Model):
"""User of Chime website."""
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
email = db.Column(db.String(64), nullable=True, unique=True)
password = db.Column(db.String(64), nullable=True)
first_name = db.Column(db.String(80))
last_name = db.Column(db.String(80))
def __repr__(self):
"""Provide helpful representation when printed."""
return "<User user_id=%s email=%s>" % (self.user_id, self.email)
class Category(db.Model):
"""Categories table"""
__tablename__ = "categories"
category_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
category_name = db.Column(db.String(30), unique=True)
category_description = db.Column(db.String(300))
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Category category_id=%s category_name=%s>" % (self.category_id, self.category_name)
class Question(db.Model):
"""Questions table"""
__tablename__ = "questions"
question_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
question_text = db.Column(db.String(500))
category_id = db.Column(db.Integer, db.ForeignKey('categories.category_id'))
category = db.relationship("Category",
backref=db.backref("questions", order_by=question_id))
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Question question_id=%s question_text=%s>" % (self.question_id, self.question_text)
##############################################################################
# Helper functions
def connect_to_db(app):
"""Connect the database to our Flask app."""
# Configure to use our SQLite database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///chime2.db'
db.app = app
db.init_app(app)
if __name__ == "__main__":
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
from server import app
connect_to_db(app)
print "Connected to DB."