-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_update_helpers.py
490 lines (353 loc) · 14.8 KB
/
make_update_helpers.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
from flask import request, session
from model import User, Region, Brand, Product, Tent
from model import SleepingBag, SleepingPad, PostalCode, db
from sqlalchemy.orm.exc import NoResultFound
from datetime import datetime
from geolocation.google_maps import GoogleMaps
import os
"""
Helper functions used in server.py to make and update products and users.
Fifteen functions total.
Also threw in two non-making or updating functions at the end:
(1) a merge sort helper function to display rental histories in descending
order based on rental submission date, and
(2) a formatting function for phone numbers so phone numbers display as
(xxx) xxx-xxxx on the user's account-info page.
"""
######################## User stuff ###################################
def make_postalcode(postalcode_string):
"""
Takes in zipcode as a string, gets lat and long info using the GoogleMaps
API, and adds the zipcode to the PostalCodes table.
"""
google_maps = GoogleMaps(api_key=os.environ['GOOGLE_API_KEY'])
location = google_maps.search(location=postalcode_string).first()
print "\n\n\n\n Google Maps API: location is %r \n\n" % location
a = PostalCode(postalcode=int(postalcode_string), latitude=location.lat,
longitude=location.lng)
db.session.add(a)
db.session.commit()
temp = PostalCode.query.get(int(postalcode_string))
print "****************\n\n\n\n\n MADE POSTALCODE %r\n\n\n\n\n*************" % temp
def make_user(password):
""" Creates User object when a user creates a new account.
Takes in a password as a string, grabs info from the create account
form, and returns a User object.
"""
firstname = request.form.get('firstname')
lastname = request.form.get('lastname')
staddress = request.form.get('staddress')
cty = request.form.get('cty')
state = request.form.get('state')
zipcode = request.form.get('zipcode')
# Change from v1: phone number is a string (used to be integer)
phonenumber = request.form.get('phonenumber')
username = request.form.get('username')
profile_url = request.form.get('profile_url')
# Get region id from the Regions table
region = Region.query.filter(Region.abbr == state).one()
state_id = region.region_id
############## TODO: MAKE TEST FOR ADDING ZIPCODE DURING MAKING USER #####
print "\n\n\n\n\n\n************* CHECKING IF POSTALCODE IN DB *****************\n\n\n\n"
zipcode_check = PostalCode.query.get(int(zipcode))
if not zipcode_check:
print "\n\n CALLING make_postalcode \n\n\n"
make_postalcode(zipcode)
if not profile_url:
profile_url = "static/img/defaultimg.jpg"
user = User(fname=firstname, lname=lastname, street=staddress,
city=cty, region_id=state_id, postalcode=zipcode,
phone=phonenumber, email=username, password=password,
profile_pic_url=profile_url)
return user
######################## Listing stuff ###################################
def check_brand(brand_id):
""" When a user lists an item, we first check if the brand of the item already
exists in the database. If not, we create it and add it to the
database's Brands table.
Takes integer brand_id (which will be -1 if it is a new brand),
and returns integer brand_id (which, for a new brand, will be the
brand_id for the newly created Brand object).
"""
if brand_id < 0:
new_brand_name = request.form.get("new_brand_name")
brand = Brand(brand_name=new_brand_name)
db.session.add(brand)
db.session.commit()
brand_id = brand.brand_id
return brand_id
def make_brand(brandname):
""" Adds a new brand to the database. This is called by the check_brand
function above if a new brand needs to be added to the database.
"""
brand = Brand(brand_name=brandname)
db.session.add(brand)
db.session.commit()
def get_brand_id(brandname):
"""Takes brand name as a string and returns brand_id as an integer."""
brand = Brand.query.filter(Brand.brand_name == brandname).one()
return brand.brand_id
def make_parent_product(brand_id, category_id):
""" When a user lists an item, we first make a parent Product object.
Takes two integers, brand_id and category_id, grabs info from the
listing form, and returns a Product object.
"""
modelname = request.form.get("modelname")
desc = request.form.get("desc")
cond = request.form.get("cond")
avail_start = request.form.get("avail_start")
avail_end = request.form.get("avail_end")
pricing = float(request.form.get("pricing"))
image = request.form.get("image")
if not image:
image = "static/img/defaultimg.jpg"
avail_start = datetime.strptime(avail_start, "%Y-%m-%d")
avail_end = datetime.strptime(avail_end, "%Y-%m-%d")
user = User.query.filter(User.email == session['user']).one()
product = Product(cat_id=category_id, brand_id=brand_id,
owner_user_id=user.user_id, model=modelname,
description=desc, condition=cond,
avail_start_date=avail_start, avail_end_date=avail_end,
price_per_day=pricing, image_url=image)
return product
def make_tent(product_id):
""" Makes a subset Tent object given the corresponding parent Product
prod_id primary key.
Takes in prod_id as an integer, grabs info from the list-tent form,
and returns a Tent object.
"""
best_use_id = int(request.form.get("bestuse"))
sleep = int(request.form.get("sleep"))
seasoncat = int(request.form.get("seasoncat"))
weight = int(request.form.get("weight"))
# Deal with optional values.
try:
width = int(request.form.get("width"))
except ValueError:
width = None
try:
length = int(request.form.get("length"))
except ValueError:
length = None
try:
doors = int(request.form.get("doors"))
except ValueError:
doors = None
try:
poles = int(request.form.get("poles"))
except ValueError:
poles = None
tent = Tent(prod_id=product_id, use_id=best_use_id,
sleep_capacity=sleep, seasons=seasoncat, min_trail_weight=weight,
floor_width=width, floor_length=length, num_doors=doors,
num_poles=poles)
return tent
def make_sleeping_bag(product_id):
""" Makes a subset SleepingBag object given the corresponding parent Product
prod_id primary key.
Takes in prod_id as an integer, grabs info from the list-sleepingbag
form, and returns a SleepingBag object.
"""
filltype = request.form.get("filltype")
temp = int(request.form.get("temp"))
bag_weight = int(request.form.get("bag_weight"))
# Deal with optional values.
try:
length = int(request.form.get("length"))
except ValueError:
length = None
try:
gender = request.form.get("gender")
except ValueError:
gender = None
sleeping_bag = SleepingBag(prod_id=product_id, fill_code=filltype,
temp_rating=temp, weight=bag_weight, length=length,
gender_code=gender)
return sleeping_bag
def make_sleeping_pad(product_id):
""" Makes a subset SleepingPad object given the corresponding parent Product
prod_id primary key.
Takes in prod_id as an integer, grabs info from the list-sleepingpad
form, and returns a SleepingPad object.
"""
padtype = request.form.get("padtype")
best_use_id = int(request.form.get("bestuse"))
r_val = float(request.form.get("r_val"))
pad_weight = int(request.form.get("pad_weight"))
pad_length = int(request.form.get("pad_length"))
# Deal with optional values.
try:
pad_width = int(request.form.get("pad_length"))
except ValueError:
pad_width = None
sleeping_pad = SleepingPad(prod_id=product_id, type_code=padtype,
use_id=best_use_id, r_value=r_val,
weight=pad_weight,
length=pad_length, width=pad_width)
return sleeping_pad
###################### Editing stuff ################################
def update_parent_product(prod_id, brand_id):
""" Updates parent Product attributes.
Takes integers brand_id and category_id, and uses info from the edit form
to update the parent Product.
"""
product = Product.query.get(prod_id)
product.available = True
product.brand_id = brand_id
product.model = request.form.get("modelname")
product.description = request.form.get("desc")
product.condition = request.form.get("cond")
product.price_per_day = float(request.form.get("pricing"))
product.image_url = request.form.get("image")
avail_start = request.form.get("avail_start")
avail_end = request.form.get("avail_end")
avail_start = datetime.strptime(avail_start, "%Y-%m-%d")
avail_end = datetime.strptime(avail_end, "%Y-%m-%d")
product.avail_start_date = avail_start
product.avail_end_end = avail_end
db.session.commit()
return
def update_tent(prod_id):
""" Updates tent object with the given prod_id.
Grabs info from the category-specific area of the edit item form.
"""
tent = Tent.query.get(prod_id)
tent.use_id = int(request.form.get("bestuse"))
tent.sleep_capacity = int(request.form.get("sleep"))
tent.seasons = int(request.form.get("seasoncat"))
tent.min_trail_weight = int(request.form.get("weight"))
# Deal with optional values.
try:
tent.floor_width = int(request.form.get("width"))
except ValueError:
tent.floor_width = None
try:
tent.floor_length = int(request.form.get("length"))
except ValueError:
tent.floor_length = None
try:
tent.num_doors = int(request.form.get("doors"))
except ValueError:
tent.num_doors = None
try:
tent.num_poles = int(request.form.get("poles"))
except ValueError:
tent.num_poles = None
db.session.commit()
return
def update_sleeping_bag(prod_id):
""" Updates sleeping bag object of given prod_id.
Grabs info from the category-specific area of the edit item form.
"""
sleeping_bag = SleepingBag.query.get(prod_id)
sleeping_bag.fill_code = request.form.get("filltype")
sleeping_bag.temp_rating = int(request.form.get("temp"))
# Deal with optional values.
try:
sleeping_bag.weight = int(request.form.get("bag_weight"))
except ValueError:
sleeping_bag.weight = None
try:
sleeping_bag.length = int(request.form.get("length"))
except ValueError:
sleeping_bag.length = None
gender = request.form.get("gender")
if gender == "Z":
sleeping_bag.gender_code = None
else:
sleeping_bag.gender_code = gender
db.session.commit()
return
def update_sleeping_pad(prod_id):
""" Updates sleeping pad object of given prod_id.
Grabs info from the category-specific area of the edit item form.
"""
sleeping_pad = SleepingPad.query.get(prod_id)
sleeping_pad.type_code = request.form.get("padtype")
sleeping_pad.use_id = int(request.form.get("bestuse"))
sleeping_pad.r_value = float(request.form.get("r_val"))
sleeping_pad.weight = int(request.form.get("pad_weight"))
sleeping_pad.length = int(request.form.get("pad_length"))
# Deal with optional values.
try:
sleeping_pad.width = int(request.form.get("pad_width"))
except ValueError:
sleeping_pad.width = None
db.session.commit()
return
# These are not related to making or updating objects. Just threw them in here
# so I wouldn't have another helper function file.
def calc_avg_star_rating(ratings):
""" Calculates average star rating to one decimal point to display one
show-user and show-product-ratings templates.
Takes a list of ratings and returns average star rating as a
float. If no star ratings, returns -1.
"""
avg_star_rating = -1
if ratings:
sum_stars = 0
count_star_ratings = 0
for rating in ratings:
if rating.stars:
sum_stars += rating.stars
count_star_ratings += 1
avg_star_rating = float(sum_stars) / count_star_ratings
return avg_star_rating
def reverse_merge_sort_histories(lst):
""" Sorts histories in descending order based on rental_sumbission_date
for display on a user's acount-info page.
Takes in list of product histories and returns a list of sorted histories.
"""
if len(lst) > 1:
midpt = int(len(lst) / 2)
lst1 = lst[:midpt]
lst2 = lst[midpt:]
# Recursively split the lists in half. Degenerate case is list of
# single items.
reverse_merge_sort_histories(lst1)
reverse_merge_sort_histories(lst2)
# Function will continue here when the recrusive splits complete.
lst1_index = 0
lst2_index = 0
# Use below to write over the list passed in as a parameter.
ordered_lst_index = 0
# Loop through histories in each of the lists and compare which has a later
# rental sumbission date. The history with the later rental submission
# date is added first.
while lst1_index < len(lst1) and lst2_index < len(lst2):
if lst1[lst1_index].rental_submission_date > lst2[lst2_index].rental_submission_date:
lst[ordered_lst_index] = lst1[lst1_index]
lst1_index += 1
else:
lst[ordered_lst_index] = lst2[lst2_index]
lst2_index += 1
ordered_lst_index += 1
# If one list is longer than the other, we add all its histories to
# the end of the list.
while lst1_index < len(lst1):
lst[ordered_lst_index] = lst1[lst1_index]
ordered_lst_index += 1
lst1_index += 1
while lst2_index < len(lst2):
lst[ordered_lst_index] = lst2[lst2_index]
ordered_lst_index += 1
lst2_index += 1
return lst
def format_phone_number(phonenumber):
""" Formats phone number in (xxx) xxx-xxxx format.
Takes in string and returns string.
Note (Jan 7): had this in here in v1 because was storing
phone number as an integer. Now storing as a string,
so potentially can add to database in the "prettified"
format. But leave for now.
"""
pretty_phone_num = "("
for idx in range(0, 3):
pretty_phone_num += phonenumber[idx]
pretty_phone_num += ") "
for idx in range(3, 6):
pretty_phone_num += phonenumber[idx]
pretty_phone_num += "-"
for idx in range(6, 10):
pretty_phone_num += phonenumber[idx]
return pretty_phone_num