diff --git a/src/humulus/app.py b/src/humulus/app.py index 171b6ba..5bf78c5 100644 --- a/src/humulus/app.py +++ b/src/humulus/app.py @@ -46,4 +46,8 @@ def create_app(test_config=None): from . import auth app.register_blueprint(auth.bp) + # Register custom filters + from . import filters + filters.create_filters(app) + return app diff --git a/src/humulus/filters.py b/src/humulus/filters.py new file mode 100644 index 0000000..010a927 --- /dev/null +++ b/src/humulus/filters.py @@ -0,0 +1,68 @@ +"""This module contains filters used in rendering of Jinja templates.""" + +# Copyright 2019 Mike Shoup +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def recipe_og(recipe): + """Returns a recipe's Original Gravity""" + points = 0 + grain_points = 0 + # Loop through fermentables, adding up points + for fermentable in recipe['fermentables']: + if fermentable['type'] == 'Grain': + grain_points += ( + float(fermentable['amount']) * float(fermentable['ppg']) + ) + else: + points += ( + float(fermentable['amount']) * float(fermentable['ppg']) + ) + points += grain_points * float(recipe['efficiency']) / 100 + return '{:.3f}'.format( + round(1 + points / (1000 * float(recipe['volume'])), 3) + ) + + +def recipe_fg(recipe): + """Returns a recipe's final gravity""" + if 'yeast' not in recipe: + return 0.0 + og = float(recipe_og(recipe)) + og_delta = 0.0 + # Adjust original gravity by removing nonfermentables (i.e., Lactose) + for fermentable in recipe['fermentables']: + if fermentable['type'] == 'Non-fermentable': + og_delta += ( + float(fermentable['amount']) * float(fermentable['ppg']) / + (1000 * float(recipe['volume'])) + ) + attenuation = ( + ( + float(recipe['yeast']['low_attenuation']) + + float(recipe['yeast']['high_attenuation']) + ) / 200 + ) + print(og - 1 - og_delta) + print(attenuation) + print(og_delta) + return '{:.3f}'.format( + round(1 + (og - 1 - og_delta)*(1 - attenuation) + og_delta, 3) + ) + + + +def create_filters(app): + app.add_template_filter(recipe_og) + app.add_template_filter(recipe_fg) diff --git a/src/humulus/templates/recipes/info.html b/src/humulus/templates/recipes/info.html index 08b70b2..4c9ab2d 100644 --- a/src/humulus/templates/recipes/info.html +++ b/src/humulus/templates/recipes/info.html @@ -32,6 +32,10 @@