Skip to content

Commit 80abece

Browse files
Improve docs for get params Django API
1 parent d2aef4b commit 80abece

3 files changed

Lines changed: 22 additions & 45 deletions

File tree

saritasa_s3_tools/configs.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,23 @@ class S3FileTypeConfig(metaclass=S3FileTypeConfigMeta):
4141
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
4242
content_disposition: typing.Literal["attachment", "inline"] = "attachment"
4343

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"
44+
def get_short_description(self) -> str:
45+
"""Get short description for config."""
46+
allowed_types = (
47+
", ".join(self.allowed) if self.allowed else "All types"
48+
)
49+
content_length_range = (
50+
(
51+
f"{self.content_length_range[0]}-"
52+
f"{self.content_length_range[1]} bytes"
5153
)
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)
54+
if self.content_length_range
55+
else "Any length"
56+
)
57+
return (
58+
f"*Allowed types*: {allowed_types}, "
59+
f"*Content length range*: {content_length_range}, "
60+
f"*Expires in*: {self.expires_in} seconds, "
61+
f"*Success action status*: {self.success_action_status}, "
62+
f"*Content disposition*: {self.content_disposition}"
63+
)

saritasa_s3_tools/django/drf_fields.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ def _get_choices(self) -> dict[str, str]:
2525
current_choices = tuple(
2626
(
2727
config_name,
28-
config_name,
28+
configs.S3FileTypeConfig.configs[
29+
config_name
30+
].get_short_description(),
2931
)
3032
for config_name in configs.S3FileTypeConfig.configs
3133
)

saritasa_s3_tools/django/views.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,3 @@ 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-
self
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(S3GetParamsView.get_params.__doc__)

0 commit comments

Comments
 (0)