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//') def get_brewery_list(brewery_id): return json.dumps(find_beers_from_brewery(brewery_id)) @app.route('/brewery//', 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')