-
Notifications
You must be signed in to change notification settings - Fork 0
/
shoppingsite.py
169 lines (110 loc) · 4.38 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""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."""
# 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
melons_in_cart = session['cart']
cart_info = {}
total = 0
for melon_id in melons_in_cart:
melon = melons.get_by_id(melon_id)
cart_info.setdefault(melon_id, [melon.common_name,
melon.price, 0, 0])
# quantity = cart_info[melon_id][2]
# subtotal = cart_info[melon_id][3]
cart_info[melon_id][2] += 1
cart_info[melon_id][3] += float(melon.price)
total += float(melon.price)
cart_info = sorted(list(cart_info.values()))
print "#########"
print "TOTAL", total
print "#########"
print "#########"
print "CART LIST", cart_info
print "#########"
return render_template("cart.html",cart_info=cart_info,total=total)
@app.route("/add_to_cart/<int:melon_id>")
def add_to_cart(melon_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
melon = melons.get_by_id(melon_id)
# Check if the melon ID exists in the session. If it doesn't, add
# the ID as a key with a quantity of 0 as its value, and add 1.
# Otherwise, add 1 to the quantity.
session.setdefault("cart", []).append(melon_id)
print "#########"
print melon
print "#########"
print session
print "#########"
flash("You have added {} to your cart.".format(melon.common_name))
return redirect('/melons')
@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)