-
Notifications
You must be signed in to change notification settings - Fork 0
/
saywish.py
executable file
·123 lines (108 loc) · 3.8 KB
/
saywish.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
from flask import Flask, render_template, redirect, request, g, session
from model import SESSION as db_session
import model
import scrapedoc
app = Flask(__name__)
SECRET_KEY = "SOMETHING"
app.config.from_object(__name__)
@app.before_request
def load_user_id():
g.user_id = session.get('user_id')
@app.teardown_request
def unload(exception = None):
db_session.remove()
@app.route("/")
def index():
if g.user_id:
return redirect("/main_wishes")
return render_template("main.html")
@app.route('/authenticate', methods = ["POST"])
def login():
email = request.form['email']
password = request.form['password']
user_id = model.User.authenticate(email, password)
session['user_id'] = user_id
g.user_id = session.get('user_id')
if user_id != None:
return redirect("/main_wishes")
return "wrong password or email"
@app.route('/new_user', methods = ["POST"])
def newuser():
email = request.form['email']
password = request.form['password']
user_id = model.User.new(email, password)
session['user_id'] = user_id
g.user_id = session['user_id']
return redirect("/main_wishes")
@app.route('/logout')
def logout():
session.pop('user_id', None)
return render_template("main.html")
@app.route("/get_wishes/", methods = ["GET"])
def get_wishes():
user_id = g.user_id
website = request.args['weburl']
host_data = scrapedoc.check_host(website)
if host_data:
price_class, brand_class = [host_data.price_class], host_data.brand_class
else:
brand_class = scrapedoc.split_class(request.args['items'])
price_class = brand_class
groups = model.Wishlist.search_groups(user_id.id)
brand = scrapedoc.first_time(website, brand_class)
images = scrapedoc.find_images(website)
prices = scrapedoc.find_best_price_div(website, price_class)
host = scrapedoc.get_host(website)
image_src = scrapedoc.find_images(website)
return render_template("get_wish.html", brand = brand, images = images, website = website, prices = prices, host=host, image_src = image_src, groups=groups)
@app.route("/add_item", methods = ["POST"])
def newitem():
user_id = g.user_id
website = request.form['specific_url']
brand = (request.form['brandname'] or request.form['brandname_other']).split("|")
price = (request.form['price'] or request.form['price_other']).split("|")
item_group = request.form['item_group'] or request.form['item_group_other']
host = request.form['host']
image_src = request.form['image_src']
if len(brand) == 1:
brand =[ None, brand[0]]
if len(price) == 1:
price =[ None, price[0]]
if scrapedoc.check_host(website) == None:
model.Website.new(host, str(price[0]), str(brand[0]), None)
item=model.Item.new(brand[1], item_group, price[1], website, host, image_src)
model.Wishlist.new(user_id.id, item)
return redirect("/main_wishes")
@app.route('/grouped_items/, methods=["GET"]')
def group_items():
group_name = request.args['group']
other_items = model.Wishlist.search(g.user_id.id)
desired_group =[]
for items in other_items:
if items.item.title == group_name:
desired_group.append(items.item)
return render_template("wishlist.html", items= desired_group)
@app.route('/main_wishes')
def items():
items = model.Wishlist.search(g.user_id.id)
group_items = model.Wishlist.search_groups(g.user_id.id)
best_groups ={}
if items:
for group in group_items:
best_item =[1000000000, None]
for item in items:
if item.item.title == group:
item_price = item.item.price.split('$')
if best_item[0]>float(item_price[1]):
best_item[0], best_item[1] =float(item_price[1]), item.item
best_groups[group] = best_item[1]
else:
item_dict = None
return render_template("main_wishlist.html", item_dict = best_groups)
@app.route('/wishlist/', methods=["GET"])
def delete_item():
item_id = request.args['itemid']
model.Item.delete(item_id)
return redirect("/main_wishes")
if __name__ == "__main__":
app.run(debug = True)