-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
293 lines (196 loc) 路 8.62 KB
/
helper.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
"""Helper functions for server.py"""
# from model import *
import os
from crud import *
from sqlalchemy import and_, or_, not_
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="app")
MAPS_GEOCODING_KEY = os.environ['MAPS_GEOCODING_KEY']
def call_geocoding_api(search_terms):
"""Query Google Maps Geocoding API to convert search terms to
lat/lng coordinates"""
api_url = "https://maps.googleapis.com/maps/api/geocode/json"
payload = {
"address": search_terms,
"key": MAPS_GEOCODING_KEY
}
r = requests.get(api_url, params=payload)
response = r.json()
# Check to see if search was valid
if response.get("results"):
lat_long = response["results"][0]["geometry"]["location"]
return lat_long
# Return error message if message invalid
else:
return "Invalid search terms"
def create_user_submitted_loc(name, address, city, state):
"""Create and return a new user-submitted location."""
location = geolocator.geocode(address)
lat = location.latitude
lng = location.longitude
location = create_location(name, lat, lng, address, city, state)
db.session.add(location)
db.session.commit()
return location
# def create_user_submitted_bookloc(book_id, loc_id):
# """Create and return a new user-submitted location."""
# bookloc = create_book_location(book_id, loc_id)
# db.session.add(bookloc)
# db.session.commit()
# return location
def get_books_by_search(user_search):
""" Get books based on search string. """
books = Book.query.filter(or_(Book.title.ilike("%" + user_search + "%")),
(Author.name.ilike("%" + user_search + "%")))\
.order_by(Book.title)\
.all()
return books
def get_books_by_title(user_title_search):
""" Get books based on title search string.
"""
books = Book.query.filter(Book.title.ilike("%" + user_title_search + "%"))\
.order_by(Book.title)\
.all()
return books
def get_books_by_genre(user_genre):
""" Get subset of books based on genre.
Accepts genre entered by user, returns books.
"""
if user_genre == 'All':
books = Book.query.order_by(Book.title).all()
else:
books = Book.query.filter(Book.genre_rel.ilike("%" + user_genre + "%"))\
.order_by(Book.title)\
.all()
return books
def get_books_by_author(user_author_search):
""" Get author's books based on author name search string.
"""
books = db.session.query(Book).filter(Author.name.ilike("%" + user_author_search + "%"))\
.join(Author, Book.author_id == Author.author_id)\
.order_by(Book.title)\
.all()
return books
def get_users_rated_books(user_id):
"""Query database to find Ratings objects belonging to user
& not marked 'read."""
read_books = Rating.query.filter((Rating.user_id == user_id)
& (Rating.read.is_(True))).all()
return read_books
def get_users_saved_book(user_id, book_id):
"""Query database to find Ratings objects belonging to user
& matching book_id"""
saved_book = Rating.query.filter((Rating.user_id == user_id)
& (Rating.book_id == book_id)).first()
return saved_book
def get_session_users_saved_book(user_id, book_id):
"""Query database to find Ratings objects belonging to user in session
& matching book_id"""
saved_book = Rating.query.filter((Rating.user_id == user_id)
& (Rating.book_id == book_id)).first()
return saved_book
def get_rating_by_ids(user_id, book_id):
"""Query database to find Ratings objects matching search params."""
rating = Rating.query.filter((Rating.user_id == user_id)
& (Rating.book_id == book_id)).first()
return rating
def get_locations_by_search(user_loc_search):
""" Get books based on location search string.
"""
locations = Location.query.filter(Location.name.ilike("%" + user_loc_search + "%"))\
.order_by(Location.name)\
.all()
return locations
def get_all_locations():
locations = db.session.query(BookLocation).all()
location_dict = {}
location_list = []
for loc in locations:
dict_key = str(loc.location.lat) + str(loc.location.lng)
if loc.location.lat == 37.786220 and loc.location.lng == -122.432210:
location_list.append(loc.location.name)
elif dict_key in location_dict:
location_dict[dict_key]['desc'] = loc.location.description
else:
location_dict[dict_key] = {}
location_dict[dict_key]['id'] = loc.location.loc_id
location_dict[dict_key]['lat'] = loc.location.lat
location_dict[dict_key]['lng'] = loc.location.lng
location_dict[dict_key]['name'] = loc.location.name
return location_dict, location_list
# TODO: FIX query - user - ratings - books - book_loc
def get_booklocs_by_user(user_id):
""" Retrieve book locations
"""
# locations = FIX THIS, join
locations = db.session.query(BookLocation).filter_by(user_id=user_id).all() # this is a list
print(locations)
location_dict = {}
location_list = []
for loc in locations:
dict_key = str(loc.location.lat) + str(loc.location.lng)
if loc.location.lat == 37.786220 and loc.location.lng == -122.432210:
location_list.append(loc.location.name)
elif dict_key in location_dict:
location_dict[dict_key]['desc'] = loc.location.description
else:
location_dict[dict_key] = {}
location_dict[dict_key]['lat'] = loc.location.lat
location_dict[dict_key]['lng'] = loc.location.lng
location_dict[dict_key]['name'] = loc.location.name
return location_dict, location_list
def get_books_by_location(loc_id):
""" Retrieve book locations
Accepts book ID, returns locations formatted into 2 structures:
1. Location dictionary for locations with meaningful latitude & longitude
2. Location list for locations for which the Google maps API could not
identify a specific location from the description. For these locations,
the Google maps API returns coordinates for Hackbright in San Francisco.
Load these locations into a list, to display without markers on the book
detail page, because the markers would be misleading.
"""
books = db.session.query(BookLocation).filter_by(loc_id=loc_id).all() # this is a list
print(books)
book_dict = {}
book_list = []
for loc in books:
dict_key = loc.book.book_id
print(dict_key)
book_list.append(loc.book.title)
# TODO: FIX BOOK DICTIONARY
if dict_key not in book_dict:
book_dict.get('dict_key',{})
else:
book_dict[dict_key] = {}
book_dict[dict_key]['title'] = loc.book.title
book_dict[dict_key]['link'] = "/books/<int:loc.book.book_id>"
book_dict[dict_key]['cover'] = loc.book.cover_path
print(book_list)
print(book_dict)
return book_dict, book_list
def get_locations(book_id):
""" Retrieve book locations
Accepts book ID, returns locations formatted into 2 structures:
1. Location dictionary for locations with meaningful latitude & longitude
2. Location list for locations for which the Google maps API could not
identify a specific location from the description. For these locations,
the Google maps API returns coordinates for Hackbright in San Francisco.
Load these locations into a list, to display without markers on the book
detail page, because the markers would be misleading.
"""
locations = db.session.query(BookLocation).filter_by(book_id=book_id).all() # this is a list
print(locations)
location_dict = {}
location_list = []
for loc in locations:
dict_key = str(loc.location.lat) + str(loc.location.lng)
# if loc.location.lat == 37.786220 and loc.location.lng == -122.432210:
location_list.append(loc.location.name)
if dict_key in location_dict:
location_dict[dict_key]['desc'] = loc.location.description
else:
location_dict[dict_key] = {}
location_dict[dict_key]['lat'] = loc.location.lat
location_dict[dict_key]['lng'] = loc.location.lng
location_dict[dict_key]['name'] = loc.location.name
return location_dict, location_list