Skip to content

Commit

Permalink
Refactored by Sourcery
Browse files Browse the repository at this point in the history
  • Loading branch information
SourceryAI committed Jun 3, 2020
1 parent 6392cb4 commit 0a7f050
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 44 deletions.
2 changes: 1 addition & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_app(config_class=Config):
app.register_blueprint(main_bp)

#Logging errors via email if mail server is set up
if not app.debug and not app.testing:
if not (app.debug or app.testing):
if app.config['MAIL_SERVER']:
auth = None
if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
Expand Down
79 changes: 39 additions & 40 deletions app/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,53 +145,52 @@ def manage_recipes():
def admin_create_recipe():
if current_user.isAdmin is not True:
return('Unable to access')
return redirect(url_for('main.index'))
return render_template('admin_create_recipe.html')

@bp.route('/admin/submit_recipe', methods=['GET','POST'])
def submit_recipe():
if request.method == 'POST':
#pull in all individual form responses. First create the Recipe record w/ no ingredients or steps
recipeName = request.form['recipeName']
mealType = request.form['mealType']
Image = request.form['Image']
prepTime = request.form['prepTime']
if prepTime is None or "" or " ":
prepTime = 0.
cookTime = request.form['cookTime']
if cookTime is None or "" or " ":
cookTime = 0.
recipeCheck = Recipe2.query.filter_by(recipeName=recipeName).first()
if recipeCheck is not None:
flash('A recipe with this name already exists. Please choose a new name for your recipe')
return redirect(url_for('main.admin_create_recipe'))
recipe = Recipe(recipeName=recipeName, mealType=mealType, Image=Image, prepTime=prepTime, cookTime=cookTime)
db.session.add(recipe)
if request.method != 'POST':
return

#pull in all individual form responses. First create the Recipe record w/ no ingredients or steps
recipeName = request.form['recipeName']
mealType = request.form['mealType']
Image = request.form['Image']
prepTime = request.form['prepTime']
prepTime = 0.
cookTime = request.form['cookTime']
cookTime = 0.
recipeCheck = Recipe2.query.filter_by(recipeName=recipeName).first()
if recipeCheck is not None:
flash('A recipe with this name already exists. Please choose a new name for your recipe')
return redirect(url_for('main.admin_create_recipe'))
recipe = Recipe(recipeName=recipeName, mealType=mealType, Image=Image, prepTime=prepTime, cookTime=cookTime)
db.session.add(recipe)
# Pull in ingredient information, then query for the recipe we just created to get ID and create the recipeIngredients record for these ingredients
ingredientNames = request.form.getlist('ingredientName')
ingredientMeasurements = request.form.getlist('ingredientMeasurement')
ingredientAmounts = request.form.getlist('ingredientAmount')
recipeRecord = Recipe2.query.filter_by(recipeName=recipeName).first()
recipeRecordID = recipeRecord.id
ingredientNames = request.form.getlist('ingredientName')
ingredientMeasurements = request.form.getlist('ingredientMeasurement')
ingredientAmounts = request.form.getlist('ingredientAmount')
recipeRecord = Recipe2.query.filter_by(recipeName=recipeName).first()
recipeRecordID = recipeRecord.id
# Iterate over all the ingredients in the recipe Form
i = 0
for ingredientName in ingredientNames:
i = 0
i + 1
for ingredientName in ingredientNames:
# Check if this ingredient already exists in the ingredient table and if not create an ingredient record for that ingredient
ingredient = Ingredients.query.filter_by(ingredientName=ingredientName).first()
if ingredient is None:
newIngredient = Ingredients(ingredientName=ingredientName)
db.session.add(newIngredient)
ingredient = Ingredients.query.filter_by(ingredientName=ingredientName).first()
db.session.add(recipeIngredient)
i + 1
recipeSteps = request.form.getlist('recipeStep')
j=0
for recipeStep in recipeSteps:
j=j+1
step = Recipe_Steps(recipe_id=recipeRecordID, stepNumber=j, directions=recipeStep)
db.session.add(step)
db.session.commit()
return redirect(url_for('main.admin'))
ingredient = Ingredients.query.filter_by(ingredientName=ingredientName).first()
if ingredient is None:
newIngredient = Ingredients(ingredientName=ingredientName)
db.session.add(newIngredient)
ingredient = Ingredients.query.filter_by(ingredientName=ingredientName).first()
db.session.add(recipeIngredient)
recipeSteps = request.form.getlist('recipeStep')
j=0
for recipeStep in recipeSteps:
j += 1
step = Recipe_Steps(recipe_id=recipeRecordID, stepNumber=j, directions=recipeStep)
db.session.add(step)
db.session.commit()
return redirect(url_for('main.admin'))

@bp.route('/admin/users')
@login_required
Expand Down
4 changes: 1 addition & 3 deletions app/search_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ def search(cls, expression, page, per_page):
ids, total = query_index(cls.__tablename__, expression, page, per_page)
if total == 0:
return cls.query.filter_by(id=0), 0
when = []
for i in range(len(ids)):
when.append((ids[i], i))
when = [(ids[i], i) for i in range(len(ids))]
return cls.query.filter(cls.id.in_(ids)).order_by(db.case(when, value=cls.id)), total

@classmethod
Expand Down

0 comments on commit 0a7f050

Please sign in to comment.