/
crud.py
209 lines (108 loc) · 4.82 KB
/
crud.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
"""CRUD operations to populate the database."""
from model import db, User, Trail, Restaurant, TrailRating, RestRating, Walk, WalkTrail, WalkRest, connect_to_db
def create_user(username, email, password):
"""Create and return a new user."""
user = {}
try:
user = User(username=username, email=email, password=password)
db.session.add(user)
db.session.commit()
except:
user = None
return user
def get_user_from_username(username):
"""Return a user instance from the username."""
user = User.query.filter(User.username == username).first()
return user
def confirm_username_and_password(username, password):
"""Check the database to confirm user credentials upon login."""
user = get_user_from_username(username)
if user is not None and user.username == username and user.password == password:
return True
def create_trail(latitude, longitude, name, length=None, location=None, image=None, hiking_id=None):
"""Create and return a new trail."""
trail = Trail.query.filter((Trail.name == name) & (Trail.latitude == latitude) & (Trail.longitude == longitude)).first()
if trail is not None:
return trail
else:
trail = Trail(latitude=latitude, longitude=longitude, name=name, length=length, location=location, image=image, hiking_id=hiking_id)
db.session.add(trail)
db.session.commit()
return trail
def create_restaurant(latitude, longitude, name, price=None, location=None, phone=None, image=None, yelp_id=None):
"""Create and return a new restaurant."""
restaurant = Restaurant.query.filter((Restaurant.yelp_id == yelp_id)).first()
if restaurant is not None:
return restaurant
else:
restaurant = Restaurant(latitude=latitude, longitude=longitude, name=name, price=price, location=location, phone=phone,
image=image, yelp_id=yelp_id)
db.session.add(restaurant)
db.session.commit()
return restaurant
def create_trail_rating(trail, user, trail_comment=None, trail_star=None,
difficulty_level=None, crowded=False):
"""Create and return a new trail rating."""
trail_rating = TrailRating(trail=trail, user=user, trail_comment=trail_comment,
trail_star=trail_star, difficulty_level=difficulty_level,
crowded=crowded)
db.session.add(trail_rating)
db.session.commit()
return trail_rating
def create_rest_rating(restaurant, user, rest_comment=None, rest_star=None,
masks_worn=False, socially_distanced=False, outdoor_seating=False):
"""Create and return a new restaurant rating."""
rest_rating = RestRating(restaurant=restaurant, user=user, rest_comment=rest_comment,
rest_star=rest_star, masks_worn=masks_worn, socially_distanced=socially_distanced,
outdoor_seating=outdoor_seating)
db.session.add(rest_rating)
db.session.commit()
return rest_rating
def create_walk(user, walk_date=None):
"""Create a new walk."""
walk = Walk(user=user, walk_date=walk_date)
db.session.add(walk)
db.session.commit()
return walk
def get_walk_from_id(walk_id):
"""Get a walk from its id."""
walk = Walk.query.filter(Walk.walk_id == walk_id).first()
return walk
def get_rest_from_id(rest_id):
rest = Restaurant.query.filter(Restaurant.rest_id == rest_id).first()
return rest
def get_trail_from_id(trail_id):
trail = Trail.query.filter(Trail.trail_id == trail_id).first()
return trail
def create_walk_trail(trail, walk):
"""Create a new trail associated with a walk."""
walk_trail = WalkTrail(trail=trail, walk=walk)
db.session.add(walk_trail)
db.session.commit()
return walk_trail
def create_walk_restaurant(restaurant, walk):
"""Create a new restaurant associated with a walk."""
walk_rest = WalkRest(restaurant=restaurant, walk=walk)
db.session.add(walk_rest)
db.session.commit()
return walk_rest
def get_walk_details(walk_id):
"""Return the walks associated with a particular user."""
walk = Walk.query.filter(Walk.walk_id == walk_id).first()
walk_details = walk.get_walk_details()
return walk_details
def get_user_walks(username):
"""Return the walks associated with a particular user."""
user = get_user_from_username(username)
return user.walks
def get_trails():
"""Return the trails currently stored in the database."""
trails = Trail.query.all()
return trails
def get_restaurants():
"""Return the restaurants currently stored in the database."""
restaurants = Restaurant.query.all()
return restaurants
if __name__ == '__main__':
from server import app
connect_to_db(app)