-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
175 lines (163 loc) · 5.9 KB
/
application.py
File metadata and controls
175 lines (163 loc) · 5.9 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
#import necessary libraries
import os
import sqlite3
from flask import Flask, render_template, request, make_response, redirect
app = Flask(__name__)
#app route to home page
@app.route("/")
def index():
return render_template("index.html")
#app route to about page
@app.route("/about")
def about():
return render_template("about.html")
#app route to rent page and rent subpages
@app.route("/rent/")
@app.route("/rent/<type>/")
def rent(type=1):
#connect to rentals database
connection=sqlite3.connect("rentals.db")
cursor=connection.cursor()
cursor.execute("SELECT * FROM rentals")
rentals=cursor.fetchall()
#initialize rental info lists
names=[]
descriptions=[]
types=[]
costs_hour=[]
costs_day=[]
ids=[]
length=0
type_indicator=-1
#identify type of rental requested by extension
if type=="kayak":
type_indicator=1
if type=="canoe":
type_indicator=2
if type=="paddleboard":
type_indicator=3
#add data to lists from database
for i in range(len(rentals)):
if type_indicator==rentals[i][2] or type_indicator==-1:
names.append(rentals[i][0])
descriptions.append(rentals[i][1])
types.append(rentals[i][2])
costs_hour.append(rentals[i][3])
costs_day.append(rentals[i][4])
ids.append(rentals[i][5])
length+=1
return render_template("rent.html", names=names, descriptions=descriptions, types=types, costs_hour=costs_hour, costs_day=costs_day, length=length, ids=ids)
#app route to cart page, with add to cart function
@app.route("/cart")
def cart():
if 'cart_items' in request.cookies:
#connect to database
connection=sqlite3.connect("rentals.db")
cursor=connection.cursor()
cursor.execute("SELECT * FROM rentals")
rentals=cursor.fetchall()
#initialize necessary data lists
names=[]
costs_hour=[]
costs_day=[]
ids=[]
#add to list necessary data
for i in range(len(rentals)):
names.append(rentals[i][0])
costs_hour.append(rentals[i][3])
costs_day.append(rentals[i][4])
ids.append(rentals[i][5])
#initialize additional necessary data lists
item_list=[]
name_list=[]
quantity_list=[]
cost_hour_list=[]
cost_day_list=[]
#retrieve cookie from browser
cookie=str(request.cookies.get('cart_items'))
#dissect info in cookie
the_list=cookie.split()
for i in range(len(the_list)):
item_list.append(int(the_list[i][0:3]))
print(the_list[i][0:3])
quantity_list.append(int(the_list[i][4:]))
length=len(the_list)
#add to lists used for table in my cart
for i in range(len(the_list)):
name_list.append(names[ids.index(item_list[i])])
cost_hour_list.append(costs_hour[ids.index(item_list[i])])
cost_day_list.append(costs_day[ids.index(item_list[i])])
return render_template("cart.html", name_list=name_list, cost_hour_list=cost_hour_list, cost_day_list=cost_day_list, quantity_list=quantity_list, length=length)
else:
length=0
return render_template("cart.html", length=length)
@app.route("/addcart", methods=["POST"])
def addcart():
#create additional info for cookie
new_cookie=(request.form.get("item")+":"+request.form.get("quantity")+" ")
#add to cookie if cookie already exists
if 'cart_items' in request.cookies:
new_cookie=request.cookies.get('cart_items')+new_cookie
#set cookie
resp=make_response(redirect("/cart"))
resp.set_cookie('cart_items', new_cookie)
return resp
#app route for checkout page
@app.route("/checkout")
def checkout():
return render_template("checkout.html")
#app route for checkout finalize page
@app.route("/checkoutfinalize")
def checkoutfinalize():
if 'cart_items' in request.cookies:
#connect to database
connection=sqlite3.connect("rentals.db")
cursor=connection.cursor()
cursor.execute("SELECT * FROM rentals")
rentals=cursor.fetchall()
#initialize necessary data lists
names=[]
costs_hour=[]
costs_day=[]
ids=[]
#add to list necessary data
for i in range(len(rentals)):
names.append(rentals[i][0])
costs_hour.append(rentals[i][3])
costs_day.append(rentals[i][4])
ids.append(rentals[i][5])
#initialize additional necessary data lists
item_list=[]
name_list=[]
quantity_list=[]
cost_hour_list=[]
cost_day_list=[]
#retrieve cookie from browser
cookie=str(request.cookies.get('cart_items'))
#dissect info in cookie
the_list=cookie.split()
for i in range(len(the_list)):
item_list.append(int(the_list[i][0:3]))
print(the_list[i][0:3])
quantity_list.append(int(the_list[i][4:]))
length=len(the_list)
#add to lists used for table in my cart
for i in range(len(the_list)):
name_list.append(names[ids.index(item_list[i])])
cost_hour_list.append(costs_hour[ids.index(item_list[i])])
cost_day_list.append(costs_day[ids.index(item_list[i])])
return render_template("checkoutfinalize.html", name_list=name_list, cost_hour_list=cost_hour_list, cost_day_list=cost_day_list, quantity_list=quantity_list, length=length)
else:
length=0
return render_template("checkoutfinalize.html", length=length)
#app route to emailthanks page
@app.route("/emailthanks")
def emailthanks():
return render_template("emailthanks.html")
#app route to checkoutthanks page
@app.route("/checkoutthanks")
def checkoutthanks():
return render_template("checkoutthanks.html")
#run if main application
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000, debug=True)