def product(db, id): """Return the page with the information of the single product (id)""" session.get_or_create_session(db) product_individual = dict() product_individual['individual_product'] = [] product_individual['individual_product'].append( model.product_get(db, int(id))) # if product_individual['individual_product'][0]['name'] is not None: # product_individual['title'] = product_individual['individual_product'][0]['name'] # return template('product_individual', product_individual) # else: # return error404(404) try: product_individual['title'] = product_individual['individual_product'][ 0]['name'] return template('product_individual', product_individual) except (TypeError, AttributeError): # return error404(404) return HTTPError(status=404)
def update_cart(db, itemid, quantity, update=False): """Update or add an item to the shopping cart. If the item is already present in the cart and update=False, the quantity of the item will be incremented, if update=True then the quantity will be reset. If update=True and quantity=0, the item will be removed from the cart""" session = request.environ.get('beaker.session') product = model.product_get(db, itemid) newitem = { 'id': itemid, 'quantity': quantity, 'name': product['name'], 'cost': quantity * product['unit_cost'] } cart = session.get('cart', []) newcart = [] found = False for item in cart: if item['id'] == itemid: # update the quantity if update: item['quantity'] = quantity else: item['quantity'] += quantity item['cost'] = round(item['quantity'] * product['unit_cost'], 2) if not (quantity == 0 and update): newcart.append(item) found = True else: # carry over existing item newcart.append(item) # only add if we didn't find the product in the cart already if not found and quantity > 0: newcart.append(newitem) session['cart'] = newcart session.save()
def add_to_cart(db, itemid, quantity): """This functions is what happens at the backend when the user adds a product to their cart. This performs some checks before the actual addition of the product. session - this is the session that we get using beaker, that was initialized at the start of the application the initialization of beaker is done at the very end of main.py inside main function cart - a list of dictionaries. This uses session to initialize a cart if is is not already contained in the beaker session. product - to get the details of a product based on the itemid passed. The checks performed in the functioned are as follows: 1. if product exists in the database or not 2. if the quantity selected by the user is less than or equal to what we have in the inventory 3. if the product already exists in the cart, then update the values. A list of dictionaries of products is cart in the session of beaker. The dictionary is a key value pair of the ID, Quantity, Name and Cost of a product.""" session = request.environ.get('beaker.session') cart = session.get('cart', []) product = model.product_get(db, itemid) if product: cost = product['unit_cost'] name = product['name'] inventory = product['inventory'] if product['inventory'] >= int(quantity) >= 1: if cart: index = 0 for product in cart: if product['id'] == itemid: quantity = int(product['quantity']) + int(quantity) if quantity <= inventory: cart[index] = {'id': itemid, 'quantity': int(quantity), 'name': name, 'cost': float(quantity) * cost} break else: break index += 1 if index >= len(cart): cart.append({'id': itemid, 'quantity': int(quantity), 'name': name, 'cost': float(quantity) * cost}) else: cart.append({'id': itemid, 'quantity': int(quantity), 'name': name, 'cost': float(quantity) * cost}) session['cart'] = cart session.save()
def product(db, id): # just the product data base on id product = model.product_get(db, id) # if no such product, 404 error and print 'No such Item' if not product: return HTTPError(404,'No such Item ') else: # get the data and insert to the product.html info = { 'title': product['name'], 'id': product['id'], 'name': product['name'], 'url': product['image_url'], 'inventory': product['inventory'], 'cost': product['unit_cost'], 'description': product['description'] } # return the template with the product data return template('product', info)
def index(db): """Render the index page""" session.get_or_create_session(db) # fetch all product cursor = db.cursor() cursor.execute('SELECT name from products') rows = cursor.fetchall() count = len(rows) product = {} # initalize empty data for no in range(count): product[no] = [] for no in range(count): product[no].append(model.product_get(db, no)) info = { 'title': 'The WT', 'products': product, 'number': count, } return template('index', info)
def product(db, id): # Get or set session cookie session.get_or_create_session(db) # Get cart contents from database cart = session.get_cart_contents(db) # Info relative to page info = {'page': 'product', 'cart': 0} # Get cart items count if cart: for item in cart: info['cart'] += int(item['quantity']) # Get product data info['product'] = model.product_get(db, id) # Check if product exists if info['product'] is not None: return template('product', info) else: return HTTPResponse(status=404)
def index(db): session.get_or_create_session(db) data = model.product_get(db, 3) info = {'data': data} return template('product_page', info)