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

Streamlining multiple tagging

parent a2acc595
......@@ -9,6 +9,22 @@ from ..models import Permission, Role, User, Recipe, Comment, Cuisine, RecipeSte
from ..decorators import admin_required, permission_required
def addCuisine(name):
if Cuisine.query.filter_by(name=name).first() is None:
cuisines = [Cuisine(name=name, timestamp='now', author=current_user._get_current_object())]
else:
cuisines = [Cuisine.query.filter_by(name=name).first()]
return cuisines
def addMethod(name):
if Method.query.filter_by(name=name).first() is None:
methods = [Method(name=name, timestamp='now', author=current_user._get_current_object())]
else:
methods = [Method.query.filter_by(name=name).first()]
return methods
@main.route('/')
def index():
page = request.args.get('page', 1, type=int)
......@@ -351,19 +367,18 @@ def editrecipe(id):
form = RecipeForm()
if form.validate_on_submit():
for cuisine in form.cuisines.data:
if Cuisine.query.filter_by(name=cuisine).first() is None:
cuisines = [Cuisine(name=cuisine, timestamp='now', author=current_user._get_current_object())]
else:
cuisines = [Cuisine.query.filter_by(name=cuisine).first()]
recipe.cuisines.extend(cuisines)
for method in form.methods.data:
if Method.query.filter_by(name=method).first() is None:
methods = [Method(name=method, timestamp='now', author=current_user._get_current_object())]
else:
methods = [Method.query.filter_by(name=method).first()]
recipe.methods.extend(methods)
for i, cuisine in enumerate (form.cuisines.data):
cuisine = addCuisine(cuisine)
recipe.cuisines.extend(cuisine)
for i, method in enumerate (form.methods.data):
method = addMethod(method)
recipe.methods.extend(method)
recipe.name = form.name.data
recipe.description = form.description.data
......@@ -397,15 +412,12 @@ def editmethod(id):
form = MethodForm()
if form.validate_on_submit():
for cuisine in form.cuisines.data:
if Cuisine.query.filter_by(name=cuisine).first() is None:
cuisines = [Cuisine(name=cuisine, timestamp='now', author=current_user._get_current_object())]
else:
cuisines = [Cuisine.query.filter_by(name=cuisine).first()]
for i, cuisine in enumerate (form.cuisines.data):
cuisine = addCuisine(cuisine)
method.cuisines.extend(cuisine)
method.name = form.name.data
method.description = form.description.data
method.cuisines.extend(cuisines)
method.method_text = form.method_text.data
db.session.add(method)
flash('The method has been updated.')
......@@ -429,12 +441,9 @@ def editcuisine(id):
cuisine.name = form.name.data
cuisine.description = form.description.data
for method in form.methods.data:
if Method.query.filter_by(name=method).first() is None:
methods = [Method(name=method, timestamp='now', author=current_user._get_current_object())]
else:
methods = [Method.query.filter_by(name=method).first()]
cuisine.methods.extend(methods)
for i, method in enumerate (form.methods.data):
method = addMethod(method)
cuisine.methods.extend(method)
db.session.add(cuisine)
flash('The cuisine has been updated.')
......
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