Coverage for opt/mealie/lib/python3.12/site-packages/mealie/routes/_base/mixins.py: 90%
66 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 13:45 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 13:45 +0000
1from collections.abc import Callable 1l
2from logging import Logger 1l
4import sqlalchemy.exc 1l
5from fastapi import HTTPException, status 1l
6from pydantic import UUID4, BaseModel 1l
8from mealie.repos.repository_generic import RepositoryGeneric 1l
9from mealie.schema.response import ErrorResponse 1l
12class HttpRepo[C: BaseModel, R: BaseModel, U: BaseModel]: 1l
13 """
14 The HttpRepo[C, R, U] class is a mixin class that provides a common set of methods for CRUD operations.
15 This class is intended to be used in a composition pattern where a class has a mixin property. For example:
17 ```
18 class MyClass:
19 def __init__(self, repo, logger):
20 self.mixins = HttpRepo(repo, logger)
21 ```
23 """
25 repo: RepositoryGeneric 1l
26 exception_msgs: Callable[[type[Exception]], str] | None 1l
27 default_message: str = "An unexpected error occurred." 1l
29 def __init__( 1l
30 self,
31 repo: RepositoryGeneric,
32 logger: Logger,
33 exception_msgs: Callable[[type[Exception]], str] | None = None,
34 default_message: str | None = None,
35 ) -> None:
36 self.repo = repo 1ekbicgjhfmdnoa
37 self.logger = logger 1ekbicgjhfmdnoa
38 self.exception_msgs = exception_msgs 1ekbicgjhfmdnoa
40 if default_message: 1ekbicgjhfmdnoa
41 self.default_message = default_message 1ebicgjhfmdna
43 def get_exception_message(self, ext: Exception) -> str: 1l
44 if self.exception_msgs: 1ekbicgjhfmdna
45 return self.exception_msgs(type(ext)) 1ekbicgjhfmdna
46 return self.default_message 1ebicgfmdna
48 def handle_exception(self, ex: Exception) -> None: 1l
49 # Cleanup
50 self.logger.exception(ex) 1ekbicgjhfmdna
51 self.repo.session.rollback() 1ekbicgjhfmdna
53 # Respond
54 msg = self.get_exception_message(ex) 1ekbicgjhfmdna
56 if isinstance(ex, sqlalchemy.exc.NoResultFound): 1ekbicgjhfmdna
57 raise HTTPException( 1ebgfa
58 status.HTTP_404_NOT_FOUND,
59 detail=ErrorResponse.respond(message=msg, exception=str(ex)),
60 )
61 else:
62 raise HTTPException( 1ekbicgjhfmdna
63 status.HTTP_400_BAD_REQUEST,
64 detail=ErrorResponse.respond(message=msg, exception=str(ex)),
65 )
67 def create_one(self, data: C) -> R | None: 1l
68 item: R | None = None 1ekbicgjhfmdnoa
69 try: 1ekbicgjhfmdnoa
70 item = self.repo.create(data) 1ekbicgjhfmdnoa
71 except Exception as ex: 1ekbicgjhfmdna
72 self.handle_exception(ex) 1ekbicgjhfmdna
74 return item 1ekbicgjhfmdnoa
76 def get_one(self, item_id: int | str | UUID4, key: str | None = None) -> R: 1l
77 item = self.repo.get_one(item_id, key) 1ekbicjhfda
79 if not item: 1ekbicjhfda
80 raise HTTPException( 1ekbicjda
81 status.HTTP_404_NOT_FOUND,
82 detail=ErrorResponse.respond(message="Not found."),
83 )
85 return item 1bicjhfda
87 def update_one(self, data: U, item_id: int | str | UUID4) -> R: 1l
88 item = self.repo.get_one(item_id) 1bcda
90 if not item: 1bcda
91 raise HTTPException(
92 status.HTTP_404_NOT_FOUND,
93 detail=ErrorResponse.respond(message="Not found."),
94 )
96 try: 1bcda
97 item = self.repo.update(item_id, data) # type: ignore 1bcda
98 except Exception as ex:
99 self.handle_exception(ex)
101 return item 1bcda
103 def patch_one(self, data: U, item_id: int | str | UUID4) -> R: 1l
104 item = self.repo.get_one(item_id)
106 if not item: 106 ↛ 112line 106 didn't jump to line 112 because the condition on line 106 was always true
107 raise HTTPException(
108 status.HTTP_404_NOT_FOUND,
109 detail=ErrorResponse.respond(message="Not found."),
110 )
112 try:
113 item = self.repo.patch(item_id, data.model_dump(exclude_unset=True, exclude_defaults=True))
114 except Exception as ex:
115 self.handle_exception(ex)
117 return item
119 def delete_one(self, item_id: int | str | UUID4) -> R | None: 1l
120 item: R | None = None 1ebcghfdoa
121 try: 1ebcghfdoa
122 item = self.repo.delete(item_id) 1ebcghfdoa
123 self.logger.info(f"Deleting item with id {item_id}") 1cghdoa
124 except Exception as ex: 1ebgfa
125 self.handle_exception(ex) 1ebgfa
127 return item 1cghdoa