From 0260d300d38ebb8c27262491e3b4931d75054430 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Thu, 11 Jul 2019 14:19:17 -0600 Subject: [PATCH] Add sort to fermentables (#28) Closes #24 --- src/humulus/filters.py | 24 ++++++++++++++++ src/humulus/styles.py | 1 - src/humulus/templates/recipes/_macros.html | 4 +-- src/humulus/templates/recipes/info.html | 4 +-- tests/test_filters.py | 32 ++++++++++++++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/humulus/filters.py b/src/humulus/filters.py index 8266739..a8e2d24 100644 --- a/src/humulus/filters.py +++ b/src/humulus/filters.py @@ -111,6 +111,29 @@ def recipe_srm(recipe): return '{:.0f}'.format(1.4922 * (mcu**0.6859)) +def sort_hops(hops, form=False): + """Sorts a list of hops by use in recipe.""" + by_use = {'FWH': [], 'Boil': [], 'Whirlpool': [], 'Dry-Hop': []} + + # Split hops into each use type. + for hop in hops: + if form: + by_use[hop.use.data].append(hop) + else: + by_use[hop['use']].append(hop) + + if form: + key = lambda hop: float(hop.duration.data) + else: + key = lambda hop: float(hop['duration']) + + hops_sorted = sorted(by_use['FWH'], key=key, reverse=True) + hops_sorted.extend(sorted(by_use['Boil'], key=key, reverse=True)) + hops_sorted.extend(sorted(by_use['Whirlpool'], key=key, reverse=True)) + hops_sorted.extend(sorted(by_use['Dry-Hop'], key=key, reverse=True)) + return hops_sorted + + def create_filters(app): app.add_template_filter(recipe_og) app.add_template_filter(recipe_fg) @@ -118,3 +141,4 @@ def create_filters(app): app.add_template_filter(recipe_ibu_ratio) app.add_template_filter(recipe_abv) app.add_template_filter(recipe_srm) + app.add_template_filter(sort_hops) diff --git a/src/humulus/styles.py b/src/humulus/styles.py index 5424ef8..1b32a68 100644 --- a/src/humulus/styles.py +++ b/src/humulus/styles.py @@ -108,7 +108,6 @@ def get_styles_list(): view = get_view('_design/styles', 'by-category') styles = [['', '']] for row in view(include_docs=False)['rows']: - print(row) styles.append([row['id'], '{}{} {}'.format( row['key'][0], row['key'][1], diff --git a/src/humulus/templates/recipes/_macros.html b/src/humulus/templates/recipes/_macros.html index 6b04beb..a7f99a6 100644 --- a/src/humulus/templates/recipes/_macros.html +++ b/src/humulus/templates/recipes/_macros.html @@ -51,7 +51,7 @@ function getSpecsURL() { -#}

Fermentables

- {% for fermentable in form.fermentables %} + {% for fermentable in form.fermentables|sort(attribute='amount.data', reverse=True) %}
@@ -82,7 +82,7 @@ function getSpecsURL() { -#}

Hops

- {% for hop in form.hops %} + {% for hop in form.hops|sort_hops(form=True) %}
diff --git a/src/humulus/templates/recipes/info.html b/src/humulus/templates/recipes/info.html index 368e282..ca8a46b 100644 --- a/src/humulus/templates/recipes/info.html +++ b/src/humulus/templates/recipes/info.html @@ -124,7 +124,7 @@ Color - {%- for fermentable in recipe.fermentables -%} + {%- for fermentable in recipe.fermentables|sort(attribute='amount', reverse=True) -%} {{ fermentable.name }} {{ fermentable.amount|float|round(2) }} lb. @@ -149,7 +149,7 @@ Usage - {%- for hop in recipe.hops -%} + {%- for hop in recipe.hops|sort_hops -%} {{ hop.name }} {{ hop.amount|float|round(2) }} oz. diff --git a/tests/test_filters.py b/tests/test_filters.py index 0dd825f..4f0b1cd 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -12,8 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +from decimal import Decimal from humulus.filters import * +from humulus.recipes import HopForm def test_recipe_og(sample_recipes): @@ -71,3 +73,33 @@ def test_recipe_srm(sample_recipes): # Remove fermentables, verify 0 is returned sample_recipes['lager'].pop('fermentables') assert recipe_srm(sample_recipes['lager']) == '0' + + +def test_sort_hops(): + # Test with no form + hops = [ + {'name': '4', 'use': 'Dry-Hop', 'duration': '5'}, + {'name': '3', 'use': 'Whirlpool', 'duration': '10'}, + {'name': '2', 'use': 'Boil', 'duration': '5'}, + {'name': '1', 'use': 'Boil', 'duration': '15'}, + {'name': '0', 'use': 'FWH', 'duration': '60'}, + ] + assert sort_hops(hops) == [ + {'name': '0', 'use': 'FWH', 'duration': '60'}, + {'name': '1', 'use': 'Boil', 'duration': '15'}, + {'name': '2', 'use': 'Boil', 'duration': '5'}, + {'name': '3', 'use': 'Whirlpool', 'duration': '10'}, + {'name': '4', 'use': 'Dry-Hop', 'duration': '5'}, + ] + + # Test with form + hop_forms = [] + for hop in hops: + form = HopForm() + form.name.data = hop['name'] + form.use.data = hop['use'] + form.duration.data = Decimal(hop['duration']) + hop_forms.append(form) + + for num, hop in enumerate(sort_hops(hop_forms, form=True)): + assert hop.name.data == str(num)