-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathapp.py
More file actions
223 lines (189 loc) · 5.79 KB
/
app.py
File metadata and controls
223 lines (189 loc) · 5.79 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Main file for Shirts4Mike
# Import statement
from flask import (
Flask,
render_template,
Markup,
url_for,
flash,
redirect,
request
)
import sendgrid
from datetime import date
# App setup
app = Flask(__name__)
app.config["SECRET_KEY"] = "some_really_long_random_string_here"
# Get details for sendgrid details
sendgrid_file = "sendgrid.txt"
sendgrid_details = []
with open(sendgrid_file) as f:
sendgrid_details = f.readlines()
sendgrid_details = [x.strip("\n") for x in sendgrid_details]
# Global Variables
products_info = [
{
"id": "101",
"name": "Logo Shirt, Red",
"img": "shirt-101.jpg",
"price": 18,
"paypal": "LNRBY7XSXS5PA",
"sizes": ["Small", "Medium", "Large"]
},
{
"id": "102",
"name": "Mike the Frog Shirt, Black",
"img": "shirt-102.jpg",
"price": 20,
"paypal": "XP8KRXHEXMQ4J",
"sizes": ["Small", "Medium", "Large"]
},
{
"id": "103",
"name": "Mike the Frog Shirt, Blue",
"img": "shirt-103.jpg",
"price": 20,
"paypal": "95C659J3VZGNJ",
"sizes": ["Small", "Medium", "Large"]
},
{
"id": "104",
"name": "Logo Shirt, Green",
"img": "shirt-104.jpg",
"price": 18,
"paypal": "Z5EY4SJN64SLU",
"sizes": ["Small", "Medium", "Large"]
},
{
"id": "105",
"name": "Mike the Frog Shirt, Yellow",
"img": "shirt-105.jpg",
"price": 25,
"paypal": "RYAGP5EWG4V4G",
"sizes": ["Small", "Medium", "Large"]
},
{
"id": "106",
"name": "Logo Shirt, Gray",
"img": "shirt-106.jpg",
"price": 20,
"paypal": "QYHDD4N4SMUKN",
"sizes": ["Small", "Medium", "Large"]
},
{
"id": "107",
"name": "Logo Shirt, Teal",
"img": "shirt-107.jpg",
"price": 20,
"paypal": "RSDD7RPZFPQTQ",
"sizes": ["Small", "Medium", "Large"]
},
{
"id": "108",
"name": "Mike the Frog Shirt, Orange",
"img": "shirt-108.jpg",
"price": 25,
"paypal": "LFRHBPYZKHV4Y",
"sizes": ["Small", "Medium", "Large"]
}
]
# Functions
def get_list_view_html(product):
"""Function to return html for given shirt
The product argument should be a dictionary in this structure:
{
"id": "shirt_id",
"name": "name_of_shirt",
"img": "image_name.jpg",
"price": price_of_shirt_as_int_or_flat,
"paypal": "paypal_id"
"sizes": ["array_of_sizes"]
}
The html is returned in this structure:
<li>
<a href="shirt/shirt_id">
<img src="/static/shirt_img" alt="shirt_name">
<p>View Details</p>
</a>
</li>
"""
output = ""
image_url = url_for("static", filename=product["img"])
shirt_url = url_for("shirt", product_id=product["id"])
output = output + "<li>"
output = output + '<a href="' + shirt_url + '">'
output = (
output + '<img src="' + image_url +
'" al t="' + product["name"] + '">')
output = output + "<p>View Details</p>"
output = output + "</a>"
output = output + "</li>"
return output
# Routes
# All functions should have a page_title variables if they render templates
@app.route("/")
def index():
"""Function for Shirts4Mike Homepage"""
context = {"page_title": "Shirts 4 Mike", "current_year": date.today().year}
counter = 0
product_data = []
for product in products_info:
counter += 1
if counter < 5: # Get first 4 shirts
product_data.append(
Markup(get_list_view_html(product))
)
context["product_data"] = Markup("".join(product_data))
flash("This site is a demo do not buy anything")
return render_template("index.html", **context)
@app.route("/shirts")
def shirts():
"""Function for the Shirts Listing Page"""
context = {"page_title": "Shirts 4 Mike", "current_year": date.today().year}
product_data = []
for product in products_info:
product_data.append(Markup(get_list_view_html(product)))
context["product_data"] = Markup("".join(product_data))
flash("This site is a demo do not buy anything")
return render_template("shirts.html", **context)
@app.route("/shirt/<product_id>")
def shirt(product_id):
"""Function for Individual Shirt Page"""
context = {"page_title": "Shirts 4 Mike", "current_year": date.today().year}
my_product = ""
for product in products_info:
if product["id"] == product_id:
my_product = product
context["product"] = my_product
flash("This site is a demo do not buy anything")
return render_template("shirt.html", **context)
@app.route("/receipt")
def receipt():
"""Function to display receipt after purchase"""
context = {"page_title": "Shirts 4 Mike", "current_year": date.today().year}
return render_template("receipt.html", **context)
@app.route("/contact")
def contact():
"""Function for contact page"""
context = {"page_title": "Shirts 4 Mike", "current_year": date.today().year}
return render_template("contact.html", **context)
# Route to send email
@app.route("/send", methods=['POST'])
def send():
"""Function to send email using sendgrid API"""
sendgrid_object = sendgrid.SendGridClient(
sendgrid_details[0], sendgrid_details[1])
message = sendgrid.Mail()
sender = request.form["email"]
subject = request.form["name"]
body = request.form["message"]
message.add_to("charlie.thomas@attwoodthomas.net")
message.set_from(sender)
message.set_subject(subject)
message.set_html(body)
sendgrid_object.send(message)
flash("Email sent.")
return redirect(url_for("contact"))
# Run application
if __name__ == "__main__":
app.run(debug=True)