Commit 91b7b8df authored by Francesca Sargent 's avatar Francesca Sargent
Browse files

FieldList for adding and editing multiple flavours"

parent 79d710ad
......@@ -79,7 +79,7 @@ class FlavourTypeForm(Form):
class IngredientForm(Form):
name = StringField('Ingredient Name', validators=[Required(), Length(0,64)])
description = TextAreaField('Description')
flavours = FormField(FlavourTypeForm)
flavours = FieldList(FormField(FlavourTypeForm), min_entries=1)
submit = SubmitField('Submit')
......
......@@ -257,6 +257,37 @@ def poststuff(type):
ingredient.description = form.description.data
ingredient.author = current_user._get_current_object()
for index,flavour_entry in enumerate(form.flavours.entries):
if flavour_entry['name'].data == '':
pass
else:
flavourtype_name = flavour_entry['name'].data
print flavourtype_name
flavourtype_characteristics = flavour_entry['characteristics'].data
print flavourtype_characteristics
addFlavourType(flavourtype_name, flavourtype_characteristics)
print flavour_entry.flavours.data
flavour_ingredient_id = ingredient.id
flavour_flavourtype_name = flavour_entry['name'].data
flavour_prevalence = flavour_entry.flavours['prevalence'].data
print flavour_prevalence
if flavour_entry['name'].data == []:
pass
else:
for flavour in flavour_entry.name:
if flavour is '':
continue
else:
addFlavour(flavour_ingredient_id, flavour_flavourtype_name, flavour_prevalence)
db.session.add(ingredient)
db.session.flush()
flash('Your ingredient has been added.')
......@@ -566,7 +597,6 @@ def editcuisine(id):
return render_template('edit_recipe.html', form=form, type='cuisine')
def addFlavourType(namedata, characteristicdata):
print '@@@@@', namedata
if FlavourType.query.filter_by(name=namedata).first() != None:
flavourtype = FlavourType.query.filter_by(name=namedata).first()
else:
......@@ -595,6 +625,7 @@ def addFlavour(flavour_ingredient_id, flavour_flavourtype_name, flavour_prevalen
db.session.add(flavour)
# INGFORM
@main.route('/ingredient/<int:id>/edit', methods=['GET', 'POST'])
@login_required
def editingredient(id):
......@@ -603,46 +634,74 @@ def editingredient(id):
if current_user != ingredient.author and \
not current_user.can(Permission.ADMINISTER):
abort(403)
form = IngredientForm()
if ingredient.flavours.count() == 0:
flavourscount = 1
else:
flavourscount = ingredient.flavours.count()
class I(IngredientForm):
name = StringField('Ingredient Name', validators=[Required(), Length(0,64)])
description = TextAreaField('Description')
flavours = FieldList(FormField(FlavourTypeForm), min_entries=flavourscount)
submit = SubmitField('Submit')
form = I()
if form.validate_on_submit():
ingredient.name = form.name.data
ingredient.description = form.description.data
for index,flavour_entry in enumerate(form.flavours.entries):
if flavour_entry['name'].data == '':
pass
else:
flavourtype_name = flavour_entry['name'].data
print form.flavours.data
print flavourtype_name
# Creating a Flavour Type (the general flavour), which will link to Specific Flavour Details
flavourtype_name = form.flavours['name'].data
flavourtype_characteristics = form.flavours['characteristics'].data
flavourtype_characteristics = flavour_entry['characteristics'].data
print flavourtype_name
addFlavourType(flavourtype_name, flavourtype_characteristics)
print flavourtype_characteristics
addFlavourType(flavourtype_name, flavourtype_characteristics)
print flavour_entry.flavours.data
# Creating a Flavour, linking to Flavour Type and Ingredient
flavour_ingredient_id = ingredient.id
flavour_flavourtype_name = form.flavours['name'].data
flavour_prevalence = form.flavours.flavours['prevalence'].data
addFlavour(flavour_ingredient_id, flavour_flavourtype_name, flavour_prevalence)
flavour_ingredient_id = ingredient.id
flavour_flavourtype_name = flavour_entry.flavours['flavourtype_name'].data
flavour_prevalence = flavour_entry.flavours['prevalence'].data
print flavour_prevalence
if flavour_entry['name'].data == []:
pass
else:
for flavour in flavour_entry.name:
if flavour is '':
continue
else:
addFlavour(flavour_ingredient_id, flavour_flavourtype_name, flavour_prevalence)
db.session.add(ingredient)
db.session.add(ingredient)
flash('The ingredient has been updated.')
return redirect(url_for('.ingredient', id=ingredient.id))
form.name.data = ingredient.name
form.name.data = ingredient.name
form.description.data = ingredient.description
for flavour in ingredient.flavours:
# form.flavours.flavours['flavourtype_name'].data = flavour.flavourtype_name
form.flavours['characteristics'].data = flavour.flavourtype.characteristics
form.flavours['name'].data = flavour.flavourtype_name
for flavour in flavour.flavourtype.flavours:
form.flavours.flavours['prevalence'].data = flavour.prevalence
for i, flavour in enumerate(ingredient.flavours):
form.flavours[i]['characteristics'].data = flavour.flavourtype.characteristics
form.flavours[i]['name'].data = flavour.flavourtype_name
form.flavours.flavours['ingredient_id'].data = ingredient.id
for i, flavour in enumerate(ingredient.flavours):
form.flavours[i].flavours['prevalence'].data = flavour.prevalence
form.flavours[i].flavours['ingredient_id'].data = flavour.ingredient_id
form.flavours[i].flavours['flavourtype_name'].data = flavour.flavourtype_name
return render_template('edit_recipe.html', form=form, type='ingredient')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment