fix duplicate slugs when testing

This commit is contained in:
hay-kot 2021-03-27 13:20:59 -08:00
commit 1a69e03ee3
12 changed files with 27 additions and 57 deletions

View file

@ -42,7 +42,6 @@ def update_generics(func):
elif attribute:
setattr(class_object, key, value)
print("Complex", complex_attributed)
func(class_object, complex_attributed)
return wrapper

View file

@ -23,7 +23,6 @@ async def get_current_user(token: str = Depends(oauth2_scheme), session=Depends(
if username is None:
raise credentials_exception
token_data = TokenData(username=username)
print("Login Payload", token_data)
except JWTError:
raise credentials_exception
user = db.users.get(session, token_data.username, "email")

View file

@ -77,6 +77,5 @@ def get_today(
group_in_db: GroupInDB = db.groups.get(session, current_user.group, "name")
recipe = get_todays_meal(session, group_in_db)
print(datetime.date.today())
return recipe.slug

View file

@ -68,14 +68,11 @@ async def update_user(
token = None
if current_user.id == id or current_user.admin:
print("Current User")
db.users.update(session, id, new_data.dict())
if current_user.id == id:
print(new_data.email)
access_token = security.create_access_token(data=dict(sub=new_data.email), expires_delta=timedelta(hours=2))
token = {"access_token": access_token, "token_type": "bearer"}
print(SnackResponse.success("User Updated", token))
return SnackResponse.success("User Updated", token)

View file

@ -89,13 +89,16 @@ class ImportDatabase:
# Migration from list to Object Type Data
try:
if "" in recipe_dict["tags"]:
recipe_dict["tags"] = [tag for tag in recipe_dict["tags"] if not tag == ""]
recipe_dict["tags"] = [tag for tag in recipe_dict["tags"] if tag != ""]
except:
pass
try:
if "" in recipe_dict["categories"]:
recipe_dict["categories"] = [cat for cat in recipe_dict["categories"] if not cat == ""]
recipe_dict["categories"] = [
cat for cat in recipe_dict["categories"] if cat != ""
]
except:
pass
@ -239,16 +242,15 @@ class ImportDatabase:
item = db_table.get(self.session, search_value, search_key)
if item:
if self.force_imports:
primary_key = getattr(item, db_table.primary_key)
db_table.delete(self.session, primary_key)
else:
if not self.force_imports:
return return_model(
name=model_name,
status=False,
exception=f"Table entry with matching '{search_key}': '{search_value}' exists",
)
primary_key = getattr(item, db_table.primary_key)
db_table.delete(self.session, primary_key)
try:
db_table.create(self.session, model.dict())
import_status = return_model(name=model_name, status=True)
@ -301,12 +303,10 @@ def import_database(
import_session.clean_up()
data = {
return {
"recipeImports": recipe_report,
"settingsImports": settings_report,
"themeImports": theme_report,
"groupImports": group_report,
"userImports": user_report,
}
return data

View file

@ -4,26 +4,16 @@ from typing import Union
import pytz
from mealie.db.database import db
from mealie.db.db_setup import create_session
from pydantic.tools import T
from mealie.schema.meal import (
MealIn,
MealOut,
MealPlanIn,
MealPlanInDB,
MealPlanProcessed,
)
from mealie.schema.meal import MealIn, MealOut, MealPlanIn, MealPlanInDB, MealPlanProcessed
from mealie.schema.recipe import Recipe
from mealie.schema.user import GroupInDB
from pydantic.tools import T
from sqlalchemy.orm.session import Session
def process_meals(session: Session, meal_plan_base: MealPlanIn) -> MealPlanProcessed:
meals = []
for x, meal in enumerate(meal_plan_base.meals):
# europe = pytz.timezone("America/Anchorage")
# d = europe.localize(meal_plan_base.startDate)
# print(d)
meal: MealIn
try:
recipe: Recipe = db.recipes.get(session, meal.slug)

View file

@ -2,11 +2,12 @@ import shutil
from pathlib import Path
import yaml
from fastapi.logger import logger
from mealie.core.config import IMG_DIR, TEMP_DIR
from mealie.db.database import db
from mealie.schema.recipe import Recipe
from sqlalchemy.orm.session import Session
from mealie.utils.unzip import unpack_zip
from sqlalchemy.orm.session import Session
try:
from yaml import CLoader as Loader
@ -50,11 +51,8 @@ def read_chowdown_file(recipe_file: Path) -> Recipe:
"tags": recipe_data.get("tags").split(","),
}
print(reformat_data)
reformated_list = [{"text": instruction} for instruction in reformat_data["recipeInstructions"]]
reformated_list = []
for instruction in reformat_data["recipeInstructions"]:
reformated_list.append({"text": instruction})
reformat_data["recipeInstructions"] = reformated_list
return Recipe(**reformat_data)
@ -63,21 +61,12 @@ def read_chowdown_file(recipe_file: Path) -> Recipe:
def chowdown_migrate(session: Session, zip_file: Path):
temp_dir = unpack_zip(zip_file)
print(temp_dir.name)
path = Path(temp_dir.name)
for p in path.iterdir():
print("ItterDir", p)
for p in p.iterdir():
print("Sub Itter", p)
with temp_dir as dir:
chow_dir = next(Path(dir).iterdir())
image_dir = TEMP_DIR.joinpath(chow_dir, "images")
recipe_dir = TEMP_DIR.joinpath(chow_dir, "_recipes")
print(image_dir.exists())
print(recipe_dir.exists())
failed_recipes = []
successful_recipes = []
for recipe in recipe_dir.glob("*.md"):
@ -86,17 +75,18 @@ def chowdown_migrate(session: Session, zip_file: Path):
db.recipes.create(session, new_recipe.dict())
successful_recipes.append(new_recipe.name)
except Exception as inst:
session.rollback()
logger.error(inst)
failed_recipes.append(recipe.stem)
failed_images = []
for image in image_dir.iterdir():
try:
if not image.stem in failed_recipes:
if image.stem not in failed_recipes:
shutil.copy(image, IMG_DIR.joinpath(image.name))
except Exception as inst:
print(inst)
logger.error(inst)
failed_images.append(image.name)
report = {"successful": successful_recipes, "failed": failed_recipes}
return report

View file

@ -52,8 +52,7 @@ class Cleaner:
@staticmethod
def html(raw_html):
cleanr = re.compile("<.*?>")
cleantext = re.sub(cleanr, "", raw_html)
return cleantext
return re.sub(cleanr, "", raw_html)
@staticmethod
def image(image=None) -> str:
@ -142,12 +141,11 @@ class Cleaner:
@staticmethod
def time(time_entry):
print(time_entry, type(time_entry))
if time_entry == None:
if time_entry is None:
return None
elif type(time_entry) == datetime:
print(time_entry)
elif type(time_entry) != str:
return str(time_entry)
elif time_entry != None:
else:
return time_entry

View file

@ -25,13 +25,10 @@ def create_from_url(url: str) -> Recipe:
"""
r = requests.get(url)
new_recipe = extract_recipe_from_html(r.text, url)
print(new_recipe)
new_recipe = Cleaner.clean(new_recipe, url)
new_recipe = download_image_for_recipe(new_recipe)
recipe = Recipe(**new_recipe)
return recipe
return Recipe(**new_recipe)
def extract_recipe_from_html(html: str, url: str) -> dict:

View file

@ -34,6 +34,7 @@ def test_upload_chowdown_zip(api_client, chowdown_zip):
def test_import_chowdown_directory(api_client, chowdown_zip):
api_client.delete(f"{RECIPES_PREFIX}/roasted-okra") # TODO: Manage Test Data better
selection = chowdown_zip.name
response = api_client.post(f"{MIGRATIONS_PREFIX}/chowdown/{selection}/import")

View file

@ -14,6 +14,7 @@ def test_create_by_url(api_client, recipe_data: RecipeTestData):
def test_create_by_json(api_client):
api_client.delete(f"{RECIPES_PREFIX}/banana-bread")
response = api_client.post(RECIPES_CREATE, json=raw_recipe)
assert response.status_code == 201

View file

@ -66,7 +66,6 @@ def test_update_user(api_client: requests, token):
response = api_client.put(f"{BASE}/1", headers=token, json=update_data)
assert response.status_code == 200
print(response.text)
assert json.loads(response.text).get("access_token")