-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
80 lines (54 loc) · 2.59 KB
/
model.py
File metadata and controls
80 lines (54 loc) · 2.59 KB
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
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
"""User of website."""
__tablename__ = "users"
user_id = db.Column(db.String(50), nullable=False, primary_key=True)
name = db.Column(db.String(100), nullable=False)
created = db.Column(db.DateTime, default=db.func.now())
class Getaway(db.Model):
"""Getaway information."""
__tablename__ = "getaways"
getaway_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
user_id = db.Column(db.String(50), db.ForeignKey('users.user_id'))
track_id = db.Column(db.Integer, db.ForeignKey('songs.track_id'))
location_id = db.Column(db.Integer, db.ForeignKey('locations.location_id'))
created = db.Column(db.DateTime, default=db.func.now())
user = db.relationship("User", backref=db.backref("getaways",
order_by=getaway_id))
song = db.relationship("Song", backref=db.backref("getaways",
order_by=getaway_id))
location = db.relationship("Location", backref=db.backref("getaways",
order_by=getaway_id))
class Song(db.Model):
"""Songs for getaways."""
__tablename__ = "songs"
track_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
seven_digital_track_id = db.Column(db.Integer, nullable=False)
song_name = db.Column(db.String(75), nullable=False)
class Location(db.Model):
"""Locations of getaways."""
__tablename__ = "locations"
location_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
loc_name = db.Column(db.String(100), nullable=False)
pict_URL = db.Column(db.String(200), nullable=False)
################################################################################
def connect_to_db(app, databaseURI='postgresql:///getawaysdb'):
"""Connect the database to Flask app."""
app.config['SQLALCHEMY_DATABASE_URI'] = databaseURI
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
def example_data():
jane = User(name="jane", user_id="123")
sf = Location(loc_name="San Francisco", pict_URL="http://www.picture.com/puppy")
asong = Song(seven_digital_track_id=3326, song_name="happy")
getaway = Getaway(user=jane, location=sf, song=asong)
db.session.add_all([jane, sf, asong, getaway])
db.session.commit()
if __name__ == "__main__":
from server import app
connect_to_db(app)
db.create_all()
print "Connected to DB."