-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
277 lines (190 loc) · 8.31 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Models & db functions for The Hub
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# import wikia # probably don't need this anymore b/c manually entering
db = SQLAlchemy()
# TO DO:
# Separate models into discrete files (this file is getting really long)
########################################################################
# Model definitions
class Character(db.Model):
"""Character in the Whoniverse."""
__tablename__ = 'characters'
char_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(50))
# 1 = Doctor Regeneration, 2 = Companion,
# 3 = Jack (fixed point in time & space), 4 = Torchwood
group = db.Column(db.Integer)
actor = db.Column(db.String(50))
bio = db.Column(db.Text)
image = db.Column(db.String(100)) # Will be a link to image URL
@classmethod
def all(cls):
q = cls.query
return q.all()
# Combine by_id & by_ids in future refactoring?
@classmethod
def by_id(cls, char_id):
q = cls.query.filter_by(char_id=char_id)
c = q.first()
return c
@classmethod
def by_ids(cls, char_ids):
q = cls.query.filter(cls.char_id.in_(char_ids))
return q.all()
@classmethod
def by_name(cls, name):
q = cls.query.filter_by(name=name)
c = q.first()
return c
# TO DO
# This will be used in display_character(), post_character_rating(), where else?
# @classmethod
# def filter_by():
# pass
# For future feature to allow users to add a character to db
# @classmethod
# def new(cls, name, actor, bio, image):
# # c = create obj
# # c.insert()
# return c
# Is this a @staticmethod ? (think so...)
def relationships(self):
# find relationships
q = Relationship.query
filter_rels = q.filter((Relationship.char1_id == self.char_id) |
(Relationship.char2_id == self.char_id)).all()
# build list of char_id's
char_ids = []
for rel in filter_rels:
char_ids.extend((rel.char1_id, rel.char2_id))
char_ids = set(char_ids)
# look up characters # characters that self is related to + self
chars = Character.by_ids(char_ids)
# remove myself
chars = [c for c in chars if c.char_id != self.char_id]
return chars
appearances = db.relationship("Series",
secondary="character_series",
backref="characters")
# Not needed at present: entering manually because a link between characters
# isn't necessarily established right when a character enters the timeline.
# @staticmethod
# def first_appears(char_id):
# """Determine first series a character appears in."""
# series_in = Character.by_id(char_id).appearances
# series_set = {s.chron_order for s in series_in}
# first = min(series_set)
# return first
def __repr__(self):
"""Provide helpful representation when printed"""
return "<Character char_id=%s name=%s>" % (self.char_id, self.name)
class Relationship(db.Model):
"""Middle table: connects Character table to itself.
Pairs of characters that have interacted with each other in some way."""
__tablename__ = 'relationships'
rel_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
char1_id = db.Column(db.Integer, db.ForeignKey('characters.char_id'))
char2_id = db.Column(db.Integer, db.ForeignKey('characters.char_id'))
# TO DO: Need to reverse order in .csv file for the D3 slider
# Threshold for D3 slider
threshold = db.Column(db.Integer)
def __repr__(self):
"""Provide helpful representation when printed"""
return "<Relationship rel_id=%s char1_id=%s char2_id=%s>" % (self.rel_id, self.char1_id, self.char2_id)
class Series(db.Model):
"""A single series (British for "season") of a show in the Whoniverse."""
__tablename__ = 'series'
series_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(50))
synopsis = db.Column(db.Text)
# changed from DateTime to account for series which occur over multiple years
date = db.Column(db.String(10))
chron_order = db.Column(db.Integer) # not needed anymore, remove
image = db.Column(db.String(100)) # Will be a link to image URL
@classmethod
def all(cls):
q = cls.query
return q.all()
@classmethod
def by_id(cls, series_id):
q = cls.query.filter_by(series_id=series_id)
s = q.first()
return s
# Added 5/28, not used yet
@classmethod
def by_name(cls, name):
q = cls.query.filter_by(name=name)
s = q.first()
return s
def __repr__(self):
"""Provide helpful representation when printed"""
return "<Series series_id=%s name=%s>" % (self.series_id, self.name)
class CharacterSeries(db.Model):
"""Association table between Character & Series."""
__tablename__ = 'character_series'
appearance_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
# PrimaryKeyConstraint
character = db.Column(db.Integer, db.ForeignKey('characters.char_id'))
series = db.Column(db.Integer, db.ForeignKey('series.series_id'))
def __repr__(self):
"""Provide helpful representation when printed"""
return "<Appearance character=%s series=%s>" % (self.character, self.series)
class User(db.Model):
"""A user of the app who will rate characters."""
__tablename__ = 'users'
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
email = db.Column(db.String(50))
password = db.Column(db.String(100))
# display_name = # May or may not use, not really needed for project
def __repr__(self):
"""Provide helpful representation when printed"""
return "<User user_id=%s email=%s>" % (self.user_id, self.email)
# Middle table between Character & User
class CharacterRating(db.Model):
"""Rating of a character by a user."""
__tablename__ = 'character_ratings'
rating_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
char_id = db.Column(db.Integer, db.ForeignKey('characters.char_id'))
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
score = db.Column(db.Integer)
# Define relationship to user
user = db.relationship("User",
backref=db.backref("character_ratings", order_by=rating_id))
# Define relationship to character
character = db.relationship("Character",
backref=db.backref("character_ratings", order_by=rating_id))
def __repr__(self):
"""Provide helpful representation when printed"""
return "<Rating rating_id=%s character=%s user=%s score=%s>" % (self.rating_id, self.character, self.user, self.score)
# Middle table between Series & User
class SeriesRating(db.Model):
"""Rating of a character by a user."""
__tablename__ = 'series_ratings'
rating_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
series_id = db.Column(db.Integer, db.ForeignKey('series.series_id'))
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
score = db.Column(db.Integer)
# Define relationship to user
user = db.relationship("User",
backref=db.backref("series_ratings", order_by=rating_id))
# Define relationship to character
series = db.relationship("Series",
backref=db.backref("series_ratings", order_by=rating_id))
def __repr__(self):
"""Provide helpful representation when printed"""
return "<Rating rating_id=%s series=%s user=%s score=%s>" % (self.rating_id, self.series, self.user, self.score)
#############################################################################
# Helper functions
def connect_to_db(app):
""" Connect db to Flask app """
# Configure to use PostgreSQL db
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///the_hub'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
db.app = app
db.init_app(app)
if __name__ == "__main__":
from server import app
connect_to_db(app)
print "Welcome to the Whoniverse."