def getShoppingCart(userId): result = None if userId is not None: try: conn = mysql.connector.connect(**config) cursor = conn.cursor() query = "SELECT product_id, count FROM shopcarts WHERE user_id = %s" cursor.execute(query, [userId]) #Build Cart object from results for (product_id, count) in cursor: if result is None: result = Cart(userId) result.products[product_id] = count except mysql.connector.Error as e: print("Error in product update: {}".format(e)) finally: if cursor: cursor.close() if conn: conn.close() return result
def test_cart_add(): print("Testing cart add..") cart = Cart(79) cart.addProduct(1) cart.addProduct(1) db.updateShoppingCart(cart) cart2 = db.getShoppingCart(79) assert(cart2 is not None) bl.setShoppingCart(cart)
def on_put(self, req, resp, userId): try: userId = int(userId) """Skip the validations because we dont have yet way of adding user accounts to the system..""" doc = req.context['doc'] newCart = Cart.from_json(doc) if newCart.userId is None: newCart.userId = userId elif newCart.userId != userId: resp.status = falcon.HTTP_403 return #update product bl.setShoppingCart(newCart) resp.status = falcon.HTTP_200 #Status Ok except: #Return 500 - internal error resp.status = falcon.HTTP_500
def main(): products = db.get_products() cart = Cart() while True: command = input("Command: ") if command == "cart": show_cart(cart) elif command == "add": add_item(cart, products) elif command == "del": remove_item(cart) elif command == "exit": print("Bye!") break else: print("Not a valid command. Please try again.\n")
def ram_page(): from objects import Ram, UserAccount, Cart username = session['username'] if request.method == 'POST': ramId = request.form['id'] username = request.form['username'] ram = Ram.query.filter_by(id=ramId).first() user = UserAccount.query.filter_by(username=username).first() if user != None: cart = Cart.query.filter_by(userid=user.id).first() if cart == None: userCart = Cart(user.id, user.id, None, None, None, ramId) database.session.add(userCart) database.session.commit() else: cart.ramid = ram.id database.session.commit() return render_template('productTemplate.html', table=Ram, username=username)
def mobo_page(): from objects import MotherBoard, UserAccount, Cart username = session['username'] if request.method == 'POST': moboId = request.form['id'] username = request.form['username'] mobo = MotherBoard.query.filter_by(id=moboId).first() user = UserAccount.query.filter_by(username=username).first() if user != None: cart = Cart.query.filter_by(userid=user.id).first() if cart == None: userCart = Cart(user.id, user.id, None, None, None, None, moboId, None) database.session.add(userCart) database.session.commit() else: cart.motherboardid = mobo.id database.session.commit() return render_template('productTemplate.html', table=MotherBoard, username=username)
def hardDive_page(): from objects import HardDrive, UserAccount, Cart username = session['username'] if request.method == 'POST': harddriveId = request.form['id'] username = request.form['username'] hardDrive = HardDrive.query.filter_by(id=harddriveId).first() user = UserAccount.query.filter_by(username=username).first() if user != None: cart = Cart.query.filter_by(userid=user.id).first() if cart == None: userCart = Cart(user.id, user.id, None, None, None, None, None, harddriveId) database.session.add(userCart) database.session.commit() else: cart.harddriveid = hardDrive.id database.session.commit() return render_template('productTemplate.html', table=HardDrive, username=username)
def gpu_page(): from objects import GPU, UserAccount, Cart username = session['username'] if request.method == 'POST': gpuId = request.form['id'] username = request.form['username'] gpu = GPU.query.filter_by(id=gpuId).first() user = UserAccount.query.filter_by(username=username).first() if user != None: cart = Cart.query.filter_by(userid=user.id).first() if cart == None: userCart = Cart(user.id, user.id, gpuId, None, None, None, None, None) database.session.add(userCart) database.session.commit() else: cart.gpuid = gpu.id database.session.commit() return render_template('productTemplate.html', table=GPU, username=username)
def psu_page(): from objects import Powersupply, UserAccount, Cart username = session['username'] if request.method == 'POST': psuId = request.form['id'] username = request.form['username'] psu = Powersupply.query.filter_by(id=psuId).first() user = UserAccount.query.filter_by(username=username).first() if user != None: cart = Cart.query.filter_by(userid=user.id).first() if cart == None: userCart = Cart(user.id, user.id, None, None, psuId, None, None, None) database.session.add(userCart) database.session.commit() else: cart.powersupplyid = psu.id database.session.commit() return render_template('productTemplate.html', table=Powersupply, username=username)
def cpu_page(): from objects import CPU, UserAccount, Cart username = session['username'] if request.method == 'POST': cpuId = request.form['id'] # from productsPage username = request.form['username'] cpu = CPU.query.filter_by(id=cpuId).first() user = UserAccount.query.filter_by(username=username).first() if user != None: cart = Cart.query.filter_by(userid=user.id).first() if cart == None: # if cart exist does not, then exist it you and add item to it. userCart = Cart(user.id, user.id, None, cpu.id, None, None, None, None) database.session.add(userCart) database.session.commit() else: cart.cpuid = cpu.id database.session.commit() return render_template('productTemplate.html', table=CPU, username=username)
def test_cart_serialize(): p = db.getShoppingCart(userId=49) print(p) print(json.dumps(Cart.serialize(p), sort_keys=True))