Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function admin_create_recipe refactored with the following changes:

  • Remove unreachable code (remove-unreachable-code)

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
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)
i + 1
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'))
Comment on lines -153 to +193
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function submit_recipe refactored with the following changes:

  • Remove redundant conditional (remove-redundant-if)
  • Add guard clause (last-if-guard)
  • Replace assignment with augmented assignment (aug-assign)


@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))]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SearchableMixin.search refactored with the following changes:

  • Convert for loop into list comprehension (list-comprehension)

return cls.query.filter(cls.id.in_(ids)).order_by(db.case(when, value=cls.id)), total

@classmethod
Expand Down