-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
59 lines (39 loc) · 1.57 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
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
"""User table in database. Holds user_id, username and password."""
__tablename__ = 'users'
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
username = db.Column(db.String(30), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
class Song(db.Model):
"""Song table in database.
Holds song_id, user_id of the user who created
the song, given song name, if it exists, and song_path.
"""
__tablename__ = 'songs'
song_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=False)
name = db.Column(db.String(40), nullable=True)
song_path = db.Column(db.String(50), nullable=True)
score_path = db.Column(db.String(50), nullable=True)
def connect_to_db(app, db_name='postgresql:///project'):
"""Connect to the database."""
app.config['SQLALCHEMY_DATABASE_URI'] = db_name
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db.app = app
db.init_app(app)
def test_data():
"""Create some sample data for testing.
"""
user = User(username='username', password='password')
song = Song(user_id=1, name='song', song_path='static/user_files/user1/music/song1.wav')
db.session.add(user)
db.session.commit()
db.session.add(song)
db.session.commit()
if __name__ == "__main__":
from server import app
connect_to_db(app)
db.create_all()
print "Connected to DB."