-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
190 lines (165 loc) · 4.51 KB
/
app.py
File metadata and controls
190 lines (165 loc) · 4.51 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
from flask import (
Flask,
redirect,
render_template,
request,
url_for,
session,
abort,
flash,
g,
)
import sqlite3
from flask_bcrypt import Bcrypt
DATABASE = "couterra.db"
TABLE = "User"
app = Flask(__name__)
app.secret_key = b"secretkey"
bcrypt = Bcrypt(app)
def get_db():
db = getattr(g, "database", None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
cur = db.cursor()
cur.execute(
"""create table if not exists {} (
username TEXT PRIMARY KEY,
password TEXT,
firstName TEXT,
lastName TEXT)""".format(
TABLE
)
)
db.row_factory = make_dicts
return db
def make_dicts(cursor, row):
print("Make_dicts", "Cursor:", cursor.description, "row:", row)
return dict((cursor.description[idx][0], value) for idx, value in enumerate(row))
def query_db(query, args=(), one=False):
print("Query value:", query, "Args value:", args)
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
@app.route("/")
def index():
return render_template("index.html")
@app.route("/ethical-brands")
def ethicalbrands():
sql_names = """
SELECT company_name
FROM couterra
ORDER BY company_name ASC
"""
sql_countries = """
SELECT country
FROM couterra
ORDER BY company_name ASC
"""
sql_cities = """
SELECT city
FROM couterra
ORDER BY company_name ASC
"""
sql_websites = """
SELECT website
FROM couterra
ORDER BY company_name ASC
"""
names = query_db(sql_names)
countries = query_db(sql_countries)
cities = query_db(sql_cities)
websites = query_db(sql_websites)
return render_template("ethical-brands.html",
company_names=names,
all_countries=countries,
all_cities=cities,
all_websites=websites)
@app.route("/fashion-exchange")
def fashionexchange():
return render_template("fashion-exchange.html")
@app.route("/contact", methods=["GET", "POST"])
def feedback():
if request.method == "POST":
name = request.form["name"]
email = request.form["email"]
msg = request.form["message"]
db = get_db()
cur = db.cursor()
try:
cur.execute(
"INSERT INTO feedback(name, email, message) VALUES (?,?,?)",
[name,email,msg],
)
db.commit()
except sqlite3.InterfaceError as err:
flash("Feedback not added.")
return render_template("contact.html")
@app.route("/find")
def find():
sql_photos = """
SELECT photo
FROM post
ORDER BY brand ASC
"""
sql_emails = """
SELECT email
FROM post
ORDER BY brand ASC
"""
sql_brands = """
SELECT brand
FROM post
ORDER BY brand ASC
"""
sql_sizes = """
SELECT size
FROM post
ORDER BY brand ASC
"""
sql_conditions = """
SELECT condition
FROM post
ORDER BY brand ASC
"""
sql_extras = """
SELECT extra
FROM post
ORDER BY brand ASC
"""
photos = query_db(sql_photos)
emails = query_db(sql_emails)
brands = query_db(sql_brands)
sizes = query_db(sql_sizes)
conditions = query_db(sql_conditions)
extras = query_db(sql_extras)
return render_template("find.html",
all_photos=photos,
all_emails=emails,
all_brands=brands,
all_sizes=sizes,
all_conditions=conditions,
all_extras=extras)
return render_template("find.html")
@app.route("/post", methods=["GET", "POST"])
def post():
if request.method == "POST":
photo = request.form["photo"]
email = request.form["email"]
brand = request.form["brand"]
size = request.form["size"]
condition = request.form["condition"]
extra = request.form["extra"]
db = get_db()
cur = db.cursor()
try:
cur.execute(
"INSERT INTO post(photo, email, brand, size, condition, extra) VALUES (?,?,?,?,?,?)",
[photo,email,brand,size,condition,extra],
)
db.commit()
except sqlite3.InterfaceError as err:
flash("Item not posted.")
return render_template("post.html")
if __name__ == "__main__":
app.run(debug=True)