drf-to-mkdoc 0.1.6__py3-none-any.whl → 0.1.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of drf-to-mkdoc might be problematic. Click here for more details.

Files changed (31) hide show
  1. drf_to_mkdoc/__init__.py +1 -1
  2. drf_to_mkdoc/apps.py +6 -2
  3. drf_to_mkdoc/conf/defaults.py +2 -1
  4. drf_to_mkdoc/conf/settings.py +11 -5
  5. drf_to_mkdoc/management/commands/build_docs.py +61 -19
  6. drf_to_mkdoc/management/commands/generate_docs.py +5 -5
  7. drf_to_mkdoc/management/commands/generate_model_docs.py +1 -0
  8. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/endpoints/base.css +69 -0
  9. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/endpoints/endpoint-content.css +117 -0
  10. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/endpoints/endpoints-grid.css +119 -0
  11. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/endpoints/responsive.css +57 -50
  12. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/endpoints/theme-toggle.css +12 -0
  13. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/endpoints/variables.css +64 -21
  14. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/models/animations.css +25 -0
  15. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/models/base.css +83 -0
  16. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/models/model-cards.css +126 -0
  17. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/models/model-tables.css +57 -0
  18. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/models/responsive.css +51 -0
  19. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/models/variables.css +46 -0
  20. drf_to_mkdoc/utils/common.py +39 -32
  21. drf_to_mkdoc/utils/{endpoint_generator.py → endpoint_detail_generator.py} +214 -384
  22. drf_to_mkdoc/utils/endpoint_list_generator.py +234 -0
  23. drf_to_mkdoc/utils/extractors/query_parameter_extractors.py +12 -17
  24. drf_to_mkdoc/utils/{model_generator.py → model_detail_generator.py} +20 -51
  25. drf_to_mkdoc/utils/model_list_generator.py +67 -0
  26. {drf_to_mkdoc-0.1.6.dist-info → drf_to_mkdoc-0.1.9.dist-info}/METADATA +3 -25
  27. {drf_to_mkdoc-0.1.6.dist-info → drf_to_mkdoc-0.1.9.dist-info}/RECORD +30 -23
  28. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/extra.css +0 -358
  29. {drf_to_mkdoc-0.1.6.dist-info → drf_to_mkdoc-0.1.9.dist-info}/WHEEL +0 -0
  30. {drf_to_mkdoc-0.1.6.dist-info → drf_to_mkdoc-0.1.9.dist-info}/licenses/LICENSE +0 -0
  31. {drf_to_mkdoc-0.1.6.dist-info → drf_to_mkdoc-0.1.9.dist-info}/top_level.txt +0 -0
@@ -1,19 +1,21 @@
1
- from asyncio.log import logger
2
1
  import importlib
3
- import yaml
4
2
  import json
5
3
  import re
4
+ from asyncio.log import logger
6
5
  from functools import lru_cache
7
6
  from pathlib import Path
8
- from typing import Any, Optional
7
+ from typing import Any
9
8
 
9
+ import yaml
10
10
  from django.apps import apps
11
11
  from django.core.exceptions import AppRegistryNotReady
12
12
  from django.urls import resolve
13
13
  from django.utils.module_loading import import_string
14
14
  from drf_spectacular.generators import SchemaGenerator
15
+
15
16
  from drf_to_mkdoc.conf.settings import drf_to_mkdoc_settings
16
17
 
18
+
17
19
  class SchemaValidationError(Exception):
18
20
  """Custom exception for schema validation errors."""
19
21
 
@@ -37,9 +39,10 @@ def substitute_path_params(path: str, parameters: list[dict[str, Any]]) -> str:
37
39
  django_path = re.sub(r"<path:[^>]+>", "dummy/path", django_path)
38
40
  django_path = re.sub(r"<[^:>]+>", "dummy", django_path) # Catch remaining simple params
39
41
 
40
- return django_path
42
+ return django_path # noqa: RET504
41
43
 
42
- def load_schema() -> Optional[dict[str, Any]]:
44
+
45
+ def load_schema() -> dict[str, Any] | None:
43
46
  """Load the OpenAPI schema from doc-schema.yaml"""
44
47
  schema_file = Path(drf_to_mkdoc_settings.CONFIG_DIR) / "doc-schema.yaml"
45
48
  if not schema_file.exists():
@@ -49,7 +52,7 @@ def load_schema() -> Optional[dict[str, Any]]:
49
52
  return yaml.safe_load(f)
50
53
 
51
54
 
52
- def load_model_json_data() -> Optional[dict[str, Any]]:
55
+ def load_model_json_data() -> dict[str, Any] | None:
53
56
  """Load the JSON mapping data for model information"""
54
57
  json_file = Path(drf_to_mkdoc_settings.MODEL_DOCS_FILE)
55
58
  if not json_file.exists():
@@ -59,7 +62,7 @@ def load_model_json_data() -> Optional[dict[str, Any]]:
59
62
  return json.load(f)
60
63
 
61
64
 
62
- def load_doc_config() -> Optional[dict[str, Any]]:
65
+ def load_doc_config() -> dict[str, Any] | None:
63
66
  """Load the documentation configuration file"""
64
67
  config_file = Path(drf_to_mkdoc_settings.DOC_CONFIG_FILE)
65
68
  if not config_file.exists():
@@ -69,7 +72,7 @@ def load_doc_config() -> Optional[dict[str, Any]]:
69
72
  return json.load(f)
70
73
 
71
74
 
72
- def get_model_docstring(class_name: str) -> Optional[str]:
75
+ def get_model_docstring(class_name: str) -> str | None:
73
76
  """Extract docstring from Django model class"""
74
77
  try:
75
78
  # Check if Django is properly initialized
@@ -167,51 +170,53 @@ def get_custom_schema():
167
170
  raise QueryParamTypeError("Invalid queryparam_type")
168
171
  return data
169
172
 
173
+
170
174
  def convert_to_django_path(path: str, parameters: list[dict[str, Any]]) -> str:
171
175
  """
172
176
  Convert a path with {param} to a Django-style path with <type:param>.
173
- If PATH_PARAM_SUBSTITUTOR is set, use that function instead.
177
+ If PATH_PARAM_SUBSTITUTE_FUNCTION is set, use that function instead.
174
178
  """
175
179
  function = None
176
- func_path = getattr(drf_to_mkdoc_settings, "PATH_PARAM_SUBSTITUTOR", None)
180
+ func_path = drf_to_mkdoc_settings.PATH_PARAM_SUBSTITUTE_FUNCTION
177
181
 
178
182
  if func_path:
179
183
  try:
180
184
  function = import_string(func_path)
181
185
  except ImportError:
182
- logger.warning("PATH_PARAM_SUBSTITUTOR is not a valid import path")
186
+ logger.warning("PATH_PARAM_SUBSTITUTE_FUNCTION is not a valid import path")
183
187
 
184
188
  # If custom function exists and returns a valid value, use it
189
+ PATH_PARAM_SUBSTITUTE_MAPPING = drf_to_mkdoc_settings.PATH_PARAM_SUBSTITUTE_MAPPING
185
190
  if callable(function):
186
191
  try:
187
- value = function(path, parameters)
188
- if isinstance(value, str) and value.strip():
189
- return value
192
+ result = function(path, parameters)
193
+ if result and isinstance(result, dict):
194
+ PATH_PARAM_SUBSTITUTE_MAPPING.update(result)
190
195
  except Exception as e:
191
196
  logger.exception("Error in custom path substitutor: %s", e)
192
197
 
193
198
  # Default Django path conversion
194
199
  def replacement(match):
195
200
  param_name = match.group(1)
196
- param_info = next((p for p in parameters if p.get('name') == param_name), {})
197
- param_type = param_info.get('schema', {}).get('type')
198
- param_format = param_info.get('schema', {}).get('format')
199
-
200
- if param_type == 'integer':
201
- converter = 'int'
202
- elif param_type == 'string' and param_format == 'uuid':
203
- converter = 'uuid'
201
+ custom_param_type = PATH_PARAM_SUBSTITUTE_MAPPING.get(param_name)
202
+ if custom_param_type and custom_param_type in ("int", "uuid", "str"):
203
+ converter = custom_param_type
204
204
  else:
205
- converter = 'str'
206
-
207
- return f'<{converter}:{param_name}>'
205
+ param_info = next((p for p in parameters if p.get("name") == param_name), {})
206
+ param_type = param_info.get("schema", {}).get("type")
207
+ param_format = param_info.get("schema", {}).get("format")
208
+
209
+ if param_type == "integer":
210
+ converter = "int"
211
+ elif param_type == "string" and param_format == "uuid":
212
+ converter = "uuid"
213
+ else:
214
+ converter = "str"
208
215
 
209
- django_path = re.sub(r'{(\w+)}', replacement, path)
216
+ return f"<{converter}:{param_name}>"
210
217
 
211
- if not django_path.endswith('/'):
212
- django_path += '/'
218
+ return re.sub(r"{(\w+)}", replacement, path)
213
219
 
214
- return django_path
215
220
 
216
221
  @lru_cache
217
222
  def get_schema():
@@ -276,8 +281,8 @@ def extract_viewset_from_operation_id(operation_id: str):
276
281
  if not path:
277
282
  raise ValueError(f"Path not found for operation ID: {operation_id}")
278
283
 
284
+ resolved_path = substitute_path_params(path, parameters)
279
285
  try:
280
- resolved_path = substitute_path_params(path, parameters)
281
286
  match = resolve(resolved_path)
282
287
  view_func = match.func
283
288
  if hasattr(view_func, "view_class"):
@@ -291,8 +296,10 @@ def extract_viewset_from_operation_id(operation_id: str):
291
296
  else:
292
297
  return view_func
293
298
 
294
- except Exception as e:
295
- logger.error(f"Failed to resolve path {path}")
299
+ except Exception:
300
+ logger.error(
301
+ f"Failed to resolve path.\nschema_path{path}\ntried_path={resolved_path}\n---"
302
+ )
296
303
 
297
304
 
298
305
  def extract_viewset_name_from_operation_id(operation_id: str):