-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
221 lines (187 loc) · 8.09 KB
/
main.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
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
#!/usr/bin/python
# -*- encoding: utf-8 -*-
from bottle import *
import auth_public as auth
# import psycopg2
import psycopg2, psycopg2.extensions, psycopg2.extras
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) # bcoz šumniki
# uncomment if you want to see error reports
# debug(True)
LAST_BEER_ID = 1041
@get('/')
def index():
return template('front_page.html')
@get('/static/<filename:path>')
def static(filename):
return static_file(filename, root='static')
@get('/drinks')
def index():
return template('drinks.html', )
@get('/search')
def index():
return template('search.html')
@get('/general_info')
def index():
return template('general_info.html')
@get('/statistics')
def index():
return template('statistics.html')
@get('/wine_info/:x/')
def get_wine_info(x):
""" Performs a search through the drink database about the wine with id x """
drink_query = """(SELECT DISTINCT pijaca.ime, drzava, velikost, stopnja_alkohola, vrste_pijace.ime AS vrsta, cena, opis, slika
FROM pijaca JOIN vrste_pijace on pijaca.vrsta = vrste_pijace.id
WHERE pijaca.id = %s)"""
wine_query = """(SELECT barva,regija
FROM vino WHERE id = %s)"""
taste_query = """(SELECT DISTINCT ime
FROM okusi JOIN ima_okus ON okusi.id = ima_okus.okus
WHERE vino = %s)"""
smell_query = """(SELECT DISTINCT ime
FROM aroma JOIN ima_vonj ON aroma.id = ima_vonj.aroma
WHERE vino = %s)"""
cur.execute(drink_query, [int(x)])
r1 = next(cur)
cur.execute(wine_query, [int(x)])
r2 = next(cur)
cur.execute(taste_query, [int(x)])
r3 = []
for [t] in cur:
r3.append(t)
cur.execute(smell_query, [int(x)])
r4 = []
for [s] in cur:
r4.append(s)
return template('wine_info.html', res1=r1, res2=r2, taste=r3, smell=r4)
@get('/beer_info/:x/')
def get_beer_info(x):
""" Performs a search through the drink database about the beer with id x """
drink_query = """(SELECT DISTINCT pijaca.ime, drzava, velikost, stopnja_alkohola, vrste_pijace.ime AS vrsta, cena, opis, slika
FROM pijaca JOIN vrste_pijace on pijaca.vrsta = vrste_pijace.id
WHERE pijaca.id = %s)"""
beer_query = """(SELECT pivovarna
FROM pivo WHERE id = %s)"""
cur.execute(drink_query, [int(x)])
result = next(cur)
cur.execute(beer_query, [int(x)])
[brewery] = next(cur)
return template('beer_info.html', result=result, pivovarna=brewery)
@get('/country/:x/')
def get_country(x):
""" Performs a search through the drink database with the chosen country """
country_query = """(SELECT id, ime, drzava, velikost, stopnja_alkohola, slika, cena, vrsta
FROM pijaca WHERE drzava = %s)"""
cur.execute(country_query, [x])
results = cur
return template('drinks.html', results=results, pictures=True)
@post('/search_drinks')
def search_drinks_post():
""" Performs a search through the drink database with the given preferences """
# collecting the values entered into the search form by the user
query_dic = {"grilled": request.forms.grilled,
"fried": request.forms.fried,
"roasted": request.forms.roasted,
"smoked": request.forms.smoked,
"steamed": request.forms.steamed,
#meat
"red_meat": request.forms.red_meat,
"cured": request.forms.cured_meat,
"pork": request.forms.pork,
"poultry": request.forms.poultry,
"mollusk": request.forms.mollusk,
"fish": request.forms.fish,
"shellfish": request.forms.shellfish,
# starch
"white": request.forms.white_starch,
"whole": request.forms.whole_wheat,
"sweet": request.forms.sweet_starchy_vegetables,
"potato": request.forms.potato,
# vegetables
"alliums": request.forms.alliums,
"green": request.forms.green,
"root": request.forms.root,
"mushrooms": request.forms.funghi,
"beans": request.forms.beans,
"nuts": request.forms.nuts,
"nightshades": request.forms.nightshades,
# spices
"spicy": request.forms.spicy,
"herbs": request.forms.herbs,
"black": request.forms.black_pepper,
"red": request.forms.red_pepper,
"baking": request.forms.baking,
"exotic": request.forms.exotic,
# cheese
"soft": request.forms.soft,
"hard": request.forms.hard,
"pungent": request.forms.pungent,
# sweets
"fruit": request.forms.fruit,
"vanilla": request.forms.vanilla,
"chocolate": request.forms.chocolate,
"coffee": request.forms.coffee}
# gather the search terms actually selected by the user
l = query_dic.values()
key_terms = [x for x in l if x != ""]
pref_drink = request.forms.drink
# constructing the sql command
if len(key_terms) >= 1:
if pref_drink == "both":
sql_query = """(SELECT id, ime, drzava, velikost, stopnja_alkohola, slika, cena, vrsta
FROM search WHERE hrana = %s)"""
elif pref_drink == "beer":
sql_query = """(SELECT id, ime, drzava, velikost, stopnja_alkohola, slika, cena, vrsta
FROM search WHERE vrsta <= 34 AND hrana = %s)"""
else:
sql_query = """(SELECT id, ime, drzava, velikost, stopnja_alkohola, slika, cena, vrsta
FROM search WHERE vrsta > 34 AND hrana = %s)"""
else:
if pref_drink == "both":
sql_query = """(SELECT id, ime, drzava, velikost, stopnja_alkohola, slika, cena, vrsta
FROM search)"""
elif pref_drink == "beer":
sql_query = """(SELECT id, ime, drzava, velikost, stopnja_alkohola, slika, cena, vrsta
FROM search WHERE vrsta <= 34)"""
else:
sql_query = """(SELECT id, ime, drzava, velikost, stopnja_alkohola, slika, cena, vrsta
FROM search WHERE vrsta > 34)"""
if len(key_terms) > 1:
for _ in range(1, len(key_terms)):
additional_criteria = """
INTERSECT
(SELECT id, ime, drzava, velikost, stopnja_alkohola, slika, cena, vrsta
FROM search WHERE hrana = %s)"""
sql_query += additional_criteria
# selecting what results are sorted by
sort_key = request.forms.sort
sort_dic = {"alcohol": "stopnja_alkohola",
"price": "cena",
"size": "velikost",
"country": "drzava"}
# select the sort order
if request.forms.descending:
sort_string = " ORDER BY {} DESC".format(sort_dic[sort_key])
else:
sort_string = " ORDER BY {} ASC".format(sort_dic[sort_key])
sql_query += sort_string
# communication with database
# TODO: fix the execute values!!!
#var = [LAST_BEER_ID] + key_terms
cur.execute(sql_query, key_terms)
# show the results
results = cur
# does the user want to see pictures by the suggested drinks
pictures = True
if request.forms.pictures == "no":
pictures = False
return template('drinks.html', results=results, pictures=pictures)
#redirect("/")
# ====================================== #
# MAIN PROGRAM
# ====================================== #
# connection to the database
connection = psycopg2.connect(database=auth.db, host=auth.host, user=auth.user, password=auth.password)
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) # we disable transactions?
cur = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
# we run the server on port 8080, access at: http://localhost:8080/
run(host='localhost', port=8080, reloader=True)