-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
96 lines (66 loc) · 3.38 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
"""Models and database functions for Ratings project."""
from flask_sqlalchemy import SQLAlchemy
# This is the connection to the PostgreSQL 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 User(db.Model):
"""User of ratings website."""
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
email = db.Column(db.String(64), nullable=True)
password = db.Column(db.String(64), nullable=True)
age = db.Column(db.Integer, nullable=True)
zipcode = db.Column(db.String(15), nullable=True)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<user_id={} email={} age={} zipcode={}>".format(self.user_id, self.email, self.age, self.zipcode)
class Movie(db.Model):
"""Movie info stuff"""
__tablename__ = "movies"
movie_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
title = db.Column(db.String(150))
released_at = db.Column(db.DateTime, nullable=True)
imdb_url = db.Column(db.String(150), nullable=True)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<movie_id={} title={} released_at={} imdb_url={}>".format(self.movie_id,
self.title,
self.released_at, self.imdb_url)
class Rating(db.Model):
"""Rating Info for Movies"""
__tablename__ = "ratings"
rating_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
movie_id = db.Column(db.Integer, db. ForeignKey('movies.movie_id'))
user_id = db.Column(db.Integer, db. ForeignKey('users.user_id'))
score = db.Column(db.Integer, nullable=True)
# Define relationship to user
user = db.relationship("User",
backref=db.backref("ratings",
order_by=rating_id))
# Define relationship to movie
movie = db.relationship("Movie",
backref=db.backref("ratings",
order_by=rating_id))
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Rating rating_id={} movie_id={} user_id={} score={}>".format(self.rating_id,
self.movie_id,
self.user_id, self.score)
##############################################################################
# Helper functions
def connect_to_db(app):
"""Connect the database to our Flask app."""
# Configure to use our PstgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///ratings'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
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."