-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
80 lines (55 loc) · 2.29 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
"""Models for movie ratings app."""
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import backref
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 = a list of Rating objects
def __repr__(self):
return f'<User user_id={self.user_id} email={self.email}>'
class Movie(db.Model):
"""A user."""
__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)
poster_path = db.Column(db.String)
# ratings = a list of Rating objects
def __repr__(self):
return f'<Movie movie_id={self.movie_id} title={self.title} overview={self.overview} release_date={self.release_date} poster_path = {self.poster_path}>'
class Rating(db.Model):
"""A user."""
__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",backref= "ratings")
user = db.relationship("User",backref= "ratings")
def __repr__(self):
return f'<Rating rating_id={self.rating_id} score={self.score} movie_id={self.movie_id} user_id={self.user_id}>'
def connect_to_db(flask_app, db_uri="postgresql:///ratings", echo=True):
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)