def cart_add_submission(): id_ = request.get_json('id') # TODO: turn this process of getting id into a decorator (used for remove too) if not id_: return jsonify({'result': False}) # add the id to the current users cart user = get_user_instance(db, User) if not user: return jsonify({'result': False}) product = db.session.query(Product).get(id_) if product.total_stock == 0: return jsonify({ 'result': False, 'message': f'Product "{product.name}" is out of STOCK!' }) try: user.cart.products.append(db.session.query(Product).get(id_)) user.cart.amount += 1 db.session.commit() return jsonify({'result': True}) except Exception as e: print(e) db.session.rollback() return jsonify({'result': False})
def get_cart_amount(): try: curr_user = get_user_instance(db, User) # TODO make logged_in decorator check for existing user, not just that the session contains a user return jsonify({'amount': curr_user.cart.amount}) except Exception as e: print(e) flash('A problem occurred when attempting to get your cart amount', 'error') return redirect('/')
def clear_cart(): try: user = get_user_instance(db, User) products = user.cart.products length = len(products) # think about a pop() method but in a first in first out stack # popping out first item until length is 0 while length > 0: products.remove(products[0]) length -= 1 user.cart.amount = 0 db.session.commit() flash('Cleared your cart', 'success') return redirect('/') except Exception as e: print(e) db.session.rollback() flash('Could not clear your cart', 'error') return redirect('/cart')
def cart_remove_submission(): id_ = request.get_json('id') if not id_: return jsonify({'result': False}) user = get_user_instance(db, User) if not user: return jsonify({'result': False}) try: # basically, we do not want to remove the product itself, because other users rely on it too. So intead, we want to remove the product from our assocation table called cart_products. # NOOOOOOOOOOOO total = user.cart.products.remove(db.session.query(Product).get(id_)) user.cart.amount -= 1 # TODO truly understand how you removed, like does remove take in instances of a query product class object? Also does remove() return the amoutns of items removed? # TODO: say the user has multiple items of a thing, remove x amount of items only. db.session.commit() return jsonify({'result': True}) except Exception as e: print(e) db.session.rollback() # vital db.session.close() return jsonify({'result': False})
def delete_submission(account_id): # find the account user = get_user_instance(db, User) print(user) if user.id != session.get('userid'): flash('You\'re are not allowed to delete other users accounts', 'error') # actually going to sign the particular person out return redirect('/signout') try: # delete the account db.session.delete(user) db.session.commit() flash('Account Deleted.', 'success') # clear the session session.clear() return redirect('/') except Exception as e: print(e) db.session.rollback() flash('Could not delete your account. Try again.', 'error') return redirect('/account')
def cart(): user = get_user_instance(db, User) return render_template('/pages/cart.html', products=user.cart.products, userid=session.get('userid'))
def exist_in_cart(product_id): user = get_user_instance(db, User) for product in user.cart.products: if product.id == product_id: return jsonify({'result': True}) return jsonify({'result': False})