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
|
from flask import Flask
from flask import render_template
import json
import random
import re
app = Flask(__name__)
with open("./data/beers.json") as beers_json:
beer = json.loads(beers_json.read())
@app.route('/')
def index():
return render_template('Homepage.jinja2', suggestions=all_beer_names())
@app.route('/random')
def random_beer():
rando = random.choice(list(beer.keys()))
return render_template('random_beer.jinja2', beer=beer[rando])
@app.route('/similar', methods=['POST'])
def similar():
return render_template('similar.jinja2', beers=[])
@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>')
def get_beer_details(brewery_id, beer_id):
beer_result = beer_lookup(brewery_id,beer_id)
return render_template('beerpage.jinja2', beer_data=beer_result)
def beer_lookup(brewery_id, beer_id):
item_id = brewery_id + "-" + beer_id
try:
beer_result = beer[item_id]
return beer_result
except:
return None
def all_beer_names():
return [value[0] for key, value in beer.items()]
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
if __name__ == '__main__':
app.run()
class BeerInfo(object):
def __init__(self, item_json):
self.parent_style = item_json[1][1]
self.base_style = item_json[1][0]
self.name = item_json[0]
self.abv = item_json[2]
class Brewery(object):
def __init__(self, id):
self.id = id
self.brews = []
def add_brew(self, brew):
self.brews.append(brew)
|