From 1c3b91bbfcad2ea176a8f6dfd7e7c27ec160f0f7 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Sat, 22 Jun 2019 13:11:16 -0600 Subject: [PATCH] Add hops in the backend --- src/humulus/recipes.py | 38 +++++++++++++++++++++++++++++++++++--- tests/test_recipes.py | 17 ++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/humulus/recipes.py b/src/humulus/recipes.py index fbb8b36..dfb60a3 100644 --- a/src/humulus/recipes.py +++ b/src/humulus/recipes.py @@ -33,7 +33,6 @@ class FermentableForm(Form): CSRF is disabled for this subform (using `Form as parent class) because it is never used by itself. """ - name = StringField('Name', validators=[DataRequired()]) type = SelectField('Type', validators=[DataRequired()], choices=[(c, c) for c in ['Grain', 'LME', 'DME', 'Sugar', @@ -57,18 +56,50 @@ class FermentableForm(Form): } +class HopForm(Form): + """Form for hops. + + CSRF is disabled for this subform (using `Form as parent class) because it + is never used by itself. + """ + name = StringField('Name', validators=[DataRequired()]) + use = SelectField('Usage', validators=[DataRequired()], + choices=[(c, c) for c in ['Boil', 'FWH', 'Whirlpool', + 'Dry-Hop']]) + alpha = DecimalField('Alpha Acid %', validators=[DataRequired()]) + duration = DecimalField('Duration (min/day)', validators=[DataRequired()]) + amount = DecimalField('Amount (oz)', validators=[DataRequired()]) + + @property + def doc(self): + """Returns a dictionary that can be deserialized into JSON. + + Used for putting into CouchDB. + """ + return { + 'name': self.name.data, + 'use': self.use.data, + 'alpha': str(self.alpha.data), + 'duration': str(self.duration.data), + 'amount': str(self.amount.data) + } + class RecipeForm(FlaskForm): """Form for recipes.""" name = StringField('Name', validators=[DataRequired()]) efficiency = DecimalField('Batch Efficiency (%)', validators=[DataRequired()]) volume = DecimalField('Batch Volume (gal)', validators=[DataRequired()]) notes = TextAreaField('Notes') - fermentables = FieldList( FormField(FermentableForm), min_entries=0, max_entries=20 ) + hops = FieldList( + FormField(HopForm), + min_entries=0, + max_entries=20 + ) @property def doc(self): @@ -81,7 +112,8 @@ class RecipeForm(FlaskForm): 'efficiency': str(self.efficiency.data), 'volume': str(self.volume.data), 'notes': self.notes.data, - 'fermentables': [f.doc for f in self.fermentables] + 'fermentables': [f.doc for f in self.fermentables], + 'hops': [h.doc for h in self.hops], } diff --git a/tests/test_recipes.py b/tests/test_recipes.py index f0ae8c5..0d63236 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -15,7 +15,7 @@ from decimal import Decimal from humulus.couch import get_db -from humulus.recipes import FermentableForm, RecipeForm +from humulus.recipes import FermentableForm, HopForm, RecipeForm def test_create(client, app): @@ -71,11 +71,19 @@ def test_recipe_form_doc(app): ferm.ppg.data = Decimal('37') ferm.color.data = Decimal('1.8') + hop = HopForm() + hop.name.data = 'Test' + hop.use.data = 'Boil' + hop.alpha.data = Decimal('12.5') + hop.duration.data = Decimal('60') + hop.amount.data = Decimal('0.5') + recipe.name.data = 'Test' recipe.efficiency.data = Decimal('65') recipe.volume.data = Decimal('5.5') recipe.notes.data = 'This is a test' recipe.fermentables = [ferm] + recipe.hops = [hop] assert recipe.doc == { 'name': 'Test', @@ -89,4 +97,11 @@ def test_recipe_form_doc(app): 'ppg': '37', 'color': '1.8', }], + 'hops': [{ + 'name': 'Test', + 'use': 'Boil', + 'alpha': '12.5', + 'duration': '60', + 'amount': '0.5' + }] }