diff --git a/src/humulus/recipes.py b/src/humulus/recipes.py index 8d569b9..e854b14 100644 --- a/src/humulus/recipes.py +++ b/src/humulus/recipes.py @@ -154,14 +154,14 @@ class MashStepForm(Form): CSRF is disabled for this form (using `Form as parent class) because it is never used by itself. """ - name = StringField('Name', validators=[DataRequired()]) + name = StringField('Step Name', validators=[DataRequired()]) type = SelectField('Type', choices=[(c, c) for c in ['Infusion', 'Temperature', 'Decoction']]) - temp = DecimalField('Temperature', validators=[DataRequired()]) - time = DecimalField('Time', validators=[DataRequired()]) - amount = DecimalField('Water Amount') + temp = DecimalField('Temperature (°F)', validators=[DataRequired()]) + time = DecimalField('Time (min)', validators=[DataRequired()]) + amount = DecimalField('Water Amount (gal)') @property def doc(self): @@ -186,7 +186,7 @@ class MashForm(Form): CSRF is disabled for this form (using `Form as parent class) because it is never used by itself. """ - name = StringField('Name', validators=[Optional()]) + name = StringField('Mash Name', validators=[Optional()]) steps = FieldList( FormField(MashStepForm), min_entries=0, @@ -287,35 +287,52 @@ class RecipeForm(FlaskForm): }) if 'yeast' in data: - yeast = data['yeast'] - self.yeast.form.name.data = yeast['name'] + self.yeast.form.name.data = data['yeast']['name'] self.yeast.form.low_attenuation.data = ( - Decimal(yeast['low_attenuation']) + Decimal(data['yeast']['low_attenuation']) ) self.yeast.form.high_attenuation.data = ( - Decimal(yeast['high_attenuation']) + Decimal(data['yeast']['high_attenuation']) ) - if 'type' in yeast: - self.yeast.form.type.data = yeast['type'] - if 'lab' in yeast: - self.yeast.form.lab.data = yeast['lab'] - if 'code' in yeast: - self.yeast.form.code.data = yeast['code'] - if 'flocculation' in yeast: - self.yeast.form.flocculation.data = yeast['flocculation'] - if 'min_temperature' in yeast: + if 'type' in data['yeast']: + self.yeast.form.type.data = data['yeast']['type'] + if 'lab' in data['yeast']: + self.yeast.form.lab.data = data['yeast']['lab'] + if 'code' in data['yeast']: + self.yeast.form.code.data = data['yeast']['code'] + if 'flocculation' in data['yeast']: + self.yeast.form.flocculation.data = ( + data['yeast']['flocculation'] + ) + if 'min_temperature' in data['yeast']: self.yeast.form.min_temperature.data = ( - Decimal(yeast['min_temperature']) + Decimal(data['yeast']['min_temperature']) ) - if 'max_temperature' in yeast: + if 'max_temperature' in data['yeast']: self.yeast.form.max_temperature.data = ( - Decimal(yeast['max_temperature']) + Decimal(data['yeast']['max_temperature']) ) - if 'abv_tolerance' in yeast: + if 'abv_tolerance' in data['yeast']: self.yeast.form.abv_tolerance.data = ( - Decimal(yeast['abv_tolerance']) + Decimal(data['yeast']['abv_tolerance']) ) + if 'mash' in data: + if 'name' in data['mash']: + self.mash.form.name.data = data['mash']['name'] + if 'steps' in data['mash']: + for step in data['mash']['steps']: + new_step = { + 'name': step['name'], + 'type': step['type'], + 'temp': Decimal(step['temp']), + 'time': Decimal(step['time']) + } + if 'amount' in step: + new_step['amount'] = Decimal(step['amount']) + print(new_step) + self.mash.steps.append_entry(new_step) + class ImportForm(FlaskForm): upload = FileField(validators=[FileRequired()]) diff --git a/src/humulus/templates/recipes/_macros.html b/src/humulus/templates/recipes/_macros.html index a7f99a6..19b37e6 100644 --- a/src/humulus/templates/recipes/_macros.html +++ b/src/humulus/templates/recipes/_macros.html @@ -130,6 +130,36 @@ function getSpecsURL() { + {#- + Mash & Steps + -#} +

Mash

+
+ {{ render_field_with_errors(form.mash.form.name, 'form-control-sm') }} + {% for step in form.mash.form.steps %} +
+
+
{{ render_field_with_errors(step.form.name, 'form-control-sm') }}
+
{{ render_field_with_errors(step.form.type, 'custom-select-sm', base_class='custom-select') }}
+
+
+
{{ render_field_with_errors(step.form.temp, 'form-control-sm') }}
+
{{ render_field_with_errors(step.form.time, 'form-control-sm') }}
+
{{ render_field_with_errors(step.form.amount, 'form-control-sm') }}
+
+
+
+ +
+
+
+ {% endfor %} +
+
+
+ +
+
{#- Recipe Notes -#} diff --git a/src/humulus/templates/recipes/info.html b/src/humulus/templates/recipes/info.html index 6264a85..ee58f2e 100644 --- a/src/humulus/templates/recipes/info.html +++ b/src/humulus/templates/recipes/info.html @@ -200,11 +200,41 @@ {% endif %} +{% if recipe.mash and recipe.mash.name %} +

Mash

+
{{ recipe.mash.name }}
+
+ + + + + + + + + + + + {% for step in recipe.mash.steps %} + + + + + + + + + {% endfor %} +
#Step NameTypeTempTimeAmount
{{ loop.index }}{{ step.name }}{{ step.type }}{% if step.temp %}{{ step.temp }} °F{% endif %}{% if step.time %}{{ step.time }} min.{% endif %}{% if step.amount %}{{ step.amount }} gal.{% endif %}
+
+{% endif %} {#- Recipe Notes -#} +{% if recipe.notes %}

Recipe Notes

{{ recipe.notes }}
+{% endif %} {#- Buttons to do things -#} diff --git a/tests/test_recipes.py b/tests/test_recipes.py index 45c5959..7f3f740 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -450,3 +450,122 @@ def test_recipe_create_json(client, sample_recipes, auth): response = client.post('/recipes/create/json', buffered=True, content_type='multipart/form-data', data=data) assert response.status_code == 200 + + +def test_copyfrom(app, sample_recipes): + recipe = { + 'name': 'Test', + 'type': 'All-Grain', + 'efficiency': '65', + 'volume': '5.5', + 'notes': 'Notes', + 'style': '18A', + 'fermentables': [{ + 'name': 'Test', + 'type': 'Grain', + 'amount': '1', + 'ppg': '36', + 'color': '4' + }], + 'hops': [{ + 'name': 'Test', + 'use': 'Boil', + 'alpha': '5.5', + 'duration': '30', + 'amount': '1' + }] + } + + with app.app_context(): + form = RecipeForm() + form.copyfrom(recipe) + assert form.name.data == recipe['name'] + assert form.type.data == recipe['type'] + assert form.efficiency.data == Decimal(recipe['efficiency']) + assert form.volume.data == Decimal(recipe['volume']) + assert form.notes.data == recipe['notes'] + assert len(form.fermentables) == len(recipe['fermentables']) + assert form.fermentables[0].form.name.data == \ + recipe['fermentables'][0]['name'] + assert form.fermentables[0].form.type.data == \ + recipe['fermentables'][0]['type'] + assert form.fermentables[0].form.amount.data == \ + Decimal(recipe['fermentables'][0]['amount']) + assert form.fermentables[0].form.ppg.data == \ + Decimal(recipe['fermentables'][0]['ppg']) + assert form.fermentables[0].form.color.data == \ + Decimal(recipe['fermentables'][0]['color']) + assert len(form.hops) == len(recipe['hops']) + assert form.hops[0].form.name.data == recipe['hops'][0]['name'] + assert form.hops[0].form.use.data == recipe['hops'][0]['use'] + assert form.hops[0].form.alpha.data == Decimal(recipe['hops'][0]['alpha']) + assert form.hops[0].form.duration.data == \ + Decimal(recipe['hops'][0]['duration']) + assert form.hops[0].form.amount.data == \ + Decimal(recipe['hops'][0]['amount']) + + recipe['yeast'] = { + 'name': 'Test', 'low_attenuation': '65', 'high_attenuation': '68' + } + recipe['mash'] = {} + with app.app_context(): + form = RecipeForm() + form.copyfrom(recipe) + assert form.yeast.form.name.data == recipe['yeast']['name'] + assert form.yeast.form.low_attenuation.data == \ + Decimal(recipe['yeast']['low_attenuation']) + assert form.yeast.form.high_attenuation.data == \ + Decimal(recipe['yeast']['high_attenuation']) + + recipe['yeast'].update({ + 'type': 'Liquid', + 'lab': 'Test', + 'code': 'Test', + 'flocculation': 'Low', + 'min_temperature': '65', + 'max_temperature': '68', + 'abv_tolerance': '15' + }) + with app.app_context(): + form = RecipeForm() + form.copyfrom(recipe) + assert form.yeast.form.type.data == recipe['yeast']['type'] + assert form.yeast.form.lab.data == recipe['yeast']['lab'] + assert form.yeast.form.code.data == recipe['yeast']['code'] + assert form.yeast.form.flocculation.data == recipe['yeast']['flocculation'] + assert form.yeast.form.min_temperature.data == \ + Decimal(recipe['yeast']['min_temperature']) + assert form.yeast.form.max_temperature.data == \ + Decimal(recipe['yeast']['max_temperature']) + assert form.yeast.form.abv_tolerance.data == \ + Decimal(recipe['yeast']['abv_tolerance']) + + recipe['mash'] = { + 'name': 'Test', + 'steps': [{ + 'name': 'Infusion', + 'type': 'Infusion', + 'temp': '152', + 'time': '60' + }] + } + with app.app_context(): + form = RecipeForm() + form.copyfrom(recipe) + assert form.mash.form.name.data == recipe['mash']['name'] + assert len(form.mash.form.steps) == len(recipe['mash']['steps']) + assert form.mash.form.steps[0].form.name.data == \ + recipe['mash']['steps'][0]['name'] + assert form.mash.form.steps[0].form.type.data == \ + recipe['mash']['steps'][0]['type'] + assert form.mash.form.steps[0].form.temp.data == \ + Decimal(recipe['mash']['steps'][0]['temp']) + assert form.mash.form.steps[0].form.time.data == \ + Decimal(recipe['mash']['steps'][0]['time']) + + recipe['mash']['steps'][0]['amount'] = '3.5' + with app.app_context(): + form = RecipeForm() + form.copyfrom(recipe) + assert form.mash.form.steps[0].form.amount.data == \ + Decimal(recipe['mash']['steps'][0]['amount'])