diff --git a/src/humulus/filters.py b/src/humulus/filters.py index a8e2d24..8064afb 100644 --- a/src/humulus/filters.py +++ b/src/humulus/filters.py @@ -134,6 +134,21 @@ def sort_hops(hops, form=False): return hops_sorted +def ferm_pct(fermentables): + """Adds a 'pct' to each fermentable in fermentables. + + 'pct' represents the total percentage a fermentable makes up of the grist. + """ + total = 0 + # Calculate total + for ferm in fermentables: + total += float(ferm['amount']) + # Add a pct to each ferm + for ferm in fermentables: + ferm['pct'] = 100*float(ferm['amount'])/total + return fermentables + + def create_filters(app): app.add_template_filter(recipe_og) app.add_template_filter(recipe_fg) @@ -142,3 +157,4 @@ def create_filters(app): app.add_template_filter(recipe_abv) app.add_template_filter(recipe_srm) app.add_template_filter(sort_hops) + app.add_template_filter(ferm_pct) diff --git a/src/humulus/templates/recipes/info.html b/src/humulus/templates/recipes/info.html index ca8a46b..6264a85 100644 --- a/src/humulus/templates/recipes/info.html +++ b/src/humulus/templates/recipes/info.html @@ -122,14 +122,16 @@ Amount PPG Color + % - {%- for fermentable in recipe.fermentables|sort(attribute='amount', reverse=True) -%} + {%- for fermentable in recipe.fermentables|ferm_pct|sort(attribute='amount', reverse=True) -%} {{ fermentable.name }} {{ fermentable.amount|float|round(2) }} lb. {{ fermentable.ppg|float|round }} {{ fermentable.color|float|round(1) }}°L + {{ fermentable.pct|float|round(1) }}% {%- endfor -%} diff --git a/tests/test_filters.py b/tests/test_filters.py index 4f0b1cd..b6928df 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -103,3 +103,12 @@ def test_sort_hops(): for num, hop in enumerate(sort_hops(hop_forms, form=True)): assert hop.name.data == str(num) + + +def test_ferm_pct(): + ferms = [{'amount': '4'}, {'amount': '2'}, {'amount': '2'}] + assert ferm_pct(ferms) == [ + {'amount': '4', 'pct': 50.0}, + {'amount': '2', 'pct': 25.0}, + {'amount': '2', 'pct': 25.0} + ]