-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
82 lines (54 loc) · 2.26 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
"""Models for movie ratings app."""
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
db = SQLAlchemy()
class User(db.Model):
"""A user."""
__tablename__ = 'users'
user_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
email = db.Column(db.String, unique=True)
password = db.Column(db.String)
ratings = db.relationship("Rating", back_populates="user")
def __repr__(self):
return f'<User user_id={self.user_id} email={self.email}>'
class Movie(db.Model):
"""A movie."""
__tablename__ = 'movies'
movie_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
title = db.Column(db.String)
overview = db.Column(db.Text)
release_date = db.Column(db.DateTime) ##Remember to fix datetime for spirited away
poster_path = db.Column(db.String)
ratings = db.relationship("Rating", back_populates="movie")
def __repr__(self):
return f'<Movie movie_id={self.movie_id} title={self.title}>'
class Rating(db.Model):
"""A rating."""
__tablename__ = 'ratings'
rating_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
score = db.Column(db.Integer)
movie_id = db.Column(db.Integer, db.ForeignKey('movies.movie_id'))
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
movie = db.relationship("Movie", back_populates="ratings")
user = db.relationship("User", back_populates="ratings")
def __repr__(self):
return f'<Rating rating_id={self.rating_id} score={self.score} movie_title={self.movie.title}>'
def connect_to_db(flask_app, db_uri="postgresql:///ratings", echo=False):
flask_app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
flask_app.config["SQLALCHEMY_ECHO"] = echo
flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = flask_app
db.init_app(flask_app)
print("Connected to the db!")
if __name__ == "__main__":
from server import app
# Call connect_to_db(app, echo=False) if your program output gets
# too annoying; this will tell SQLAlchemy not to print out every
# query it executes.
connect_to_db(app)