-
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.
add mixin for models search, project goals
- Loading branch information
1 parent
b91d9c1
commit 8e6c972
Showing
6 changed files
with
153 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from django.db.models import Model | ||
from typing import Any, Callable | ||
from rest_framework.status import HTTP_404_NOT_FOUND | ||
|
||
|
||
def with_model_assertion( | ||
model: Model, | ||
accessor: str, | ||
class_based=True, | ||
**kw | ||
) -> Callable: | ||
def decorator(callback: Callable) -> Callable: | ||
def inner(*args, **kwargs) -> Any: | ||
pk_index = int(class_based) | ||
|
||
try: | ||
_model = model.objects \ | ||
.select_related(*kw.get("select", [])) \ | ||
.prefetch_related(*kw.get("prefetch", [])) \ | ||
.filter(**kw.get("filter", {})) \ | ||
.get(**{accessor: args[pk_index]}) | ||
|
||
arg_payload = list(args) | ||
arg_payload[pk_index] = _model | ||
|
||
return callback(*arg_payload, **kwargs) | ||
|
||
except model.DoesNotExist: return ( | ||
{"message": "Query model does not exist"}, | ||
HTTP_404_NOT_FOUND | ||
) | ||
|
||
return inner | ||
|
||
return decorator |
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