-
Notifications
You must be signed in to change notification settings - Fork 0
/
shoppingsite.py
128 lines (81 loc) · 3.28 KB
/
shoppingsite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Ubermelon shopping application Flask server.
Provides web interface for browsing melons, seeing detail about a melon, and
put melons in a shopping cart.
Authors: Joel Burton, Christian Fernandez, Meggie Mahnken.
"""
from flask import Flask, render_template, redirect, flash, session
import jinja2
import melons
app = Flask(__name__)
# Need to use Flask sessioning features
app.secret_key = 'this-should-be-something-unguessable'
# Normally, if you refer to an undefined variable in a Jinja template,
# Jinja silently ignores this. This makes debugging difficult, so we'll
# set an attribute of the Jinja environment that says to make this an
# error.
app.jinja_env.undefined = jinja2.StrictUndefined
@app.route("/")
def index():
"""Return homepage."""
return render_template("homepage.html")
@app.route("/melons")
def list_melons():
"""Return page showing all the melons ubermelon has to offer"""
melon_list = melons.get_all()
return render_template("all_melons.html",
melon_list=melon_list)
@app.route("/melon/<int:melon_id>")
def show_melon(melon_id):
"""Return page showing the details of a given melon.
Show all info about a melon. Also, provide a button to buy that melon.
"""
melon = melons.get_by_id(melon_id)
print melon
return render_template("melon_details.html",
display_melon=melon)
@app.route("/cart")
def shopping_cart():
"""Display content of shopping cart."""
session['melon_cart'] = {}
# TODO: Display the contents of the shopping cart.
# The logic here will be something like:
#
# - get the list-of-ids-of-melons from the session cart
# - loop over this list:
# - keep track of information about melon types in the cart
# - keep track of the total amt ordered for a melon-type
# - keep track of the total amt of the entire order
# - hand to the template the total order cost and the list of melon types
return render_template("cart.html")
@app.route("/add_to_cart/<int:id>")
def add_to_cart(id):
"""Add a melon to cart and redirect to shopping cart page.
When a melon is added to the cart, redirect browser to the shopping cart
page and display a confirmation message: 'Successfully added to cart'.
"""
# TODO: Finish shopping cart functionality
# The logic here should be something like:
#
# - add the id of the melon they bought to the cart in the session
return "Oops! This needs to be implemented!"
@app.route("/login", methods=["GET"])
def show_login():
"""Show login form."""
return render_template("login.html")
@app.route("/login", methods=["POST"])
def process_login():
"""Log user into site.
Find the user's login credentials located in the 'request.form'
dictionary, look up the user, and store them in the session.
"""
# TODO: Need to implement this!
return "Oops! This needs to be implemented"
@app.route("/checkout")
def checkout():
"""Checkout customer, process payment, and ship melons."""
# For now, we'll just provide a warning. Completing this is beyond the
# scope of this exercise.
flash("Sorry! Checkout will be implemented in a future version.")
return redirect("/melons")
if __name__ == "__main__":
app.run(debug=True)