58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
from ..models import db
|
|
from ..models.session import Recipe, Vote
|
|
from .recipes import get_recipes
|
|
|
|
|
|
def serialize_recipe(recipe):
|
|
return {
|
|
"id": recipe.id,
|
|
"title": recipe.title,
|
|
"url": recipe.url,
|
|
"imageUrl": recipe.image_url,
|
|
"meta": recipe.meta,
|
|
"position": recipe.position,
|
|
}
|
|
|
|
|
|
def add_recipe_batch(session, size=8):
|
|
existing_urls = {recipe.url for recipe in session.recipes}
|
|
next_position = len(session.recipes)
|
|
added = 0
|
|
|
|
for _ in range(3):
|
|
for recipe_data in get_recipes():
|
|
if recipe_data["url"] in existing_urls:
|
|
continue
|
|
|
|
session.recipes.append(Recipe(position=next_position, **recipe_data))
|
|
existing_urls.add(recipe_data["url"])
|
|
next_position += 1
|
|
added += 1
|
|
|
|
if added == size:
|
|
db.session.flush()
|
|
return added
|
|
|
|
if added:
|
|
db.session.flush()
|
|
return added
|
|
|
|
for recipe_data in get_recipes()[:size]:
|
|
session.recipes.append(Recipe(position=next_position, **recipe_data))
|
|
next_position += 1
|
|
added += 1
|
|
|
|
db.session.flush()
|
|
return added
|
|
|
|
|
|
def replace_recipe_batch(session, size=8):
|
|
session.matched_recipe_id = None
|
|
Vote.query.filter_by(session_id=session.id).delete()
|
|
for recipe in list(session.recipes):
|
|
db.session.delete(recipe)
|
|
|
|
db.session.flush()
|
|
session.recipes.clear()
|
|
return add_recipe_batch(session, size)
|