-
Notifications
You must be signed in to change notification settings - Fork 1
/
webapp.py
159 lines (118 loc) · 4.63 KB
/
webapp.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
from flask import Flask, request, session, render_template, redirect, flash
import model
from sqlalchemy.orm.exc import NoResultFound
import HTMLParser
app = Flask(__name__)
app.secret_key = 'dress'
@app.route("/")
def index():
"""This is the 'cover' page of the notclothesminded site"""
return render_template("index.html")
@app.route("/signup", methods=["GET"])
def show_signup():
return render_template("signup.html")
@app.route("/signup", methods=["POST"])
def process_sign_up():
first_name = request.form['first_name']
surname = request.form['surname']
email = request.form["email"]
password = request.form["password"]
new_user = model.User(first_name=first_name, surname=surname, email=email, password=password)
model.db_session.add(new_user)
model.db_session.commit()
return render_template("signup.html")
@app.route("/login", methods=["GET"])
def show_login():
return render_template("login.html")
@app.route("/login", methods=["POST"])
def login():
email = request.form["email"]
password = request.form["password"]
query = model.db_session.query(model.User)
try: # if username exists
user = query.filter_by(email = email).one()
if user.password != password:
flash ("Password incorrect, unable to login. Please try again.")
return render_template("login.html")
else:
session['user'] = user.first_name
session['email'] = user.email
return redirect("/search")
except NoResultFound:
flash ("%s is not a registered email. Please try again." % email)
return render_template("login.html")
@app.route("/logout")
def logout():
session.clear()
flash("You've successfully logged out.")
return render_template("login.html")
@app.route("/user/<int:id>")
def show_favorites():
"""This page will display the user's saved items, as well as the search facet
panel on the left hand side"""
pass
@app.route("/search")
def search():
return render_template("search.html")
@app.route("/search_results", methods=["POST"])
def get_results():
description = request.form['description']
title = description.split(' ')
try:
min_bust = int(request.form['min-bust'])
max_bust = int(request.form['max-bust'])
min_waist = int(request.form['min-waist'])
max_waist = int(request.form['max-waist'])
min_hip = int(request.form['min-hip'])
max_hip = int(request.form['max-hip'])
limit = int(request.form['limit'])
offset = int(request.form['offset'])
query = model.db_session.query(model.Listing).filter(
model.Listing.min_bust <= max_bust).filter(
model.Listing.max_bust >= min_bust).filter(
model.Listing.min_waist <= max_waist).filter(
model.Listing.max_waist >= min_waist).filter(
model.Listing.min_hip <= max_hip).filter(
model.Listing.max_hip >= min_hip).order_by(model.Listing.etsy_listing_id)
for word in title:
#print word
query = query.filter(model.Listing.title.ilike('%' + word + '%'))
# print query
count = query.count()
#if count is not cleanly divisible by offset value, add 1 page
if count % limit == 0:
total_pages = count/limit
else:
total_pages = count/limit + 1
query = query.limit(limit).offset(offset)
results = query.all()
for listing in results:
listing.title = HTMLParser.HTMLParser().unescape(listing.title)
current_page = (offset/limit)+1
print current_page
min_page = max(current_page-5, 1)
max_page = min(min_page+9, total_pages)+1
print min_page, max_page
# count = len(results)
def format_price(amount):
return u'{0:.2f}'.format(amount)
return render_template("_search_results.html",
listings=results,
count=count,
#passing function to template to format the price
format_price=format_price,
total_pages=total_pages,
current_page=current_page,
min_page=min_page,
max_page=max_page)
except ValueError:
flash("Please input measurements as numbers, and fill out all fields. Thank you.")
return render_template("_error.html")
# @app.route("/listing_details")
# def get_listing_detail():
# return render_template("listing_details.html")
if __name__ == '__main__':
app.run(debug=True)
#uncomment for deployed version
# if __name__ == '__main__':
# app.run(host='0.0.0.0', port=80, debug=True)