Commit cc30dbb4 authored by Francesca Sargent 's avatar Francesca Sargent
Browse files

Fixed step submission issue, duplicate ingredients on Recipe page

parent ce1f6560
......@@ -367,7 +367,25 @@ def edit_profile_admin(id):
@main.route('/recipe/<int:id>', methods=['GET', 'POST'])
def recipe(id):
recipe = [Recipe.query.get_or_404(id)]
recipe = Recipe.query.get_or_404(id)
ingredients = []
for step in recipe.steps:
for ingredient in step.ingredients:
ingredients.append(ingredient)
if ingredient.name in ingredients:
print "match"
else:
ingredients.append(ingredient)
ingredients = list(set(ingredients))
print ingredients
# print ingredients
# form = CommentForm()
# if form.validate_on_submit():
# comment = Comment(body=form.body.data,
......@@ -384,7 +402,7 @@ def recipe(id):
# page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
# error_out=False)
# comments = pagination.items
return render_template('recipe.html', recipes=recipe)
return render_template('recipe.html', recipes=[recipe], ingredients=ingredients)
@main.route('/cuisine/<int:id>', methods=['GET', 'POST'])
def cuisine(id):
......@@ -412,10 +430,15 @@ def editrecipe(id):
not current_user.can(Permission.ADMINISTER):
abort(403)
if recipe.steps.count() == 0:
stepscount = 1
else:
stepscount = recipe.steps.count()
class F(RecipeEditForm):
name = StringField('Recipe Name', validators=[Required(), Length(0, 64)])
description = PageDownField('Recipe Description')
steps = FieldList(FormField(RecipeStepForm), min_entries=(recipe.steps.count())+1)
steps = FieldList(FormField(RecipeStepForm), min_entries=stepscount)
cuisines = SplitStringField(validators=[Required()])
methods = SplitStringField(validators=[Required()])
submit = SubmitField('Submit')
......@@ -444,30 +467,29 @@ def editrecipe(id):
pass
else:
step_id = (str(recipe.id)+str(index))
step_id = (str(recipe.id)+str(index))
step_exist = RecipeSteps.query.get(step_id)
if step_exist != None:
step = RecipeSteps.query.get(step_id)
if RecipeSteps.query.get(step_id) != None:
step = RecipeSteps.query.get(step_id)
else:
step = RecipeSteps()
step.id = (str(recipe.id)+str(index))
step.step_id = index
step.step_text = step_entry.step_text.data
step.recipe_id = recipe.id
for ingredient in step_entry.ingredients.data:
if ingredient is '':
pass
else:
ingred = addIngredient(ingredient)
step.ingredients.extend(ingred)
db.session.add(step)
if step_entry.ingredients.data == []:
db.session.add(step)
else:
for ingredient in step_entry.ingredients.data:
if ingredient is '':
continue
else:
ingred = addIngredient(ingredient)
step.ingredients.extend(ingred)
db.session.add(step)
db.session.add(recipe)
......
......@@ -176,6 +176,7 @@ $('ul#steps').after('<div id="add_another_button">Add</div><div class="remove_th
$('div#add_another_button').click(function () {
console.log('clicked');
last_li = $(this).prev('li')
clone_field_list('li:last');
var lis = document.querySelectorAll('.steptext').length;
......@@ -202,9 +203,11 @@ function clone_field_list(selector) {
var elem_id = new_element.find(':input')[0].id;
var elem_num = parseInt(elem_id.replace(/.*-(\d{1,4})-.*/m, '$1')) + 1;
new_element.find(':input').each(function() {
var id = $(this).attr('id').replace('-' + (elem_num - 1) + '-', '-' + elem_num + '-');
var id = $(this).attr('id', function(i,txt) {return txt.replace(/\d+/, elem_num); });
var name = $(this).attr('name', function(i,txt) {return txt.replace(/\d+/, elem_num); });
$(this).val('')
$(this).addClass('steptext');
$(this).html('');
$(this).html('');
});
new_element.find('label').each(function() {
var new_for = $(this).attr('for').replace('-' + (elem_num - 1) + '-', '-' + elem_num + '-');
......
......@@ -18,13 +18,9 @@ Created by <a href="{{ url_for('.user', username=recipe.author.username) }}">{{
{% if recipe.methods.count() > 0 %}
<strong>Associated Methods: </strong> {% for method in recipe.methods %} <span class="badge"> <a href="{{ url_for('.method', id=method.id) }}">{{ method.name }}</a> </span>{% endfor %} <br/>
{% endif %}
<strong>Ingredients: </strong> {% for step in recipe.steps %}
{% if step.ingredients %}
{% for ingredient in step.ingredients %}
{% if ingredients|length > 0 %} <strong>Ingredients: </strong> {% for ingredient in ingredients %}
<span class="badge"><a href="{{ url_for('.ingredient', id=ingredient.id) }}"> {{ ingredient.name }} </a></span>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}{% endif %}
</p>
<p>{% if recipe.description %}
{{ recipe.description }}
......
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