| 1 |
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Pattern, TypeVar, Union |
| 2 |
|
| 3 |
from typing_extensions import Annotated |
| 4 |
|
| 5 |
from ninja.params import functions as param_functions |
| 6 |
|
| 7 |
__all__ = [ |
| 8 |
"Body", |
| 9 |
"Cookie", |
| 10 |
"File", |
| 11 |
"Form", |
| 12 |
"Header", |
| 13 |
"Path", |
| 14 |
"Query", |
| 15 |
"BodyEx", |
| 16 |
"CookieEx", |
| 17 |
"FileEx", |
| 18 |
"FormEx", |
| 19 |
"HeaderEx", |
| 20 |
"PathEx", |
| 21 |
"QueryEx", |
| 22 |
"P", |
| 23 |
] |
| 24 |
|
| 25 |
|
| 26 |
class ParamShortcut: |
| 27 |
def __init__(self, base_func: Callable[..., Any]) -> None: |
| 28 |
self._base_func = base_func |
| 29 |
|
| 30 |
def __call__(self, *args: Any, **kwargs: Any) -> Any: |
| 31 |
return self._base_func(*args, **kwargs) |
| 32 |
|
| 33 |
def __getitem__(self, args: Any) -> Any: |
| 34 |
if isinstance(args, tuple): |
| 35 |
return Annotated[args[0], self._base_func(**args[1])] |
| 36 |
return Annotated[args, self._base_func()] |
| 37 |
|
| 38 |
|
| 39 |
if TYPE_CHECKING: |
| 40 |
|
| 41 |
T = TypeVar("T") |
| 42 |
Body = Annotated[T, param_functions.Body()] |
| 43 |
Cookie = Annotated[T, param_functions.Cookie()] |
| 44 |
File = Annotated[T, param_functions.File()] |
| 45 |
Form = Annotated[T, param_functions.Form()] |
| 46 |
Header = Annotated[T, param_functions.Header()] |
| 47 |
Path = Annotated[T, param_functions.Path()] |
| 48 |
Query = Annotated[T, param_functions.Query()] |
| 49 |
|
| 50 |
|
| 51 |
from typing_extensions import Annotated as BodyEx |
| 52 |
from typing_extensions import Annotated as CookieEx |
| 53 |
from typing_extensions import Annotated as FileEx |
| 54 |
from typing_extensions import Annotated as FormEx |
| 55 |
from typing_extensions import Annotated as HeaderEx |
| 56 |
from typing_extensions import Annotated as PathEx |
| 57 |
from typing_extensions import Annotated as QueryEx |
| 58 |
else: |
| 59 |
Body = ParamShortcut(param_functions.Body) |
| 60 |
Cookie = ParamShortcut(param_functions.Cookie) |
| 61 |
File = ParamShortcut(param_functions.File) |
| 62 |
Form = ParamShortcut(param_functions.Form) |
| 63 |
Header = ParamShortcut(param_functions.Header) |
| 64 |
Path = ParamShortcut(param_functions.Path) |
| 65 |
Query = ParamShortcut(param_functions.Query) |
| 66 |
|
| 67 |
|
| 68 |
BodyEx = Body |
| 69 |
CookieEx = Cookie |
| 70 |
FileEx = File |
| 71 |
FormEx = Form |
| 72 |
HeaderEx = Header |
| 73 |
PathEx = Path |
| 74 |
QueryEx = Query |
| 75 |
|
| 76 |
|
| 77 |
def P( |
| 78 |
*, |
| 79 |
alias: Optional[str] = None, |
| 80 |
title: Optional[str] = None, |
| 81 |
description: Optional[str] = None, |
| 82 |
gt: Optional[float] = None, |
| 83 |
ge: Optional[float] = None, |
| 84 |
lt: Optional[float] = None, |
| 85 |
le: Optional[float] = None, |
| 86 |
min_length: Optional[int] = None, |
| 87 |
max_length: Optional[int] = None, |
| 88 |
pattern: Union[str, Pattern[str], None] = None, |
| 89 |
example: Any = None, |
| 90 |
examples: Optional[Dict[str, Any]] = None, |
| 91 |
deprecated: Optional[bool] = None, |
| 92 |
include_in_schema: bool = True, |
| 93 |
**extra: Any, |
| 94 |
) -> Dict[str, Any]: |
| 95 |
"Arguments for BodyEx, QueryEx, etc." |
| 96 |
return dict( |
| 97 |
alias=alias, |
| 98 |
title=title, |
| 99 |
description=description, |
| 100 |
gt=gt, |
| 101 |
ge=ge, |
| 102 |
lt=lt, |
| 103 |
le=le, |
| 104 |
min_length=min_length, |
| 105 |
max_length=max_length, |
| 106 |
pattern=pattern, |
| 107 |
example=example, |
| 108 |
examples=examples, |
| 109 |
deprecated=deprecated, |
| 110 |
include_in_schema=include_in_schema, |
| 111 |
**extra, |
| 112 |
) |