mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-09 01:13:10 -05:00
improve developer tooling (backend) (#1051)
* add basic pre-commit file * add flake8 * add isort * add pep585-upgrade (typing upgrades) * use namespace for import * add mypy * update ci for backend * flake8 scope * fix version format * update makefile * disable strict option (temporary) * fix mypy issues * upgrade type hints (pre-commit) * add vscode typing check * add types to dev deps * remote container draft * update setup script * update compose version * run setup on create * dev containers update * remove unused pages * update setup tips * expose ports * Update pre-commit to include flask8-print (#1053) * Add in flake8-print to pre-commit * pin version of flake8-print * formatting * update getting strated docs * add mypy to pre-commit * purge .mypy_cache on clean * drop mypy Co-authored-by: zackbcom <zackbcom@users.noreply.github.com>
This commit is contained in:
@@ -38,7 +38,7 @@ class RecipeDataService(BaseService):
|
||||
except Exception as e:
|
||||
self.logger.exception(f"Failed to delete recipe data: {e}")
|
||||
|
||||
def write_image(self, file_data: bytes, extension: str) -> Path:
|
||||
def write_image(self, file_data: bytes | Path, extension: str) -> Path:
|
||||
extension = extension.replace(".", "")
|
||||
image_path = self.dir_image.joinpath(f"original.{extension}")
|
||||
image_path.unlink(missing_ok=True)
|
||||
@@ -91,8 +91,8 @@ class RecipeDataService(BaseService):
|
||||
if ext not in img.IMAGE_EXTENSIONS:
|
||||
ext = "jpg" # Guess the extension
|
||||
|
||||
filename = str(self.recipe_id) + "." + ext
|
||||
filename = Recipe.directory_from_id(self.recipe_id).joinpath("images", filename)
|
||||
file_name = f"{str(self.recipe_id)}.{ext}"
|
||||
file_path = Recipe.directory_from_id(self.recipe_id).joinpath("images", file_name)
|
||||
|
||||
try:
|
||||
r = requests.get(image_url, stream=True, headers={"User-Agent": _FIREFOX_UA})
|
||||
@@ -102,7 +102,7 @@ class RecipeDataService(BaseService):
|
||||
|
||||
if r.status_code == 200:
|
||||
r.raw.decode_content = True
|
||||
self.logger.info(f"File Name Suffix {filename.suffix}")
|
||||
self.write_image(r.raw, filename.suffix)
|
||||
self.logger.info(f"File Name Suffix {file_path.suffix}")
|
||||
self.write_image(r.raw, file_path.suffix)
|
||||
|
||||
filename.unlink(missing_ok=True)
|
||||
file_path.unlink(missing_ok=True)
|
||||
|
||||
@@ -69,7 +69,6 @@ class RecipeService(BaseService):
|
||||
all_asset_files = [x.file_name for x in recipe.assets]
|
||||
|
||||
for file in recipe.asset_dir.iterdir():
|
||||
file: Path
|
||||
if file.is_dir():
|
||||
continue
|
||||
if file.name not in all_asset_files:
|
||||
@@ -102,13 +101,13 @@ class RecipeService(BaseService):
|
||||
|
||||
def create_one(self, create_data: Union[Recipe, CreateRecipe]) -> Recipe:
|
||||
|
||||
create_data: Recipe = self._recipe_creation_factory(
|
||||
data: Recipe = self._recipe_creation_factory(
|
||||
self.user,
|
||||
name=create_data.name,
|
||||
additional_attrs=create_data.dict(),
|
||||
)
|
||||
|
||||
create_data.settings = RecipeSettings(
|
||||
data.settings = RecipeSettings(
|
||||
public=self.group.preferences.recipe_public,
|
||||
show_nutrition=self.group.preferences.recipe_show_nutrition,
|
||||
show_assets=self.group.preferences.recipe_show_assets,
|
||||
@@ -117,7 +116,7 @@ class RecipeService(BaseService):
|
||||
disable_amount=self.group.preferences.recipe_disable_amount,
|
||||
)
|
||||
|
||||
return self.repos.recipes.create(create_data)
|
||||
return self.repos.recipes.create(data)
|
||||
|
||||
def create_from_zip(self, archive: UploadFile, temp_path: Path) -> Recipe:
|
||||
"""
|
||||
|
||||
@@ -27,7 +27,7 @@ class TemplateService(BaseService):
|
||||
super().__init__()
|
||||
|
||||
@property
|
||||
def templates(self) -> list:
|
||||
def templates(self) -> dict[str, list[str]]:
|
||||
"""
|
||||
Returns a list of all templates available to render.
|
||||
"""
|
||||
@@ -78,6 +78,8 @@ class TemplateService(BaseService):
|
||||
if t_type == TemplateType.zip:
|
||||
return self._render_zip(recipe)
|
||||
|
||||
raise ValueError(f"Template Type '{t_type}' not found.")
|
||||
|
||||
def _render_json(self, recipe: Recipe) -> Path:
|
||||
"""
|
||||
Renders a JSON file in a temporary directory and returns
|
||||
@@ -98,18 +100,18 @@ class TemplateService(BaseService):
|
||||
"""
|
||||
self.__check_temp(self._render_jinja2)
|
||||
|
||||
j2_template: Path = self.directories.TEMPLATE_DIR / j2_template
|
||||
j2_path: Path = self.directories.TEMPLATE_DIR / j2_template
|
||||
|
||||
if not j2_template.is_file():
|
||||
raise FileNotFoundError(f"Template '{j2_template}' not found.")
|
||||
if not j2_path.is_file():
|
||||
raise FileNotFoundError(f"Template '{j2_path}' not found.")
|
||||
|
||||
with open(j2_template, "r") as f:
|
||||
with open(j2_path, "r") as f:
|
||||
template_text = f.read()
|
||||
|
||||
template = Template(template_text)
|
||||
rendered_text = template.render(recipe=recipe.dict(by_alias=True))
|
||||
|
||||
save_name = f"{recipe.slug}{j2_template.suffix}"
|
||||
save_name = f"{recipe.slug}{j2_path.suffix}"
|
||||
|
||||
save_path = self.temp.joinpath(save_name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user