mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-05 23:43:11 -05:00
Refactor/conver to controllers (#923)
* add dependency injection for get_repositories * convert events api to controller * update generic typing * add abstract controllers * update test naming * migrate admin services to controllers * add additional admin route tests * remove print * add public shared dependencies * add types * fix typo * add static variables for recipe json keys * add coverage gutters config * update controller routers * add generic success response * add category/tag/tool tests * add token refresh test * add coverage utilities * covert comments to controller * add todo * add helper properties * delete old service * update test notes * add unit test for pretty_stats * remove dead code from post_webhooks * update group routes to use controllers * add additional group test coverage * abstract common permission checks * convert ingredient parser to controller * update recipe crud to use controller * remove dead-code * add class lifespan tracker for debugging * convert bulk export to controller * migrate tools router to controller * update recipe share to controller * move customer router to _base * ignore prints in flake8 * convert units and foods to new controllers * migrate user routes to controllers * centralize error handling * fix invalid ref * reorder fields * update routers to share common handling * update tests * remove prints * fix cookbooks delete * fix cookbook get * add controller for mealplanner * cover report routes to controller * remove __future__ imports * remove dead code * remove all base_http children and remove dead code
This commit is contained in:
@@ -1,2 +1 @@
|
||||
from .dependencies import *
|
||||
from .grouped import *
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
from fastapi import BackgroundTasks, Depends
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
from mealie.schema.user.user import PrivateUser
|
||||
|
||||
from .dependencies import generate_session, get_admin_user, get_current_user, is_logged_in
|
||||
|
||||
|
||||
class RequestContext:
|
||||
def __init__(
|
||||
self,
|
||||
background_tasks: BackgroundTasks,
|
||||
session: Session = Depends(generate_session),
|
||||
user=Depends(get_current_user),
|
||||
):
|
||||
self.session: Session = session
|
||||
self.bg_task: BackgroundTasks = background_tasks
|
||||
self.user: bool = user
|
||||
|
||||
|
||||
class PublicDeps:
|
||||
"""
|
||||
PublicDeps contains the common dependencies for all read operations through the API.
|
||||
Note: The user object is used to definer what assets the user has access to.
|
||||
|
||||
Args:
|
||||
background_tasks: BackgroundTasks
|
||||
session: Session
|
||||
user: bool
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
background_tasks: BackgroundTasks,
|
||||
session: Session = Depends(generate_session),
|
||||
user=Depends(is_logged_in),
|
||||
):
|
||||
self.session: Session = session
|
||||
self.bg_task: BackgroundTasks = background_tasks
|
||||
self.user: bool = user
|
||||
|
||||
|
||||
class UserDeps:
|
||||
"""
|
||||
UserDeps contains the common dependencies for all read operations through the API.
|
||||
Note: The user must be logged in or the route will return a 401 error.
|
||||
|
||||
Args:
|
||||
background_tasks: BackgroundTasks
|
||||
session: Session
|
||||
user: UserInDB
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
background_tasks: BackgroundTasks,
|
||||
session: Session = Depends(generate_session),
|
||||
user=Depends(get_current_user),
|
||||
):
|
||||
self.session: Session = session
|
||||
self.bg_task: BackgroundTasks = background_tasks
|
||||
self.user: PrivateUser = user
|
||||
|
||||
|
||||
class AdminDeps:
|
||||
def __init__(
|
||||
self,
|
||||
background_tasks: BackgroundTasks,
|
||||
session: Session = Depends(generate_session),
|
||||
user=Depends(get_admin_user),
|
||||
):
|
||||
self.session: Session = session
|
||||
self.bg_task: BackgroundTasks = background_tasks
|
||||
self.user: PrivateUser = user
|
||||
31
mealie/core/exceptions.py
Normal file
31
mealie/core/exceptions.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from sqlite3 import IntegrityError
|
||||
|
||||
from mealie.lang.providers import AbstractLocaleProvider
|
||||
|
||||
|
||||
class PermissionDenied(Exception):
|
||||
"""
|
||||
This exception is raised when a user tries to access a resource that they do not have permission to access.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class NoEntryFound(Exception):
|
||||
"""
|
||||
This exception is raised when a user tries to access a resource that does not exist.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def mealie_registered_exceptions(t: AbstractLocaleProvider) -> dict:
|
||||
"""
|
||||
This function returns a dictionary of all the globally registered exceptions in the Mealie application.
|
||||
"""
|
||||
|
||||
return {
|
||||
PermissionDenied: t.t("exceptions.permission-denied"),
|
||||
NoEntryFound: t.t("exceptions.no-entry-found"),
|
||||
IntegrityError: t.t("exceptions.integrity-error"),
|
||||
}
|
||||
@@ -14,7 +14,6 @@ settings = get_app_settings()
|
||||
LOGGER_FILE = DATA_DIR.joinpath("mealie.log")
|
||||
DATE_FORMAT = "%d-%b-%y %H:%M:%S"
|
||||
LOGGER_FORMAT = "%(levelname)s: %(asctime)s \t%(message)s"
|
||||
LOGGER_HANDLER = None
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import secrets
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
Reference in New Issue
Block a user