def cart_index(): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) cart_items = session["cart"]["items"] products = [] subtotal = 0.0 # loop over products currently in cart for cart_item in cart_items: db_product = Product.query.get(cart_item["product"]) products.append({ "product": db_product, "quantity": cart_item["quantity"], "inventory": db_product.inventory }) subtotal += (db_product.price * int(cart_item["quantity"])) # Calculate discount user = User.query.get(session["user"]["id"]) discount = (subtotal * user.discount) total = subtotal - discount return render_template('cart/index.html', products=products, subtotal=subtotal, discount=discount, total=total)
def view(order_id): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) order = Order.query.get( order_id) #Obtain Order of the order id to find salesperson id salesperson = User.query.get( order.salesperson_id) #Obtain Salesperson so we can use his name order_items = OrderProduct.query.filter( OrderProduct.order_id == order_id ).all( ) # = Select all products from OrderProduct in recent order so we can list them grouped_products = map( lambda item: { "product": Product.query.get(item.product_id), "quantity": item.quantity }, order_items) return render_template("ratings/view.html", order=order, products=grouped_products, salesperson=salesperson.name())
def products_index(): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) if (session["user"]["type"] == 'director'): return redirect(url_for('director_dashboard')) if (session["user"]["type"] == 'manager'): return redirect(url_for('manager_dashboard')) if (session["user"]["type"] == 'salesperson'): return redirect(url_for('salesperson_dashboard')) categories = db.session.query(Product.category.distinct()).all() category = request.args.get('category') if category: products = Product.query.filter_by(category=category).all() else: products = Product.query.all() top3 = None if not category: db_result = db.engine.execute("SELECT product_id, SUM(quantity) FROM order_products GROUP BY product_id ORDER BY SUM(quantity) DESC LIMIT 3;") top3 = [] for r in db_result: top3.append(Product.query.get(r[0])) return render_template("products/index.html", categories=categories, products=products, result=top3)
def place_rating(): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) user = User.query.get(session["user"]["id"]) order_id = request.form["order_id"] # "order_id" is from the form order = Order.query.get(order_id) # Last check for if we can rate this order if order.is_rated_by_user(session["user"]["id"]): flash("You can't rate an order that you've already rated.") return redirect(url_for('products_index')) if user.type == "client": user_being_rated = order.salesperson_id elif user.type == "salesperson": user_being_rated = order.client_id rating_type = request.form["rating_type"] if rating_type == "1": rating_type = True else: rating_type = False score = user.rate(user_being_rated, user.id, order_id, rating_type) flash("Thank you for rating!") return redirect(url_for('products_index'))
def checkout_page(): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) cart_items = session["cart"]["items"] if len(cart_items) == 0: flash("You have no items in your cart!") return redirect(url_for('products_index')) products = [] subtotal = 0.0 # Make cookie into list of products to display for cart_item in cart_items: db_product = Product.query.get(cart_item["product"]) products.append({ "product": db_product, "quantity": cart_item["quantity"] }) subtotal += (db_product.price * int(cart_item["quantity"])) # Calculate discount user = User.query.get(session["user"]["id"]) discount = (subtotal * user.discount) total = subtotal - discount return render_template("checkout/index.html", subtotal=subtotal, discount=discount, products=products, total=total)
def add_to_cart(id, quantity): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) product = Product.query.get_or_404(id) quantity = request.form["quantity"] item_updated = False for item in session["cart"]["items"]: if item["product"] == product.id: print "FOUND" item["quantity"] = int(item["quantity"]) + int(quantity) item_updated = True break if not item_updated: session["cart"]["items"].append({ "product": id, "quantity": quantity }) flash("Successfully added {0} to cart.".format(product.name)) if request.args.get('return_to_home'): return redirect(url_for("products_index")) return redirect(url_for("show_product", id=product.id))
def show_product(id): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) product = Product.query.get_or_404(id) return render_template("products/show.html", product=product)
def view_user_ratings(): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) clients = sorted(User.query.filter(User.type == "client").all(), key=lambda x: x.rating(), reverse=True) salespeople = sorted(User.query.filter(User.type == "salesperson").all(), key=lambda x: x.rating(), reverse=True) return render_template("users/view_user_ratings.html", clients=clients, salespeople=salespeople)
def new_complaint(): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) user = User.query.get(session["user"]["id"]) clients = None if user.type == "salesperson": clients = user.get_clients() return render_template("complaints/new.html", clients=clients)
def rate(order_id): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) # Can we rate this order? order = Order.query.get(order_id) if order.is_rated_by_user(session["user"]["id"]): flash("You can't rate an order that you've already rated.") return redirect(url_for('products_index')) return render_template("ratings/rate.html", order_id=order_id)
def view(order_id): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) order = Order.query.get(order_id) #Obtain Order of the order id to find salesperson id salesperson = User.query.get(order.salesperson_id) #Obtain Salesperson so we can use his name order_items = OrderProduct.query.filter(OrderProduct.order_id == order_id).all() # = Select all products from OrderProduct in recent order so we can list them grouped_products = map(lambda item: { "product": Product.query.get(item.product_id), "quantity": item.quantity }, order_items) return render_template("ratings/view.html", order=order, products=grouped_products, salesperson=salesperson.name())
def create_complaint(): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) details = request.form["details"] # Validation required_fields = [details] trimmed = [i.strip() for i in required_fields] if "" in trimmed: flash("You're missing required fields.") return redirect(url_for('new_complaint')) complainer = User.query.get(session["user"]["id"]) if complainer.type == "client": other_user = complainer.get_salesperson() num_prev_complaints = len(other_user.complaints().all() ) + 1 # Adding one for the current complaint # Suspend? if num_prev_complaints % 9 == 0 and num_prev_complaints > 0: print "Suspending {0}!".format(other_user.email) other_user.suspend() # Commission decrease? Make sure we they are not suspended, that's punishment enough... if num_prev_complaints % 2 == 0 and num_prev_complaints > 0 and other_user.active: print "Decreasing commission for {0}!".format(other_user.email) other_user.commission = other_user.commission * 0.9 db.session.commit() if complainer.type == "salesperson": other_user = User.query.get(request.form["client"]) # Blacklist? num_prev_complaints = len(other_user.complaints().all() ) + 1 # Adding one for the current complaint if num_prev_complaints % 2 == 0 and num_prev_complaints > 0: print "Blacklisting {0}!".format(other_user.email) other_user.blacklist() complaint = Complaint(other_user.id, complainer.id, details, datetime.now()) db.session.add(complaint) db.session.commit() flash("You have successfully complained against {0}".format( other_user.name())) return redirect(url_for("products_index"))
def cart_delete(id): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) product_dict = None for item in session["cart"]["items"]: if item["product"] == id: product_dict = item break session["cart"]["items"].remove(product_dict) return redirect(url_for('cart_index'))
def cart_edit(): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) product_id = request.form["product_id"] quantity = request.form["quantity"] # product_dict = None for item in session["cart"]["items"]: if item["product"] == int(product_id): item["quantity"] = quantity break return redirect(url_for("cart_index"))
def rating_index(): (valid, error) = check_user_validity() if not valid: flash(error) return redirect(url_for('login')) # Get all orders relevant to the current user user = User.query.get(session["user"]["id"]) if user.type == "client": orders = Order.query.filter(Order.client_id == user.id).all() elif user.type == "salesperson": orders = Order.query.filter(Order.salesperson_id == user.id).all() # Further filter orders based on if the current user rated them # orders = [i for i in orders if not i.is_rated_by_user(session["user"]["id"])] return render_template("ratings/index.html", orders=orders, user_type=user.type, id=session["user"]["id"])