-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
318 lines (225 loc) · 11 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
################################################################################
# Defining our db
# given a skirun, we can get skirun.lifts (this is because we call it lifts on the Skirun object)
# from skirun.lifts, then we have list of all lifts
# iterate through skirun.lifts, for each lift, you can do lift.loading_pt.location (loading_pt is from definition on Lift object)
class Skirun(db.Model):
"""information about each ski run"""
# Lets SQL alchemy know there is a table named 'skiruns'
__tablename__ = "skiruns"
# Lets SQL alchemy know which columns to add
skirun_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
category_id = db.Column(db.Integer, db.ForeignKey('categories.category_id'))
groomed = db.Column(db.Boolean)
status = db.Column(db.Boolean)
name = db.Column(db.String(200))
level = db.Column(db.String(200))
# Defining the relationship between the skirun class and the lift table
lifts = db.relationship("Lift", secondary="skiruns_lifts")
# Defining the relationship between the skirun class and the category table
category = db.relationship("Category")
def __repr__(self):
""" Provide helpful information about each skirun"""
return "< Skirun name={} skirun_id={}>".format(self.name, self.skirun_id)
class Lift(db.Model):
"""information about each lift """
# Lets SQL alchemy know there is a table named 'lifts'
__tablename__ = "lifts"
# Lets SQL alchemy know which columns to add
lift_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(150))
status = db.Column(db.String(150))
mountain = db.Column(db.String(200))
# food_id = db.Column(db.Integer,
# db.ForeignKey('foods.food_id'),
# nullable=False)
# Defining the realtionship between the lift class and the skirun table
skiruns = db.relationship("Skirun", secondary="skiruns_lifts")
# Defining the relationship between the food class and the lift table
foods = db.relationship("Food", secondary="foods_lifts")
def __repr__(self):
""" Provide helpful information about each lift"""
return "<name={} lift_id={}>".format(self.name, self.lift_id)
class FoodLift(db.Model):
"""Associative table for the relationships of lifts and food """
__tablename__ = "foods_lifts"
FL_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
lift_id = db.Column(db.Integer,
db.ForeignKey('lifts.lift_id'),
nullable=False)
food_id = db.Column(db.Integer,
db.ForeignKey('foods.food_id'),
nullable=False)
class Food(db.Model):
"""Information about food options on the mountain """
__tablename__ = "foods"
food_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(150))
description = db.Column(db.String(250))
# if food location at base TRUE if at summit FALSE
location = db.Column(db.Boolean)
yelp_id = db.Column(db.String(250))
# Defining the relationship between the food class and the lift table
lifts = db.relationship("Lift", secondary="foods_lifts")
def to_dict(self):
""" returns the rating in dictionary form"""
return {'food_id': self.food_id,
'name': self.name,
'description': self.description,
'location': self.location}
def __repr__(self):
""" Provide helpful information about each restaurant"""
return "< Restaurant name={} food_id={}>".format(self.name, self.food_id)
class SkirunLift(db.Model):
__tablename__ = 'skiruns_lifts'
skirun_lift_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
lift_id = db.Column(db.Integer,
db.ForeignKey('lifts.lift_id'),
nullable=False)
skirun_id = db.Column(db.Integer,
db.ForeignKey('skiruns.skirun_id'),
nullable=False)
class Category(db.Model):
"""information about the differnt riding categories """
# Lets SQL alchemy know there is a table named 'levels'
__tablename__ = "categories"
# Lets SQL alchemy know which columns to add
category_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
cat = db.Column(db.String(150))
icon = db.Column(db.String(150))
# Defining the relationship between the skirun class and the category table
skiruns = db.relationship("Skirun")
# Defining the relationship between the user class and the category table
users = db.relationship("User", secondary='catusers')
def __repr__(self):
""" Provide helpful information about each category"""
return "<category={} cat_id={}>".format(self.cat, self.category_id)
class User(db.Model):
"""User of WhistlerMTN."""
# Lets SQL alchemy know there is a table named 'Users'
__tablename__ = "users"
#Creating the table
# Lets SQL alchemy know this is an integer pk
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
fname = db.Column(db.String(200), nullable=False)
lname = db.Column(db.String(200), nullable=False)
email = db.Column(db.String(200), nullable=False)
zipcode = db.Column(db.String(200))
password = db.Column(db.String(64), nullable=False)
level_id = db.Column(db.Integer,
db.ForeignKey('skill_levels.level_id'))
# Defining the relationship with skilllevel class
skills = db.relationship('SkillLevel')
# Defining the realtionship with category class
categories = db.relationship('Category', secondary='catusers')
def __repr__(self):
""" Provide helpful information about this class"""
return "<User user_id={} email={}>".format(self.user_id, self.email)
# FIXME USER <--> CATEGORIES
class CatUser(db.Model):
__tablename__ = 'catusers'
# An associative table connecting users to categories
catuser_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
category_id = db.Column(db.Integer,
db.ForeignKey('categories.category_id'),
nullable=False)
user_id = db.Column(db.Integer,
db.ForeignKey('users.user_id'),
nullable=False)
class SkillLevel(db.Model):
"""Green, Blue, Black"""
# Lets SQL alchemy know there is a table named 'Users'
__tablename__ = "skill_levels"
#Creating the table
# Lets SQL alchemy know this is an integer primary key that auto increments
level_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
level = db.Column(db.String(20), nullable=False)
# Defining the relationship between the user class and the skilllevel table
users = db.relationship("User")
def __repr__(self):
""" Provide helpful information about this class"""
return "<ID: skill_id={} level={}>".format(self.level_id, self.level)
class Rating(db.Model):
"""Ratings information for each skirun"""
# Lets SQL alchemy know there is a table named 'Ratings'
__tablename__ = "ratings"
#Creating the table
# Lets SQL alchemy know this is an integer primary key that auto increments
rating_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
rating = db.Column(db.Integer, nullable=False)
comment = db.Column(db.String(250))
# define skirun relationship to ratings through skirun id
skirun_id = db.Column(db.Integer,
db.ForeignKey('skiruns.skirun_id'),
nullable=False)
#Define user_id as a FK to the users table passed in as "table.column_name"
user_id = db.Column(db.Integer,
db.ForeignKey('users.user_id'),
nullable=False)
# Defining the relationship between the user class and the ratings table
user = db.relationship("User",
backref=db.backref("ratings"))
# Defining a relationship to the Skirun class from the ratings table
skirun = db.relationship("Skirun",
backref=db.backref("ratings"))
def __repr__(self):
""" Provide helpful information about this class"""
return "<User rating_id={} for skirun_id={}>".format(self.rating_id, self.skirun_id)
def to_dict(self):
""" returns the rating in dictionary form"""
return {'rating_id': self.rating_id,
'rating': self.rating,
'comment': self.comment}
class Fave(db.Model):
"""Tracks all of the users favorite skiruns"""
# Lets SQL alchemy know there is a table named 'faves'
__tablename__ = "faves"
#Creating the table
# Lets SQL alchemy know this is an integer primary key that auto increments
fave_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
skirun_id = db.Column(db.Integer,
db.ForeignKey('skiruns.skirun_id'),
nullable=False)
user_id = db.Column(db.Integer,
db.ForeignKey('users.user_id'),
nullable=False)
# Defining the relationship between the user class and the faves table
users = db.relationship("User",
backref=db.backref("faves"))
# Defining the relationship between the skirun class and the faves table
skiruns = db.relationship("Skirun",
backref=db.backref("faves"))
def __repr__(self):
""" Provide helpful information about this class"""
return "<ID: fave_id={} skirun_id={}>".format(self.fave_id, self.skirun_id)
class Weather(db.Model):
"""information about weather on Blackcomb & Whistler mountain """
# Lets SQL alchemy know which columns to add
weather_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
daily_snowfall = db.Column(db.Integer)
overnight_snowfall = db.Column(db.String(150))
forcast_icon = db.Column(db.String(150))
wind_forcast = db.Column(db.String(150))
snow_forcast = db.Column(db.String(150))
def __repr__(self):
""" Provide helpful information about each loading location"""
return "<dailysnow={} snowforcast={}>".format(self.daily_snowfall, self.snow_forcast)
##############################################################################
# Helper functions
def connect_to_db(app, db_uri='postgresql:///whistler'):
"""Connect the database to our Flask app."""
# Configure to use our PstgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
if __name__ == "__main__":
# As a convenience, so we can run this module interactively,
# and work with the database directly.
from flask import Flask
app = Flask(__name__)
connect_to_db(app)
print "Connected to DB."
db.create_all()