Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
bookshop_project_A/checkout.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
125 lines (119 sloc)
6.52 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from main import app | |
from flask import Flask, session, g, redirect, url_for, render_template, abort, flash, make_response, request | |
import sqlite3 | |
from functools import wraps | |
# ALWAYS check user session valid | |
@app.before_request | |
def user_logged_in(): | |
user_id = session.get('access_mode') | |
if user_id is None: | |
# Session not found | |
g.user = None | |
else: | |
g.user= user_id | |
@app.route('/shopping_cart', methods = ['GET','POST']) | |
def shopping_cart(): | |
if request.method == 'POST': | |
if request.form['Submit'] == 'GO BACK': | |
return redirect(url_for('homepage')) | |
# 'cancel' the shopping cart | |
elif request.form['Submit'] == 'CLEAR CART': | |
# Keep access_mode or user will be logged out, cart session cleared | |
remember_user = session['access_mode'] | |
session.clear() | |
session['access_mode'] = remember_user | |
return redirect(url_for('homepage')) | |
elif request.form['Submit'] == 'CHECKOUT': | |
return redirect(url_for('checkout')) | |
else: | |
return render_template('cart.html', page=url_for('shopping_cart')) | |
# not in homepage.py with add_book because seperate page alltogether | |
# help from 5001CEM https://coventry.aula.education/?#/dashboard/d0f2228a-9eff-445e-953d-ebf4082fa218/journey/materials/2d09838b-a7da-45dd-9ff0-667c0f1e5a95 | |
@app.route('/shopping_cart/remove', methods = ['POST']) | |
def remove(): | |
_isbn = request.form['isbn'] | |
session['book'][_isbn]['quantity'] -= 1 | |
session['book'][_isbn]['total_individual_price'] -= session['book'][_isbn]['price'] | |
session['total_quantity'] -= 1 | |
session['total_price'] -= session['book'][_isbn]['price'] | |
# if ALL instances of session['book'][_isbn] no longer exist: | |
if session['book'][_isbn]['quantity'] == 0: | |
#create row | |
book_val = (_isbn,session['book'][_isbn]) | |
#in the code below, we create a new session['book'] and put there all elements, except the one that has 0 quantity in our basket | |
# we do it by iterating over our session['book'] and put all of the elements that is not the one that we have just removed the last copy of, in a new list | |
session['book'] = dict([i for i in list(session['book'].items()) if i != book_val]) | |
# if book qunatity is 0 there are no books | |
if session['total_quantity'] == 0: | |
session.pop('book',None) | |
# template will cause error if no books in cart as 'book' would be empty, so must redirect | |
return redirect(url_for('homepage')) | |
return redirect(url_for('shopping_cart')) | |
# end of borrowed code | |
@app.route('/checkout', methods = ['GET','POST']) | |
def checkout(): | |
# help from 5001CEM https://coventry.aula.education/?#/dashboard/d0f2228a-9eff-445e-953d-ebf4082fa218/journey/materials/2d09838b-a7da-45dd-9ff0-667c0f1e5a95 | |
if request.method == 'POST': | |
if request.form['button_chooser'] == 'sold_out': | |
return redirect(url_for('homepage')) | |
elif request.form['button_chooser'] == 'proceed': | |
return redirect(url_for('payment')) | |
else: | |
# For every book selected: | |
for key, value in session['book'].items(): | |
# if session quantity > qty_left (pulled from db col 8) | |
if session['book'][key]['qty_left'] < session['book'][key]['quantity']: | |
books_over = session['book'][key]['quantity'] - session['book'][key]['qty_left'] | |
# Ammend checkout and update prices | |
session['total_quantity'] -= books_over | |
session['total_price'] -= session['book'][key]['price'] * books_over | |
session['book'][key]['total_individual_price'] -= session['book'][key]['price'] * books_over | |
session['book'][key]['quantity'] = session['book'][key]['qty_left'] | |
# Sold out books | |
if 'sold_out' in session: | |
session['sold_out'].append([session['book'][key]['title']]) | |
else: | |
session['sold_out'] = [session['book'][key]['title']] | |
if session['book'][key]['quantity'] == 0: | |
book_val = (key,value) | |
# Sold out book removed | |
session['book'] = dict([i for i in list(session['book'].items()) if i != book_val]) | |
if 'sold_out' in session: | |
# Pretty much storing all the missing books for user message | |
session['sold_out'] = ', '.join(str(z) for z in session['sold_out']) | |
# If first book is 3 and all are + 1 then postal cost always = number of books + 2 | |
_post_cost = session['total_quantity'] + 2 | |
session['total_cost'] = session['total_price'] + _post_cost | |
session['total_cost'] = "{:.1f}".format(float(session['total_cost'])) | |
session['total_cost'] = float(session['total_cost']) | |
return render_template('checkout.html',page=url_for('checkout'), post_cost = _post_cost) | |
@app.route('/checkout/payment') | |
def payment(): | |
# Dummy template with no validation or verification - in alignment with req | |
return render_template('payment.html') | |
# Payment complete screen | |
@app.route('/checkout/payment/complete', methods=["GET", "POST"]) | |
def dummy_payment(): | |
if request.method == "POST": | |
con = sqlite3.connect('bookshop_database.db') | |
con.row_factory = lambda cursor, row: row[0] | |
cur = con.cursor() | |
# For EVERY book thats in cart update the quantity | |
# Note this only happens after payment has COMPLETED to stop books dissapearing | |
for key, value in session['book'].items(): | |
# Select the row with the same isbn value | |
cur.execute('SELECT quantity FROM books WHERE isbn13 = ? ', (key, )) | |
before_qty = cur.fetchone() | |
# Take cart quantity value from the current qnty | |
upd_quantity = int(before_qty) - session['book'][key]['quantity'] | |
# Update row to new quantity | |
cur.execute('UPDATE BOOKS set quantity=? WHERE isbn13=?',(upd_quantity, key)) | |
con.commit() | |
con.close() | |
# Clearing session in order to clear basket | |
remember_user = session['access_mode'] | |
session.clear() | |
session['access_mode'] = remember_user | |
flash('Thank you for your purchase') | |
# Basic template | |
return render_template('payment_done.html') |