Skip to content

Commit ca717db

Browse files
Improve docs for get params Django API
1 parent 3e14f15 commit ca717db

5 files changed

Lines changed: 66 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ We follow [Semantic Versions](https://semver.org/).
66

77
- Replace [httpx](https://github.com/encode/httpx) with
88
[httpx2](https://github.com/pydantic/httpx2) in docs and tests
9+
- Improve docs for get params Django API
910

1011
## 0.8.0
1112

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ local = [
108108
# A library and CLI app for rendering project templates.
109109
# https://copier.readthedocs.io/en/latest/
110110
"copier",
111+
# Werkzeug is a comprehensive WSGI web application library.
112+
# Required to use runserver_plus
113+
# https://werkzeug.palletsprojects.com/en/stable/
114+
"Werkzeug",
111115
]
112116
linters = [
113117
# Mypy is a static type checker for Python.

saritasa_s3_tools/configs.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ class S3FileTypeConfig(metaclass=S3FileTypeConfigMeta):
3939
expires_in: int = 3600
4040
success_action_status: int = 201
4141
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
42-
content_disposition: (
43-
typing.Literal["attachment"] | typing.Literal["inline"]
44-
) = "attachment"
42+
content_disposition: typing.Literal["attachment", "inline"] = "attachment"
43+
44+
@staticmethod
45+
def get_formatted_s3_configs() -> str:
46+
"""Get formatted S3 configs descriptions for api description."""
47+
formatted_s3_configs: list[str] = []
48+
for name, config in S3FileTypeConfig.configs.items():
49+
allowed_types = (
50+
", ".join(config.allowed) if config.allowed else "All types"
51+
)
52+
content_length_range = (
53+
(
54+
f"{config.content_length_range[0]}-"
55+
f"{config.content_length_range[1]} bytes"
56+
)
57+
if config.content_length_range
58+
else "Any length"
59+
)
60+
formatted_s3_configs.append(
61+
f"`{name}`\n\n"
62+
f"| Parameter | Value |\n"
63+
f"|:---|:---|\n"
64+
f"| Allowed | {allowed_types} |\n"
65+
f"| Content length range | {content_length_range} |\n"
66+
f"| Expires in | {config.expires_in} seconds |\n"
67+
f"| Success action status | {config.success_action_status} |\n"
68+
f"| Content disposition | {config.content_disposition} |",
69+
)
70+
return "\n\n".join(formatted_s3_configs)

saritasa_s3_tools/django/views.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,21 @@ def get_extra_meta_data(
154154
responses=serializers.S3ConfigSerializer(),
155155
),
156156
)(S3GetParamsView)
157+
158+
159+
class _Docs(str):
160+
"""Hack to make dynamic docs for drf-spectacular."""
161+
162+
def expandtabs(
163+
self,
164+
*args: typing.Any,
165+
**kwargs,
166+
) -> str:
167+
return str(
168+
S3GetParamsView.get_params.__doc__
169+
+ "\n\n**Available configs**:\n\n"
170+
+ configs.S3FileTypeConfig.get_formatted_s3_configs(),
171+
).expandtabs(*args, **kwargs)
172+
173+
174+
S3GetParamsView.get_params.__doc__ = _Docs()

uv.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)