drf-to-mkdoc 0.1.9__py3-none-any.whl → 0.2.1__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 (35) hide show
  1. drf_to_mkdoc/conf/defaults.py +5 -0
  2. drf_to_mkdoc/conf/settings.py +123 -9
  3. drf_to_mkdoc/management/commands/build_docs.py +8 -7
  4. drf_to_mkdoc/management/commands/build_endpoint_docs.py +69 -0
  5. drf_to_mkdoc/management/commands/build_model_docs.py +50 -0
  6. drf_to_mkdoc/management/commands/{generate_model_docs.py → extract_model_data.py} +18 -24
  7. drf_to_mkdoc/static/drf-to-mkdoc/javascripts/try-out-sidebar.js +879 -0
  8. drf_to_mkdoc/static/drf-to-mkdoc/stylesheets/endpoints/try-out-sidebar.css +728 -0
  9. drf_to_mkdoc/utils/ai_tools/__init__.py +0 -0
  10. drf_to_mkdoc/utils/ai_tools/enums.py +13 -0
  11. drf_to_mkdoc/utils/ai_tools/exceptions.py +19 -0
  12. drf_to_mkdoc/utils/ai_tools/providers/__init__.py +0 -0
  13. drf_to_mkdoc/utils/ai_tools/providers/base_provider.py +123 -0
  14. drf_to_mkdoc/utils/ai_tools/providers/gemini_provider.py +80 -0
  15. drf_to_mkdoc/utils/ai_tools/types.py +81 -0
  16. drf_to_mkdoc/utils/commons/__init__.py +0 -0
  17. drf_to_mkdoc/utils/commons/code_extractor.py +22 -0
  18. drf_to_mkdoc/utils/commons/file_utils.py +35 -0
  19. drf_to_mkdoc/utils/commons/model_utils.py +83 -0
  20. drf_to_mkdoc/utils/commons/operation_utils.py +83 -0
  21. drf_to_mkdoc/utils/commons/path_utils.py +78 -0
  22. drf_to_mkdoc/utils/commons/schema_utils.py +230 -0
  23. drf_to_mkdoc/utils/endpoint_detail_generator.py +16 -35
  24. drf_to_mkdoc/utils/endpoint_list_generator.py +1 -1
  25. drf_to_mkdoc/utils/extractors/query_parameter_extractors.py +33 -30
  26. drf_to_mkdoc/utils/model_detail_generator.py +44 -40
  27. drf_to_mkdoc/utils/model_list_generator.py +25 -15
  28. drf_to_mkdoc/utils/schema.py +259 -0
  29. {drf_to_mkdoc-0.1.9.dist-info → drf_to_mkdoc-0.2.1.dist-info}/METADATA +16 -5
  30. {drf_to_mkdoc-0.1.9.dist-info → drf_to_mkdoc-0.2.1.dist-info}/RECORD +33 -16
  31. drf_to_mkdoc/management/commands/generate_docs.py +0 -138
  32. drf_to_mkdoc/utils/common.py +0 -353
  33. {drf_to_mkdoc-0.1.9.dist-info → drf_to_mkdoc-0.2.1.dist-info}/WHEEL +0 -0
  34. {drf_to_mkdoc-0.1.9.dist-info → drf_to_mkdoc-0.2.1.dist-info}/licenses/LICENSE +0 -0
  35. {drf_to_mkdoc-0.1.9.dist-info → drf_to_mkdoc-0.2.1.dist-info}/top_level.txt +0 -0
@@ -7,6 +7,11 @@ DEFAULTS = {
7
7
  "CUSTOM_SCHEMA_FILE": "docs/configs/custom_schema.json", # Path to custom schema file
8
8
  "PATH_PARAM_SUBSTITUTE_FUNCTION": None,
9
9
  "PATH_PARAM_SUBSTITUTE_MAPPING": {},
10
+ # AI documentation settings
11
+ "ENABLE_AI_DOCS": False,
12
+ "AI_CONFIG_DIR_NAME": "ai_code", # Directory name for AI-generated code files
13
+ "AI_OPERATION_MAP_FILE": "docs/configs/operation_map.json", # Path to operation map file
14
+ "SERIALIZERS_INHERITANCE_DEPTH": 1, # Maximum depth for class inheritance analysis
10
15
  # Django apps - required, no default
11
16
  "DJANGO_APPS": None, # List of Django app names to process
12
17
  }
@@ -1,31 +1,145 @@
1
+ from pathlib import Path
2
+ from typing import Any, ClassVar
3
+
1
4
  from django.conf import settings
2
5
 
3
6
  from drf_to_mkdoc.conf.defaults import DEFAULTS
4
7
 
5
8
 
6
9
  class DRFToMkDocSettings:
7
- required_settings = ["DJANGO_APPS"]
8
- project_settings = {"PROJECT_NAME": "drf-to-mkdoc"}
10
+ required_settings: ClassVar[list[str]] = ["DJANGO_APPS"]
11
+ project_settings: ClassVar[dict[str, Any]] = {"PROJECT_NAME": "drf-to-mkdoc"}
12
+
13
+ settings_types: ClassVar[dict[str, type]] = {
14
+ "ENABLE_AI_DOCS": bool,
15
+ "AI_CONFIG_DIR_NAME": str,
16
+ "SERIALIZERS_INHERITANCE_DEPTH": int,
17
+ }
18
+
19
+ settings_ranges: ClassVar[dict[str, tuple[int, int]]] = {
20
+ "SERIALIZERS_INHERITANCE_DEPTH": (1, 3),
21
+ }
22
+
23
+ path_settings = {
24
+ "CONFIG_DIR",
25
+ "MODEL_DOCS_FILE",
26
+ "DOC_CONFIG_FILE",
27
+ "CUSTOM_SCHEMA_FILE",
28
+ "AI_OPERATION_MAP_FILE",
29
+ }
9
30
 
10
31
  def __init__(self, user_settings_key="DRF_TO_MKDOC", defaults=None):
11
32
  self.user_settings_key = user_settings_key
12
33
  self._user_settings = getattr(settings, user_settings_key, {})
13
34
  self.defaults = defaults or {}
14
35
 
15
- def get(self, key):
16
- if key not in self.defaults:
17
- if key in self.project_settings:
18
- return self.project_settings[key]
19
- raise AttributeError(f"Invalid DRF_TO_MKDOC setting: '{key}'")
36
+ def _validate_type(self, key: str, value: Any) -> None:
37
+ """Validate the type of setting value."""
38
+ if key in self.settings_types:
39
+ expected_type = self.settings_types[key]
40
+ if not isinstance(value, expected_type):
41
+ raise TypeError(
42
+ f"DRF_TO_MKDOC setting '{key}' must be of type {expected_type.__name__}, "
43
+ f"got {type(value).__name__} instead."
44
+ )
20
45
 
21
- value = self._user_settings.get(key, self.defaults[key])
46
+ def _validate_range(self, key: str, value: Any) -> None:
47
+ """Validate the range of a setting value."""
48
+ if key in self.settings_ranges:
49
+ min_val, max_val = self.settings_ranges[key]
50
+ if not min_val <= value <= max_val:
51
+ raise ValueError(
52
+ f"DRF_TO_MKDOC setting '{key}' must be between {min_val} and {max_val}, "
53
+ f"got {value} instead."
54
+ )
22
55
 
56
+ def _validate_required(self, key: str, value: Any) -> None:
57
+ """Validate if a required setting is configured."""
23
58
  if value is None and key in self.required_settings:
24
59
  raise ValueError(
25
60
  f"DRF_TO_MKDOC setting '{key}' is required but not configured. "
26
61
  f"Please add it to your Django settings under {self.user_settings_key}."
27
62
  )
28
63
 
64
+ def _validate_dir(self, key: str, value: str) -> None:
65
+ if key not in self.path_settings or not isinstance(value, str):
66
+ return
67
+
68
+ if not value.strip():
69
+ raise ValueError(
70
+ f"DRF_TO_MKDOC path setting '{key}' cannot be empty or contain only whitespace."
71
+ )
72
+
73
+ dangerous_components = {"..", "~", "/", "\\"}
74
+ path_parts = Path(value).parts
75
+ for part in path_parts:
76
+ if part in dangerous_components or part.startswith("."):
77
+ raise ValueError(
78
+ f"DRF_TO_MKDOC path setting '{key}' contains unsafe path component '{part}'. "
79
+ f"Directory names should be simple names without separators or relative path components."
80
+ )
81
+
82
+ if Path(value).is_absolute():
83
+ raise ValueError(
84
+ f"DRF_TO_MKDOC path setting '{key}' cannot be an absolute path. "
85
+ f"Use relative directory names only."
86
+ )
87
+
88
+ reserved_names = {
89
+ "CON",
90
+ "PRN",
91
+ "AUX",
92
+ "NUL",
93
+ "COM1",
94
+ "COM2",
95
+ "COM3",
96
+ "COM4",
97
+ "COM5",
98
+ "COM6",
99
+ "COM7",
100
+ "COM8",
101
+ "COM9",
102
+ "LPT1",
103
+ "LPT2",
104
+ "LPT3",
105
+ "LPT4",
106
+ "LPT5",
107
+ "LPT6",
108
+ "LPT7",
109
+ "LPT8",
110
+ "LPT9",
111
+ }
112
+ if value.upper() in reserved_names:
113
+ raise ValueError(
114
+ f"DRF_TO_MKDOC path setting '{key}' uses a reserved system name '{value}'. "
115
+ f"Please choose a different name."
116
+ )
117
+
118
+ invalid_chars = '<>:"|?*'
119
+ if any(char in value for char in invalid_chars):
120
+ raise ValueError(
121
+ f"DRF_TO_MKDOC path setting '{key}' contains invalid characters. "
122
+ f"Avoid using: {invalid_chars}"
123
+ )
124
+
125
+ def get(self, key):
126
+ if key in self.project_settings:
127
+ return self.project_settings[key]
128
+
129
+ # User-provided settings take precedence
130
+ if key in self._user_settings:
131
+ value = self._user_settings[key]
132
+ else:
133
+ value = self.defaults.get(key, None)
134
+
135
+ # Run all validations
136
+ self._validate_required(key, value)
137
+ self._validate_type(key, value)
138
+ self._validate_range(key, value)
139
+ self._validate_dir(key, value)
140
+
141
+ if value is None:
142
+ raise AttributeError(f"Invalid DRF_TO_MKDOC setting: '{key}'")
29
143
  return value
30
144
 
31
145
  def __getattr__(self, key):
@@ -37,7 +151,7 @@ class DRFToMkDocSettings:
37
151
  for setting in self.required_settings:
38
152
  try:
39
153
  self.get(setting)
40
- except ValueError:
154
+ except (ValueError, AttributeError):
41
155
  missing_settings.append(setting)
42
156
 
43
157
  if missing_settings:
@@ -34,15 +34,16 @@ class Command(BaseCommand):
34
34
  )
35
35
 
36
36
  try:
37
- # Generate the model documentation JSON first
38
- self.stdout.write("Generating model documentation...")
39
- call_command("generate_model_docs", "--pretty")
40
- self.stdout.write(self.style.SUCCESS("Model documentation generated."))
37
+ # Extract model data from Django models
38
+ self.stdout.write("Extracting model data...")
39
+ call_command("extract_model_data", "--pretty")
40
+ self.stdout.write(self.style.SUCCESS("Model data extracted."))
41
41
 
42
42
  # Generate the documentation content
43
- self.stdout.write("Generating documentation content...")
44
- call_command("generate_docs")
45
- self.stdout.write(self.style.SUCCESS("Documentation content generated."))
43
+ self.stdout.write("Building documentation content...")
44
+ call_command("build_model_docs")
45
+ call_command("build_endpoint_docs")
46
+ self.stdout.write(self.style.SUCCESS("Documentation content built."))
46
47
 
47
48
  # Build the MkDocs site
48
49
  self.stdout.write("Building MkDocs site...")
@@ -0,0 +1,69 @@
1
+ from pathlib import Path
2
+
3
+ from django.core.management.base import BaseCommand
4
+
5
+ from drf_to_mkdoc.conf.settings import drf_to_mkdoc_settings
6
+ from drf_to_mkdoc.utils.commons.schema_utils import get_schema
7
+ from drf_to_mkdoc.utils.endpoint_detail_generator import (
8
+ generate_endpoint_files,
9
+ parse_endpoints_from_schema,
10
+ )
11
+ from drf_to_mkdoc.utils.endpoint_list_generator import create_endpoints_index
12
+
13
+
14
+ class Command(BaseCommand):
15
+ help = "Build endpoint documentation from OpenAPI schema"
16
+
17
+ def handle(self, *args, **options):
18
+ self.stdout.write(
19
+ self.style.SUCCESS("🚀 Starting endpoint documentation generation...")
20
+ )
21
+
22
+ docs_dir = self._setup_docs_directory()
23
+ schema_data = self._load_schema_data()
24
+
25
+ if schema_data:
26
+ self._generate_endpoints_documentation(schema_data, docs_dir)
27
+ self.stdout.write(
28
+ self.style.SUCCESS("✅ Endpoint documentation generation complete!")
29
+ )
30
+ else:
31
+ self.stdout.write(self.style.ERROR("❌ Failed to generate endpoint documentation."))
32
+
33
+ def _setup_docs_directory(self):
34
+ docs_dir = Path(drf_to_mkdoc_settings.DOCS_DIR)
35
+ docs_dir.mkdir(parents=True, exist_ok=True)
36
+ return docs_dir
37
+
38
+ def _load_schema_data(self):
39
+ try:
40
+ schema = get_schema()
41
+ except Exception as e:
42
+ self.stdout.write(self.style.ERROR(f"❌ Failed to load OpenAPI schema: {e}"))
43
+ return {}
44
+ if not schema:
45
+ self.stdout.write(self.style.ERROR("❌ Failed to load OpenAPI schema"))
46
+ return {}
47
+
48
+ paths = schema.get("paths", {})
49
+ components = schema.get("components", {})
50
+
51
+ self.stdout.write(f"📊 Loaded {len(paths)} API paths")
52
+
53
+ return {"paths": paths, "components": components}
54
+
55
+ def _generate_endpoints_documentation(self, schema_data, docs_dir):
56
+ self.stdout.write("🔗 Generating endpoint documentation...")
57
+
58
+ paths = schema_data["paths"]
59
+ components = schema_data["components"]
60
+
61
+ endpoints_by_app = parse_endpoints_from_schema(paths)
62
+ total_endpoints = generate_endpoint_files(endpoints_by_app, components)
63
+ create_endpoints_index(endpoints_by_app, docs_dir)
64
+
65
+ self.stdout.write(
66
+ self.style.SUCCESS(
67
+ f"✅ Generated {total_endpoints} endpoint files with Django view introspection"
68
+ )
69
+ )
@@ -0,0 +1,50 @@
1
+ from pathlib import Path
2
+
3
+ from django.core.management.base import BaseCommand
4
+
5
+ from drf_to_mkdoc.conf.settings import drf_to_mkdoc_settings
6
+ from drf_to_mkdoc.utils.commons.file_utils import load_json_data
7
+ from drf_to_mkdoc.utils.model_detail_generator import generate_model_docs
8
+ from drf_to_mkdoc.utils.model_list_generator import create_models_index
9
+
10
+
11
+ class Command(BaseCommand):
12
+ help = "Build model documentation from model JSON data"
13
+
14
+ def handle(self, *args, **options):
15
+ self.stdout.write(self.style.SUCCESS("🚀 Starting model documentation generation..."))
16
+
17
+ docs_dir = self._setup_docs_directory()
18
+ models_data = self._load_models_data()
19
+
20
+ if models_data:
21
+ self._generate_models_documentation(models_data, docs_dir)
22
+ self.stdout.write(self.style.SUCCESS("✅ Model documentation generation complete!"))
23
+ else:
24
+ self.stdout.write(self.style.ERROR("❌ Failed to generate model documentation."))
25
+
26
+ def _setup_docs_directory(self):
27
+ docs_dir = Path(drf_to_mkdoc_settings.DOCS_DIR)
28
+ docs_dir.mkdir(parents=True, exist_ok=True)
29
+ return docs_dir
30
+
31
+ def _load_models_data(self):
32
+ models_data = load_json_data(
33
+ drf_to_mkdoc_settings.MODEL_DOCS_FILE, raise_not_found=False
34
+ )
35
+
36
+ if not models_data:
37
+ self.stdout.write(self.style.WARNING("⚠️ No model data found"))
38
+
39
+ return models_data
40
+
41
+ def _generate_models_documentation(self, models_data, docs_dir):
42
+ self.stdout.write("📋 Generating model documentation...")
43
+
44
+ try:
45
+ generate_model_docs(models_data)
46
+ create_models_index(models_data, docs_dir)
47
+ self.stdout.write(self.style.SUCCESS("✅ Model documentation generated"))
48
+ except Exception as e:
49
+ self.stdout.write(self.style.WARNING(f"⚠️ Failed to generate model docs: {e}"))
50
+ raise
@@ -1,6 +1,7 @@
1
1
  import inspect
2
2
  import json
3
3
  import re
4
+ from collections import defaultdict
4
5
  from pathlib import Path
5
6
 
6
7
  from django.apps import apps
@@ -11,7 +12,7 @@ from drf_to_mkdoc.conf.settings import drf_to_mkdoc_settings
11
12
 
12
13
 
13
14
  class Command(BaseCommand):
14
- help = "Generate model documentation JSON from Django model introspection"
15
+ help = "Extract model data from Django model introspection and save as JSON"
15
16
 
16
17
  def add_arguments(self, parser):
17
18
  parser.add_argument(
@@ -38,33 +39,26 @@ class Command(BaseCommand):
38
39
 
39
40
  model_docs = self.generate_model_documentation(exclude_apps)
40
41
 
41
- json_data = {
42
- "models": model_docs,
43
- "stats": {
44
- "total_models": len(model_docs),
45
- "total_apps": len({model["app_label"] for model in model_docs.values()}),
46
- },
47
- }
48
-
49
42
  output_path = Path(output_file)
50
43
  output_path.parent.mkdir(parents=True, exist_ok=True)
51
44
  with output_path.open("w", encoding="utf-8") as f:
45
+ payload = dict(model_docs)
52
46
  if pretty:
53
- json.dump(json_data, f, indent=2, ensure_ascii=False, default=str)
47
+ json.dump(payload, f, ensure_ascii=False, sort_keys=True, default=str, indent=2)
54
48
  else:
55
- json.dump(json_data, f, ensure_ascii=False, default=str)
49
+ json.dump(payload, f, ensure_ascii=False, sort_keys=True, default=str)
56
50
 
57
51
  self.stdout.write(
58
52
  self.style.SUCCESS(f"✅ Generated model documentation: {output_path.absolute()}")
59
53
  )
60
- self.stdout.write(f"📊 Total models: {len(model_docs)}")
61
54
  self.stdout.write(
62
- f"📦 Total apps: {len({model['app_label'] for model in model_docs.values()})}"
55
+ f"📊 Total models: {sum([len(model_docs[app_label]) for app_label in model_docs])}"
63
56
  )
57
+ self.stdout.write(f"📦 Total apps: {len(model_docs)}")
64
58
 
65
59
  def generate_model_documentation(self, exclude_apps):
66
60
  """Generate documentation for all Django models"""
67
- model_docs = {}
61
+ model_docs = defaultdict(dict)
68
62
 
69
63
  for app_config in apps.get_app_configs():
70
64
  app_label = app_config.label
@@ -78,13 +72,11 @@ class Command(BaseCommand):
78
72
 
79
73
  for model in app_config.get_models():
80
74
  model_name = model.__name__
81
- model_key = f"{app_label}.{model_name}"
82
-
83
75
  self.stdout.write(f" 📋 Processing model: {model_name}")
84
76
 
85
- model_docs[model_key] = self.introspect_model(model, app_label)
77
+ model_docs[app_label][model_name] = self.introspect_model(model, app_label)
86
78
 
87
- return model_docs
79
+ return {app_label: dict(models) for app_label, models in model_docs.items()}
88
80
 
89
81
  def introspect_model(self, model, app_label):
90
82
  """Introspect a single Django model"""
@@ -100,7 +92,7 @@ class Command(BaseCommand):
100
92
  "description": self.get_model_description(model),
101
93
  "abstract": meta.abstract,
102
94
  "proxy": meta.proxy,
103
- "fields": {},
95
+ "column_fields": {},
104
96
  "relationships": {},
105
97
  "meta_options": self.get_meta_options(meta),
106
98
  "methods": self.get_model_methods(model),
@@ -111,10 +103,9 @@ class Command(BaseCommand):
111
103
  if field.many_to_many or field.one_to_many or field.many_to_one or field.one_to_one:
112
104
  # Handle relationships separately
113
105
  model_doc["relationships"][field.name] = self.introspect_relationship(field)
114
- else:
115
- # Handle regular fields
116
- model_doc["fields"][field.name] = self.introspect_field(field)
117
-
106
+ if not (field.one_to_many or field.many_to_many):
107
+ # Handle column fields
108
+ model_doc["column_fields"][field.name] = self.introspect_field(field)
118
109
  return model_doc
119
110
 
120
111
  def introspect_field(self, field):
@@ -155,6 +146,9 @@ class Command(BaseCommand):
155
146
  "type": field.__class__.__name__,
156
147
  "related_model": related_model_label,
157
148
  "related_name": getattr(field, "related_name", None),
149
+ "app_label": field.related_model._meta.app_label,
150
+ "table_name": field.related_model._meta.db_table,
151
+ "verbose_name": field.related_model._meta.verbose_name,
158
152
  "on_delete": self.get_on_delete_name(field),
159
153
  "null": getattr(field, "null", False),
160
154
  "blank": getattr(field, "blank", False),
@@ -287,7 +281,7 @@ class Command(BaseCommand):
287
281
  if display_method_match:
288
282
  field_name = display_method_match.group(1)
289
283
  if field_name in model_field_names:
290
- # Exclude if the field doesn't exist in the model
284
+ # Exclude built-in get_<field>_display for fields present on the model
291
285
  continue
292
286
 
293
287
  method = getattr(model, attr_name)