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
|
from flask import Flask
from flask import render_template
from flask import redirect
from flask import request
from flask import url_for
from utils import get_closest, translate_to_attr
import json
import random
import re
from utils import get_drinks_like
app = Flask(__name__)
with open("./data/beer_info_small.json") as beers_json:
beer = json.loads(beers_json.read())
with open('./data/brewery_names.json') as f:
brewery_names = json.load(f)
@app.route('/')
def index():
return render_template('Homepage.jinja2')
@app.route('/random')
def random_beer():
rando = random.choice(list(beer.keys())).split('-')
return redirect(url_for('get_beer_details', brewery_id=rando[0], beer_id=rando[1]))
@app.route('/brewery/<brewery_id>/')
def get_brewery_list(brewery_id):
return json.dumps(find_beers_from_brewery(brewery_id))
@app.route('/brewery/<brewery_id>/<beer_id>', methods=['GET', 'POST'])
def get_beer_details(brewery_id, beer_id):
result, brewery_name = beer_lookup(brewery_id, beer_id)
full_id = brewery_id+'-'+beer_id
beerinfo = BeerInfo(full_id, brewery_name, result)
attrs = ['Burnt', 'Bready', 'Malty', 'Dark', 'Floral', 'Hoppy', 'Dank',
'Grassy', 'Boozy', 'Warming', 'Bitter', 'Wheaty', 'Sour',
'Barnyardy', 'Woody', 'Smoky', 'Roasty', 'Yeasty', 'Sweet',
'Juicy', 'Light', 'Thin', 'Spicy', 'Dry', 'Fruity', 'Herbal',
'Salty']
similar_beers = get_closest(full_id)
similar_beer_objects = []
for item in similar_beers:
item_result, item_brewer = beer_lookup(item)
similar_beer_objects.append(BeerInfo(item, item_brewer, item_result))
if request.method == 'POST':
attr = request.form['attr']
direction = request.form['direction']
attr_beers = translate_to_attr(full_id, attr, int(direction))
attr_beer_objects = []
for item in attr_beers:
item_result, item_brewer = beer_lookup(item[0])
attr_beer_objects.append(BeerInfo(item[0], item_brewer, item_result))
return render_template('beerpage.jinja2', beer_data=beerinfo,
attrs=attrs, similar_beers=similar_beer_objects, attr=attr,
direction=direction, attr_beers=attr_beer_objects)
return render_template('beerpage.jinja2', beer_data=beerinfo, attrs=attrs,
similar_beers=similar_beer_objects)
def beer_lookup(brewery_id, beer_id=None):
if beer_id:
item_id = brewery_id + "-" + beer_id
try:
beer_result = beer[item_id]
return beer_result, brewery_names[brewery_id]
except:
return None
else:
try:
beer_result = beer[brewery_id]
return beer_result, brewery_names[brewery_id.split("-")[0]]
except:
return None
def find_beers_from_brewery(brewery_id):
pattern = re.compile('^' + brewery_id + '-.*')
beers = [value for key, value in beer.items() if pattern.match(key)]
return beers
class BeerInfo(object):
def __init__(self, beer_id, brewery_name, item_json):
self._id = beer_id
self.parent_style = item_json[1][1]
self.base_style = item_json[1][0]
self.name = item_json[0]
self.abv = item_json[2]
self.brewer = brewery_name
self.num_reviews = item_json[3]
self.drinks_like = get_drinks_like(self._id)
self.link ="/brewery/" + beer_id.split('-')[0] + '/' + beer_id.split('-')[1]
class Brewery(object):
def __init__(self, breweryid):
self.id = breweryid
self.brews = []
def add_brew(self, brew):
self.brews.append(brew)
if __name__ == '__main__':
app.config.update(TEMPLATES_AUTO_RELOAD=True)
app.run(host='0.0.0.0')
|