From e5a9f35fcb0a4add8559a0106ebe746ded29589a Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Thu, 4 Jul 2019 08:43:58 -0600 Subject: [PATCH] Add all filters. --- src/humulus/filters.py | 31 ++++++++++++++++++++++ src/humulus/templates/recipes/info.html | 35 ++++++++++++++++++------- tests/test_filters.py | 30 +++++++++++++++++++++ 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/src/humulus/filters.py b/src/humulus/filters.py index 8c13d19..5c12f81 100644 --- a/src/humulus/filters.py +++ b/src/humulus/filters.py @@ -79,7 +79,38 @@ def recipe_ibu(recipe): return '{:.0f}'.format(ibu) +def recipe_ibu_ratio(recipe): + """Return a recipe's IBU ratio""" + if 'fermentables' not in recipe or 'hops' not in recipe: + return '0' + og = float(recipe_og(recipe)) + ibu = float(recipe_ibu(recipe)) + return '{:.2f}'.format(round(0.001 * ibu / (og - 1), 2)) + + +def recipe_abv(recipe): + """Return a recipe's finished ABV""" + if 'fermentables' not in recipe or 'yeast' not in recipe: + return '0' + og = float(recipe_og(recipe)) + fg = float(recipe_fg(recipe)) + return '{:.1f}'.format(round((og - fg) * 131.25, 1)) + + +def recipe_srm(recipe): + """Return a recipe's SRM""" + if 'fermentables' not in recipe: + return '0' + mcu = 0 + for f in recipe['fermentables']: + mcu += float(f['amount']) * float(f['color']) / float(recipe['volume']) + return '{:.0f}'.format(1.4922 * (mcu**0.6859)) + + def create_filters(app): app.add_template_filter(recipe_og) app.add_template_filter(recipe_fg) app.add_template_filter(recipe_ibu) + app.add_template_filter(recipe_ibu_ratio) + app.add_template_filter(recipe_abv) + app.add_template_filter(recipe_srm) diff --git a/src/humulus/templates/recipes/info.html b/src/humulus/templates/recipes/info.html index 56e57c5..28df021 100644 --- a/src/humulus/templates/recipes/info.html +++ b/src/humulus/templates/recipes/info.html @@ -32,12 +32,6 @@
{{ recipe.efficiency|int }}%
Batch Volume
{{ recipe.volume|float|round(1) }} gal.
-
OG
-
{{ recipe|recipe_og }}
-
FG
-
{{ recipe|recipe_fg }}
-
IBU
-
{{ recipe|recipe_ibu }}
@@ -53,11 +47,34 @@
+

Estimated Specifications

+
+
+
+
Original Gravity
+
{{ recipe|recipe_og }}
+
Final Gravity
+
{{ recipe|recipe_fg }}
+
Alcohol
+
{{ recipe|recipe_abv }} %/vol.
+
+
+
+
+
Bitterness
+
{{ recipe|recipe_ibu }} IBU
+
Bitterness Ratio
+
{{ recipe|recipe_ibu_ratio }} IBU/OG
+
Color
+
{{ recipe|recipe_srm }} SRM
+
+
+
{#- Fermentables -#}

Fermentables

-
+
@@ -81,7 +98,7 @@ Hops -#}

Hops

-
+
@@ -108,7 +125,7 @@ -#} {% if 'yeast' in recipe %}

Yeast

-
+
diff --git a/tests/test_filters.py b/tests/test_filters.py index 7a59cbd..14ecda5 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -41,3 +41,33 @@ def test_recipe_ibu(sample_recipes): # Remove hops, verify 0 is returned sample_recipes['lager'].pop('hops') assert recipe_ibu(sample_recipes['lager']) == '0' + + +def test_recipe_ibu_ratio(sample_recipes): + assert recipe_ibu_ratio(sample_recipes['lager']) == '0.48' + assert recipe_ibu_ratio(sample_recipes['sweetstout']) == '0.89' + # Remove fermentables, verify 0 is returned + sample_recipes['lager'].pop('fermentables') + assert recipe_ibu_ratio(sample_recipes['lager']) == '0' + # Remove hops, verify 0 is returned + sample_recipes['sweetstout'].pop('hops') + assert recipe_ibu_ratio(sample_recipes['sweetstout']) == '0' + + +def test_recipe_abv(sample_recipes): + assert recipe_abv(sample_recipes['lager']) == '5.3' + assert recipe_abv(sample_recipes['sweetstout']) == '3.0' + # Remove fermentables, verify 0 is returned + sample_recipes['lager'].pop('fermentables') + assert recipe_abv(sample_recipes['lager']) == '0' + # Remove yeast, verify 0 is returned + sample_recipes['sweetstout'].pop('yeast') + assert recipe_abv(sample_recipes['sweetstout']) == '0' + + +def test_recipe_srm(sample_recipes): + assert recipe_srm(sample_recipes['lager']) == '3' + assert recipe_srm(sample_recipes['sweetstout']) == '21' + # Remove fermentables, verify 0 is returned + sample_recipes['lager'].pop('fermentables') + assert recipe_srm(sample_recipes['lager']) == '0'