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?
6005CEM/cw1_2
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (33 sloc)
1.6 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
@app.route("/basket/payment", methods=["GET", "POST"]) | |
def pay(): | |
""" | |
Fake paymeent. | |
YOU DO NOT NEED TO IMPLEMENT PAYMENT | |
""" | |
if not flask.session["user"]: | |
flask.flash("You need to be logged in") | |
return flask.redirect(flask.url_for("index")) | |
#Get the total cost | |
cost = 0 | |
#Fetch USer ID from Sssion | |
theQry = "Select * FROM User WHERE id = {0}".format(flask.session["user"]) | |
theUser = query_db(theQry, one=True) | |
#Add products to the user | |
sessionBasket = flask.session.get("basket", None) | |
theDate = datetime.datetime.utcnow() | |
for key in sessionBasket: | |
#As we should have a trustworthy key in the basket. | |
theQry = "INSERT INTO PURCHASE (userID, productID, date) VALUES ({0},{1},'{2}')".format(theUser['id'], | |
key, | |
theDate) | |
theQry2 = f"SELECT * FROM product WHERE id = {key}" | |
theItem = query_db(theQry2, one=True) | |
quantity = int(sessionBasket[key]) | |
thePrice = theItem["price"] * quantity | |
cost += thePrice | |
app.logger.debug(theQry) | |
write_db(theQry) | |
#Clear the Session | |
flask.session.pop("basket", None) | |
return flask.render_template("pay.html", | |
total=cost) |