diff --git a/src/humulus/filters.py b/src/humulus/filters.py index 8266739..aafc495 100644 --- a/src/humulus/filters.py +++ b/src/humulus/filters.py @@ -111,6 +111,62 @@ 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: + hops_sorted = sorted( + by_use['FWH'], + key=lambda hop: float(hop.duration.data), + reverse=True + ) + hops_sorted.extend( + sorted(by_use['Boil'], + key=lambda hop: float(hop.duration.data), + reverse=True) + ) + hops_sorted.extend( + sorted(by_use['Whirlpool'], + key=lambda hop: float(hop.duration.data), + reverse=True) + ) + hops_sorted.extend( + sorted(by_use['Dry-Hop'], + key=lambda hop: float(hop.duration.data), + reverse=True) + ) + else: + hops_sorted = sorted( + by_use['FWH'], + key=lambda hop: float(hop['duration']), + reverse=True + ) + hops_sorted.extend( + sorted(by_use['Boil'], + key=lambda hop: float(hop['duration']), + reverse=True) + ) + hops_sorted.extend( + sorted(by_use['Whirlpool'], + key=lambda hop: float(hop['duration']), + reverse=True) + ) + hops_sorted.extend( + sorted(by_use['Dry-Hop'], + key=lambda hop: float(hop['duration']), + reverse=True) + ) + return hops_sorted + + def create_filters(app): app.add_template_filter(recipe_og) app.add_template_filter(recipe_fg) @@ -118,3 +174,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/templates/recipes/_macros.html b/src/humulus/templates/recipes/_macros.html index 4214e74..a7f99a6 100644 --- a/src/humulus/templates/recipes/_macros.html +++ b/src/humulus/templates/recipes/_macros.html @@ -82,7 +82,7 @@ function getSpecsURL() { -#}