def test_update_available_quantity(self): rowcount = db.update_available_quantity(4, 10) self.assertEqual( rowcount, 0, "Update Available Quantity Day 1 - Updated non-existent listing available_quantity." ) db.update_available_quantity(7, 1) listing = db.get_one_listing(1) self.assertEqual( listing["available_quantity"], 43, "Update Available Quantity Day 1 - Unexpected available quantity after update." )
def listing_purchase(listing_id): if 'user_id' not in session: return redirect(url_for('login')) rel_link = helper_functions.relative_link(request.path, request.referrer) listing = db.get_one_listing(listing_id) buy_item = buy_form() ppu = listing['price_per_unit'] total_price = "${:.2f}".format(float(listing['price_per_unit']) * 1) qty_purchased = buy_item.quantity.data if buy_item.validate_on_submit( ) and qty_purchased <= listing['available_quantity']: db.update_available_quantity(qty_purchased, listing_id) total_cost = round( float(listing['price_per_unit']) * int(qty_purchased)) order_created = db.add_new_order(listing_id, qty_purchased, total_cost, session['user_id']) if order_created == 1: listing_detail = db.get_listing_details_for_confirmation_page( listing_id) order = { 'listing_detail': listing_detail, 'qty': qty_purchased, 'total_price': "${:.2f}".format(total_cost) } name = '{} {}'.format(order['listing_detail'][3].capitalize(), order['listing_detail'][4].capitalize()) else: flash('FAILED TO CREATE ORDER') return render_template('listing-confirmation.html', listing_id=listing_id, order=order, name=name, unit=listing['unit_type']) elif buy_item.validate_on_submit( ) and qty_purchased > listing['available_quantity']: flash('Please select no more than the quantity that is available.') elif buy_item.validate_on_submit(): flash('Unable to purchase item') return render_template('listing-purchase.html', listing=listing, form=buy_item, rel_link=rel_link, total_price=total_price, ppu=ppu)
def listing_detail(listing_id): rel_link = helper_functions.relative_link(request.path, request.referrer) listing = db.get_one_listing(listing_id) user = db.get_one_user(listing['seller_id']) # NOTE: GAPI uses are limited, only comment out to make feature actually work # NOTE: API CALL FOR GIVEN BUYER -> GIVEN SELLER, applies to all listings by one seller # -------------------------------------------------------------------------- buyer_address = session['zipcode'] seller_address = helper_functions.address_string(listing['seller_id']) url = "https://maps.googleapis.com/maps/api/directions/json?origin={}&destination={}&key={}".format( buyer_address, seller_address[1], google_maps_key()) result = simplejson.load(urllib.request.urlopen(url)) result_dist = result['routes'][0]['legs'][0]['distance']['text'] result_time = result['routes'][0]['legs'][0]['duration']['text'] time_dist = [result_time, result_dist] # time_dist = ['10 mins', '4.5 mi'] return render_template('listing-detail.html', listing=listing, user=user, rel_link=rel_link, time_dist=time_dist)
def test_get_one_listing(self): listing = db.get_one_listing(1) self.assertEqual(listing['title'], "Peppers")
def test_get_one_listing(self): listing = db.get_one_listing(1) self.assertTrue( listing is None, "Get One Listing Day 0 - Non-existent listing should not have returned." )