-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
180 lines (138 loc) · 6.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""all classes for interaction with the database.
"""
from flask_sqlalchemy import SQLAlchemy
from random import randint
# from datetime import date
db = SQLAlchemy()
class Movie(db.Model):
"""class for movie info.
"""
__tablename__ = "movies"
movie_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
tmdb_id = db.Column(db.String(12), nullable=False)
imdb_id = db.Column(db.String(20), nullable=False)
overview = db.Column(db.UnicodeText)
tagline = db.Column(db.String(200))
image_src = db.Column(db.String(200), nullable=False)
title = db.Column(db.String(200), nullable=False)
tmdb_rating = db.Column(db.Numeric(asdecimal=True))
# below are deliberately non-normalized for efficiency
bechdel = db.Column(db.Integer, nullable=False)
visual = db.Column(db.Integer, nullable=False)
linear = db.Column(db.Integer, nullable=False)
cheerful = db.Column(db.Integer, nullable=False)
active = db.Column(db.Integer, nullable=False)
magical = db.Column(db.Integer, nullable=False)
mature = db.Column(db.Integer, nullable=False)
# implicit:
# movie_ratings = db.relationship("MovieRating")
# users = db.relationship("User", secondary=movie_ratings)
@classmethod
def get_randomized_movies(cls):
"""returns all movies in the database, but makes sure to RANDOMIZE.
"""
movies = cls.query.all()
movies_rand = []
while len(movies) > 0:
index = randint(0, len(movies) - 1)
next_movie = movies.pop(index)
movies_rand.append(next_movie)
return movies_rand
def __repr__(self):
return """<movie_id: %i, title: %s, bec: %i, vis: %i, lin: %i, chr: %i,
act: %i, mag: %i, mat: %i>""" % (self.movie_id, self.title, self.bechdel, self.visual, self.linear, self.cheerful, self.active, self.magical, self.mature)
class Comic(db.Model):
"""class for comic book info
"""
__tablename__ = "comics"
comic_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
isbn10 = db.Column(db.String(10))
isbn13 = db.Column(db.String(13), unique=True)
author = db.Column(db.String(100))
artist = db.Column(db.String(100)) # if artist is not writer
publisher = db.Column(db.String(50))
title = db.Column(db.String(200))
summary = db.Column(db.UnicodeText)
# below are deliberately non-normalized for efficiency
bechdel = db.Column(db.Integer, nullable=False)
visual = db.Column(db.Integer, nullable=False)
linear = db.Column(db.Integer, nullable=False)
cheerful = db.Column(db.Integer, nullable=False)
active = db.Column(db.Integer, nullable=False)
magical = db.Column(db.Integer, nullable=False)
mature = db.Column(db.Integer, nullable=False)
# implicit:
# comic_recs = db.relationship("ComicRec")
# user = db.relationship("User", secondary=comic_recs)
def __repr__(self):
return """<comic_id: %i, title: %s, bec: %i, vis: %i, lin: %i, chr: %i,
act: %i, mag: %i, mat: %i>""" % (self.comic_id, self.title, self.bechdel, self.visual, self.linear, self.cheerful, self.active, self.magical, self.mature)
class User(db.Model):
"""class for user info
"""
__tablename__ = "users"
user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
nickname = db.Column(db.String(30))
email = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(200), nullable=False)
# implicit:
# movie_ratings = db.relationship("MovieRating")
# comic_recs = db.relationship("ComicRec")
# movies = db.relationship("Movie", secondary=movie_rating)
# comics = db.relationship("Comic", secondary=comic_recs)
def __repr__(self):
return "<User_id: %i, email: %s>" % (self.user_id, self.email)
class MovieRating(db.Model):
"""class for rating for each movie the user rates (1-5)
"""
__tablename__ = "movie_ratings"
mr_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=False)
movie_id = db.Column(db.Integer, db.ForeignKey('movies.movie_id'), nullable=False)
rating = db.Column(db.Integer, nullable=False)
rated_at = db.Column(db.Date)
##### TEST #########
__table_args__ = (db.UniqueConstraint('user_id', 'movie_id'), )
user = db.relationship("User", backref="movie_ratings")
movie = db.relationship("Movie", backref="movie_ratings")
def __repr__(self):
return """<mr_id: %i, user_id: %i, movie_id: %i,
rating: %i>""" % (self.mr_id, self.user_id, self.movie_id, self.rating)
class ComicRec(db.Model):
"""class for comic recommendations to each user.
"""
__tablename__ = "comic_recs"
cr_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=False)
comic_id = db.Column(db.Integer, db.ForeignKey('comics.comic_id'), nullable=False)
recd_at = db.Column(db.Date, nullable=False)
user_read = db.Column(db.Boolean)
user_rating = db.Column(db.Integer) # 1-5
__table_args__ = (db.UniqueConstraint('user_id', 'comic_id'), )
user = db.relationship("User", backref="comic_recs")
comic = db.relationship("Comic", backref="comic_recs")
def __repr__(self):
return "<cr_id: %i, user_id: %i, comic_id: %i>" % (self.cr_id, self.user_id, self.comic_id)
#___________________________________________________________________________
#___________________________________________________________________________
# below for LOCAL hosting
# def connect_to_db(app, db_name="flix2comix"):
# """Connect to database."""
# db_path = 'postgresql:///%s' % (db_name)
# app.config['SQLALCHEMY_DATABASE_URI'] = db_path
# app.config['SQLALCHEMY_ECHO'] = True
# db.app = app
# db.init_app(app)
# for heroku:
def connect_to_db(app, db_uri=None):
"""Connect our application to our database."""
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri or 'postgres:///flix2comix'
db.app = app
db.init_app(app)
print "Connected to DB."
if __name__ == '__main__':
"""if run directly, allows for interaction with database.
"""
from server import app
connect_to_db(app)
print "Connected to DB!"