Select Git revision
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
routes.py 10.02 KiB
from flask import render_template, redirect, url_for, request
from flask_login import login_required, login_user, logout_user, current_user
from .databases import User
from .forms import LoginForm
from . import app, lm
from .databases import product_table
from .databases import review_table
from .forms import orderByForm
from .forms import SignupForm
from .forms import addToCart
from . import db
from .forms import removeFormCart
from .forms import removeFormWishlist
from .forms import checkoutForm
from .forms import typeViewForm
from .forms import reviewForm
# renders the login page
# if the form is validated and submitted it logs the user in
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.verify_password(form.password.data):
return redirect(url_for('login', **request.args))
login_user(user, form.remember_me.data)
return redirect(url_for('home'))
return render_template('login.html', form=form)
# renders the signup page
# if the signup for is validated and submitted it adds a new user with the details provided so they can log in
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if form.validate_on_submit():
password_in = str(form.password.data)
username_in = str(form.username.data)
if User.query.filter_by(username=username_in).first() is None:
User.register(username_in, password_in)
return redirect(url_for('login'))
return render_template('signup.html', form=form)
# logs out the user when pressed and takes the user to the home page
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('home'))
# takes the user to the home page
@app.route('/')
def home():
return render_template('home.html')
@app.route('/shopping', methods=['GET', 'POST'])
def shopping():
products_pass = product_table.query.all()
order_by_form = orderByForm()
type_form = typeViewForm()
# if the order form has been submitted it changes the data for the products passed in so they are in the order selected
if order_by_form.validate_on_submit() and order_by_form.is_submitted():
order_show = str(order_by_form.order.data)
if order_show == 'Price Ascending':
products_pass = product_table.query.order_by(product_table.product_price).all()
elif order_show == 'Price Descending':
products_pass = product_table.query.order_by(product_table.product_price.desc()).all()
elif order_show == 'Default':
products_pass = product_table.query.all()
# if the type form is submitted it changes the input data so that it includes just the type submitted
elif type_form.validate_on_submit() and type_form.is_submitted():
type_in = type_form.type.data
if type_in == 'All':
product_table.query.all()
elif type_in == 'Fork':
products_pass = product_table.query.filter_by(product_type = 'fork')
elif type_in == 'Frame':
products_pass = product_table.query.filter_by(product_type = 'frame')
# if the one of the add to cart or add to wishlist buttons are pressed it adds the item to cart depending on the item selected
elif request.method == 'POST':
try:
item_to_add_cart = request.form['item_id_cart']
for user_row in db.session.query(User).filter_by(id=current_user.get_id()):
user_row.cart = user_row.cart + item_to_add_cart + ' '
db.session.commit()
except:
item_to_add_wishlist = request.form['item_id_wishlist']
for user_row in db.session.query(User).filter_by(id=current_user.get_id()):
user_row.wishlist = user_row.wishlist + item_to_add_wishlist + ' '
db.session.commit()
# renders the shopping page
return render_template('shopping.html', product_table = products_pass, order_by_form = order_by_form, type_form = type_form)
@app.route('/cart', methods=['GET', 'POST'])
@login_required
def cart():
cart_string = current_user.cart
cart_id_list = cart_string.split()
cart_list = []
for item_id in cart_id_list:
items = db.session.query(product_table).all()
for item in items:
if item.product_id == int(item_id):
cart_list.append(item)
remove_form = removeFormCart()
if remove_form.validate_on_submit() and remove_form.validate():
remove_item_id = remove_form.item_id_remove.data
clear_all = remove_form.clear_all.data
if remove_item_id in cart_id_list:
cart_id_list.remove(remove_item_id)
new_cart_string = ''
for item_id in cart_id_list:
new_cart_string = new_cart_string + item_id + ' '
for c in db.session.query(User).filter_by(id=current_user.get_id()):
c.cart = new_cart_string
db.session.commit()
if clear_all:
for c in db.session.query(User).filter_by(id=current_user.get_id()):
c.cart = ''
db.session.commit()
return redirect(url_for('cart'))
total_price = get_total_cart_price()
free_shipping = False
shipping_price = 14.99
if total_price >= 2000:
free_shipping = True
if free_shipping:
shipping_price = 0
final_price = shipping_price + total_price
return render_template('cart.html', cart_list = cart_list, cart_size = len(cart_list), total_price = total_price,
remove_form = remove_form, shipping_price = shipping_price ,final_price = final_price)
@app.route('/wishlist', methods=['GET', 'POST'])
@login_required
def wishlist():
wishlist_string = current_user.wishlist
wishlist_id_list = wishlist_string.split()
wishlist_list = []
for item_id in wishlist_id_list:
items = db.session.query(product_table).all()
for item in items:
if item.product_id == int(item_id):
wishlist_list.append(item)
total_price = 0
for item in wishlist_list:
total_price += item.product_price
remove_form = removeFormWishlist()
if remove_form.validate_on_submit():
remove_item_id = remove_form.item_id_remove.data
clear_all = remove_form.clear_all.data
if remove_item_id in wishlist_id_list:
wishlist_id_list.remove(remove_item_id)
new_wishlist_string = ''
for item_id in wishlist_id_list:
new_wishlist_string = new_wishlist_string + item_id + ' '
for c in db.session.query(User).filter_by(id=current_user.get_id()):
c.wishlist = new_wishlist_string
db.session.commit()
if clear_all:
for c in db.session.query(User).filter_by(id=current_user.get_id()):
c.wishlist = ''
db.session.commit()
return redirect(url_for('wishlist'))
return render_template('wishlist.html', wishlist_list = wishlist_list, wishlist_size = len(wishlist_list), total_price = total_price, remove_form = remove_form)
@app.route('/item', methods=['GET', 'POST'])
def item_page():
id = request.args.get('id', default='1', type=int)
all_products = product_table.query.all()
for product in all_products:
if product.product_id == id:
product_in = product
cartForm = addToCart()
if cartForm.validate_on_submit():
for c in db.session.query(User).filter_by(id = current_user.get_id()):
if cartForm.choice.data == 'Cart':
c.cart = c.cart + str(product_in.product_id) + ' '
else:
c.wishlist = c.wishlist + str(product_in.product_id) + ' '
db.session.commit()
review_form = reviewForm()
if review_form.validate_on_submit():
review_in = review_form.review.data
for c in db.session.query(User).filter_by(id=current_user.get_id()):
current_user_username = c.username
review_table.new_review(review_in=review_in, product_id_in=product_in.product_id, author_in = current_user_username)
reviews = review_table.query.filter_by(product_id=product_in.product_id)
return render_template('item_page.html', product = product_in, cartForm = cartForm, reviewForm = review_form, reviews = reviews)
@app.route('/checkout', methods=['GET', 'POST'])
@login_required
def checkout():
form = checkoutForm()
# checks for validation of checkout form and if validated clears the cart for the current user and returns to the thank you page
if form.validate_on_submit():
for c in db.session.query(User).filter_by(id = current_user.get_id()):
c.cart = ''
db.session.commit()
return redirect(url_for('thank_you'))
# gets the total price of items using the get_total_price function
total_price = get_total_cart_price()
free_shipping = False
shipping_price = 14.99
if total_price >= 2000:
free_shipping = True
if free_shipping:
shipping_price = 0
final_price = shipping_price + total_price
return render_template('checkout.html', total_price = total_price, form = form, shipping_price = shipping_price ,final_price = final_price)
def get_total_cart_price():
cart_string = current_user.cart
cart_id_list = cart_string.split()
cart_list = []
# gets all of the items in the cart and adds them to a list
for item_id in cart_id_list:
items = db.session.query(product_table).all()
for item in items:
if item.product_id == int(item_id):
cart_list.append(item)
# loops through the previously created cart list and adds the price of each item to the total price to be passed in
total_price = 0
for item in cart_list:
total_price += item.product_price
return total_price
@app.route('/thank_you', methods=['GET', 'POST'])
@login_required
def thank_you():
return render_template('thank_you.html')
@lm.user_loader
def load_user(id):
return User.query.get(int(id))