forked from mealie-recipes/mealie
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: random sort option for front page (mealie-recipes#2363)
* Add hook for random sorting * Add random sorting to front page * Add multiple tests for random sorting. * Be extra sure that all recipes are returned. * Too stable random. seed doesn't reach backend. * add timestamp to useRecipeSearch * Update randomization tests for timestamp seeding * ruff cleanup * pass timestamp separately in getAll * remove debugging log items * remove timestamp from address bar * remove defaults from backend timestamps * timestamp should be optional * fix edge case: query without timestamp * similar edge case: no timestamp in pagination * ruff :/ * better edge case handling * stabilize random search test w/more recipes * better pagination seeding * update pagination seed test * remove redundant random/seed check * Test for api routes to random sorting. * please the typing gods * hack to make query parameters throw correct exc * ruff * fix validator message typo * black reformatting --------- Co-authored-by: Hayden <[email protected]>
- Loading branch information
Showing
10 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from inspect import signature | ||
|
||
from fastapi.exceptions import HTTPException, ValidationError | ||
|
||
|
||
def make_dependable(cls): | ||
""" | ||
Pydantic BaseModels are very powerful because we get lots of validations and type checking right out of the box. | ||
FastAPI can accept a BaseModel as a route Dependency and it will automatically handle things like documentation | ||
and error handling. However, if we define custom validators then the errors they raise are not handled, leading | ||
to HTTP 500's being returned. | ||
To better understand this issue, you can visit https://github.com/tiangolo/fastapi/issues/1474 for context. | ||
A workaround proposed there adds a classmethod which attempts to init the BaseModel and handles formatting of | ||
any raised ValidationErrors, custom or otherwise. However, this means essentially duplicating the class's | ||
signature. This function automates the creation of a workaround method with a matching signature so that you | ||
can avoid code duplication. | ||
usage: | ||
async def fetch(thing_request: ThingRequest = Depends(make_dependable(ThingRequest))): | ||
""" | ||
|
||
def init_cls_and_handle_errors(*args, **kwargs): | ||
try: | ||
signature(init_cls_and_handle_errors).bind(*args, **kwargs) | ||
return cls(*args, **kwargs) | ||
except ValidationError as e: | ||
for error in e.errors(): | ||
error["loc"] = ["query"] + list(error["loc"]) | ||
raise HTTPException(422, detail=e.errors()) from None | ||
|
||
init_cls_and_handle_errors.__signature__ = signature(cls) | ||
return init_cls_and_handle_errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters