lxcor-docca 0.1.0__tar.gz
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.
- lxcor_docca-0.1.0/LICENSE +21 -0
- lxcor_docca-0.1.0/PKG-INFO +91 -0
- lxcor_docca-0.1.0/README.md +67 -0
- lxcor_docca-0.1.0/docca/__init__.py +0 -0
- lxcor_docca-0.1.0/docca/admin/__init__.py +5 -0
- lxcor_docca-0.1.0/docca/admin/doc_endpoint.py +47 -0
- lxcor_docca-0.1.0/docca/admin/doc_parameter_def.py +13 -0
- lxcor_docca-0.1.0/docca/admin/doc_response_field.py +12 -0
- lxcor_docca-0.1.0/docca/admin/doc_tag.py +12 -0
- lxcor_docca-0.1.0/docca/apps.py +5 -0
- lxcor_docca-0.1.0/docca/management/__init__.py +0 -0
- lxcor_docca-0.1.0/docca/management/commands/__init__.py +0 -0
- lxcor_docca-0.1.0/docca/management/commands/dumpdocs.py +119 -0
- lxcor_docca-0.1.0/docca/management/commands/loaddocs.py +190 -0
- lxcor_docca-0.1.0/docca/management/commands/syncdocs.py +473 -0
- lxcor_docca-0.1.0/docca/migrations/0001_initial.py +65 -0
- lxcor_docca-0.1.0/docca/migrations/0002_add_overview.py +18 -0
- lxcor_docca-0.1.0/docca/migrations/0003_add_parameter_def.py +84 -0
- lxcor_docca-0.1.0/docca/migrations/0004_add_response_field.py +56 -0
- lxcor_docca-0.1.0/docca/migrations/0005_add_slug.py +52 -0
- lxcor_docca-0.1.0/docca/migrations/__init__.py +0 -0
- lxcor_docca-0.1.0/docca/models/__init__.py +7 -0
- lxcor_docca-0.1.0/docca/models/doc_endpoint.py +101 -0
- lxcor_docca-0.1.0/docca/models/doc_parameter.py +65 -0
- lxcor_docca-0.1.0/docca/models/doc_parameter_def.py +48 -0
- lxcor_docca-0.1.0/docca/models/doc_response_field.py +67 -0
- lxcor_docca-0.1.0/docca/models/doc_tag.py +31 -0
- lxcor_docca-0.1.0/docca/templatetags/__init__.py +0 -0
- lxcor_docca-0.1.0/docca/templatetags/docca_tags.py +141 -0
- lxcor_docca-0.1.0/docca/urls.py +11 -0
- lxcor_docca-0.1.0/docca/views.py +172 -0
- lxcor_docca-0.1.0/lxcor_docca.egg-info/PKG-INFO +91 -0
- lxcor_docca-0.1.0/lxcor_docca.egg-info/SOURCES.txt +39 -0
- lxcor_docca-0.1.0/lxcor_docca.egg-info/dependency_links.txt +1 -0
- lxcor_docca-0.1.0/lxcor_docca.egg-info/requires.txt +2 -0
- lxcor_docca-0.1.0/lxcor_docca.egg-info/top_level.txt +1 -0
- lxcor_docca-0.1.0/pyproject.toml +40 -0
- lxcor_docca-0.1.0/setup.cfg +4 -0
- lxcor_docca-0.1.0/tests/test_portal.py +99 -0
- lxcor_docca-0.1.0/tests/test_slug.py +54 -0
- lxcor_docca-0.1.0/tests/test_syncdocs.py +101 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 lxcor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: lxcor-docca
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: API documentation portal for Django REST Framework — auto-syncs endpoints, renders a searchable portal with tags, parameters, and response fields.
|
|
5
|
+
Author-email: Alexandre Scozzafave Alves <frontdesk@lxcor.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/lxcor/api-suite/tree/main/docca
|
|
8
|
+
Project-URL: Repository, https://github.com/lxcor/api-suite
|
|
9
|
+
Keywords: django,drf,api,documentation,portal
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Framework :: Django
|
|
12
|
+
Classifier: Framework :: Django :: 3.2
|
|
13
|
+
Classifier: Framework :: Django :: 4.2
|
|
14
|
+
Classifier: Framework :: Django :: 5.0
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
19
|
+
Requires-Python: >=3.8
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: django>=3.2
|
|
23
|
+
Requires-Dist: djangorestframework>=3.14
|
|
24
|
+
|
|
25
|
+
# docca
|
|
26
|
+
|
|
27
|
+
API documentation portal for Django REST Framework.
|
|
28
|
+
|
|
29
|
+
`docca` auto-discovers your DRF viewsets and API views, syncs them into a database-backed manifest, and renders a searchable portal with tags, parameters, and response fields — no YAML, no decorators, no separate spec file.
|
|
30
|
+
|
|
31
|
+
Part of the [lxcor/api-suite](https://github.com/lxcor/api-suite).
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install lxcor-docca
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Setup
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
# settings.py
|
|
43
|
+
INSTALLED_APPS = [
|
|
44
|
+
...
|
|
45
|
+
'docca',
|
|
46
|
+
]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
# urls.py
|
|
51
|
+
from django.urls import path, include
|
|
52
|
+
|
|
53
|
+
urlpatterns = [
|
|
54
|
+
path('docs/', include('docca.urls')),
|
|
55
|
+
]
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Annotate your views:
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
class MySunriseViewSet(viewsets.ViewSet):
|
|
62
|
+
docca_tag = 'Solar'
|
|
63
|
+
docca_overview = 'Returns sunrise and sunset times for a given location and date.'
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Sync endpoints into the database:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
python manage.py syncdocs
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Visit `/docs/` to see the portal.
|
|
73
|
+
|
|
74
|
+
## Settings
|
|
75
|
+
|
|
76
|
+
| Setting | Default | Description |
|
|
77
|
+
|---|---|---|
|
|
78
|
+
| `DOCCA_APP_COLORS` | `{}` | Bootstrap color per app label (`'warning'`, `'danger'`, etc.) |
|
|
79
|
+
| `DOCCA_APP_DESCRIPTIONS` | `{}` | Product-facing description per app, shown on the portal |
|
|
80
|
+
|
|
81
|
+
## Management commands
|
|
82
|
+
|
|
83
|
+
| Command | Description |
|
|
84
|
+
|---|---|
|
|
85
|
+
| `syncdocs` | Discover and sync all DRF endpoints into the database |
|
|
86
|
+
| `dumpdocs` | Export endpoint overviews to a JSON fixture |
|
|
87
|
+
| `loaddocs` | Import endpoint overviews from a JSON fixture |
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# docca
|
|
2
|
+
|
|
3
|
+
API documentation portal for Django REST Framework.
|
|
4
|
+
|
|
5
|
+
`docca` auto-discovers your DRF viewsets and API views, syncs them into a database-backed manifest, and renders a searchable portal with tags, parameters, and response fields — no YAML, no decorators, no separate spec file.
|
|
6
|
+
|
|
7
|
+
Part of the [lxcor/api-suite](https://github.com/lxcor/api-suite).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install lxcor-docca
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Setup
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
# settings.py
|
|
19
|
+
INSTALLED_APPS = [
|
|
20
|
+
...
|
|
21
|
+
'docca',
|
|
22
|
+
]
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
# urls.py
|
|
27
|
+
from django.urls import path, include
|
|
28
|
+
|
|
29
|
+
urlpatterns = [
|
|
30
|
+
path('docs/', include('docca.urls')),
|
|
31
|
+
]
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Annotate your views:
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
class MySunriseViewSet(viewsets.ViewSet):
|
|
38
|
+
docca_tag = 'Solar'
|
|
39
|
+
docca_overview = 'Returns sunrise and sunset times for a given location and date.'
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Sync endpoints into the database:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
python manage.py syncdocs
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Visit `/docs/` to see the portal.
|
|
49
|
+
|
|
50
|
+
## Settings
|
|
51
|
+
|
|
52
|
+
| Setting | Default | Description |
|
|
53
|
+
|---|---|---|
|
|
54
|
+
| `DOCCA_APP_COLORS` | `{}` | Bootstrap color per app label (`'warning'`, `'danger'`, etc.) |
|
|
55
|
+
| `DOCCA_APP_DESCRIPTIONS` | `{}` | Product-facing description per app, shown on the portal |
|
|
56
|
+
|
|
57
|
+
## Management commands
|
|
58
|
+
|
|
59
|
+
| Command | Description |
|
|
60
|
+
|---|---|
|
|
61
|
+
| `syncdocs` | Discover and sync all DRF endpoints into the database |
|
|
62
|
+
| `dumpdocs` | Export endpoint overviews to a JSON fixture |
|
|
63
|
+
| `loaddocs` | Import endpoint overviews from a JSON fixture |
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
MIT
|
|
File without changes
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Admin configuration for DocEndpoint and DocParameter."""
|
|
2
|
+
|
|
3
|
+
from django.contrib import admin
|
|
4
|
+
|
|
5
|
+
from docca.models import DocEndpoint, DocParameter, DocResponseField
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class DocParameterInline(admin.TabularInline):
|
|
9
|
+
model = DocParameter
|
|
10
|
+
extra = 1
|
|
11
|
+
fields = ['param_def', 'location', 'required', 'description_override', 'example']
|
|
12
|
+
readonly_fields = []
|
|
13
|
+
autocomplete_fields = ['param_def']
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DocResponseFieldInline(admin.TabularInline):
|
|
17
|
+
model = DocResponseField
|
|
18
|
+
extra = 1
|
|
19
|
+
fields = ['name', 'data_type', 'nullable', 'description', 'example']
|
|
20
|
+
readonly_fields = ['name', 'data_type', 'nullable']
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@admin.register(DocEndpoint)
|
|
24
|
+
class DocEndpointAdmin(admin.ModelAdmin):
|
|
25
|
+
list_display = ['method', 'path', 'app_label', 'tag', 'published', 'is_orphan']
|
|
26
|
+
list_filter = ['method', 'app_label', 'published', 'is_orphan', 'tag']
|
|
27
|
+
search_fields = ['path', 'view_name', 'summary']
|
|
28
|
+
readonly_fields = ['path', 'method', 'view_name', 'app_label', 'is_orphan', 'created_at', 'updated_at']
|
|
29
|
+
fields = [
|
|
30
|
+
'path', 'method', 'view_name', 'app_label',
|
|
31
|
+
'overview',
|
|
32
|
+
'summary', 'description',
|
|
33
|
+
'tag', 'published', 'is_orphan',
|
|
34
|
+
'created_at', 'updated_at',
|
|
35
|
+
]
|
|
36
|
+
inlines = [DocParameterInline, DocResponseFieldInline]
|
|
37
|
+
actions = ['publish_selected', 'unpublish_selected']
|
|
38
|
+
|
|
39
|
+
def publish_selected(self, request, queryset):
|
|
40
|
+
updated = queryset.update(published=True)
|
|
41
|
+
self.message_user(request, '%d endpoint(s) published.' % updated)
|
|
42
|
+
publish_selected.short_description = 'Publish selected endpoints'
|
|
43
|
+
|
|
44
|
+
def unpublish_selected(self, request, queryset):
|
|
45
|
+
updated = queryset.update(published=False)
|
|
46
|
+
self.message_user(request, '%d endpoint(s) unpublished.' % updated)
|
|
47
|
+
unpublish_selected.short_description = 'Unpublish selected endpoints'
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Admin configuration for DocParameterDef."""
|
|
2
|
+
|
|
3
|
+
from django.contrib import admin
|
|
4
|
+
|
|
5
|
+
from docca.models import DocParameterDef
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@admin.register(DocParameterDef)
|
|
9
|
+
class DocParameterDefAdmin(admin.ModelAdmin):
|
|
10
|
+
list_display = ['name', 'param_type', 'description']
|
|
11
|
+
list_filter = ['param_type']
|
|
12
|
+
search_fields = ['name', 'description']
|
|
13
|
+
ordering = ('name',)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Admin configuration for DocResponseField."""
|
|
2
|
+
|
|
3
|
+
from django.contrib import admin
|
|
4
|
+
|
|
5
|
+
from docca.models import DocResponseField
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class DocResponseFieldInline(admin.TabularInline):
|
|
9
|
+
model = DocResponseField
|
|
10
|
+
extra = 1
|
|
11
|
+
fields = ['name', 'data_type', 'nullable', 'description', 'example']
|
|
12
|
+
readonly_fields = ['name', 'data_type', 'nullable']
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Admin configuration for DocTag."""
|
|
2
|
+
|
|
3
|
+
from django.contrib import admin
|
|
4
|
+
|
|
5
|
+
from docca.models import DocTag
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@admin.register(DocTag)
|
|
9
|
+
class DocTagAdmin(admin.ModelAdmin):
|
|
10
|
+
list_display = ['name', 'slug', 'order']
|
|
11
|
+
prepopulated_fields = {'slug': ('name',)}
|
|
12
|
+
ordering = ('order', 'name')
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Management command to export docca endpoint overviews to a JSON file."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from django.core.management.base import BaseCommand
|
|
7
|
+
from django.utils import timezone
|
|
8
|
+
|
|
9
|
+
from docca.models import DocEndpoint, DocParameterDef, DocResponseField
|
|
10
|
+
|
|
11
|
+
DEFAULT_OUTPUT_DIR = 'fixtures'
|
|
12
|
+
DEFAULT_FILENAME_TEMPLATE = 'docca_overviews_{timestamp}.json'
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Command(BaseCommand):
|
|
16
|
+
"""Export DocEndpoint manager-controlled fields to a JSON file.
|
|
17
|
+
|
|
18
|
+
Produces a file keyed by (path, method) — not pk — so it is portable
|
|
19
|
+
across environments and survives database resets. Only the fields that
|
|
20
|
+
managers own are exported:
|
|
21
|
+
|
|
22
|
+
overview, tag (slug), published
|
|
23
|
+
|
|
24
|
+
syncdocs-managed fields (summary, description, app_label, view_name)
|
|
25
|
+
are excluded — they are always re-derived from code.
|
|
26
|
+
|
|
27
|
+
Safe to run at any time without affecting live data.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
help = 'Export docca endpoint overviews and visibility settings to JSON.'
|
|
31
|
+
|
|
32
|
+
def add_arguments(self, parser):
|
|
33
|
+
parser.add_argument(
|
|
34
|
+
'--output',
|
|
35
|
+
type=str,
|
|
36
|
+
default=None,
|
|
37
|
+
help=(
|
|
38
|
+
'Path to write the JSON file. '
|
|
39
|
+
'Defaults to fixtures/docca_overviews_<timestamp>.json.'
|
|
40
|
+
),
|
|
41
|
+
)
|
|
42
|
+
parser.add_argument(
|
|
43
|
+
'--app',
|
|
44
|
+
type=str,
|
|
45
|
+
default=None,
|
|
46
|
+
help='Limit export to endpoints belonging to a specific app label.',
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
def handle(self, **options):
|
|
50
|
+
output_path = options['output']
|
|
51
|
+
app_label = options['app']
|
|
52
|
+
|
|
53
|
+
if output_path is None:
|
|
54
|
+
timestamp = timezone.now().strftime('%Y%m%d_%H%M%S')
|
|
55
|
+
filename = DEFAULT_FILENAME_TEMPLATE.format(timestamp=timestamp)
|
|
56
|
+
output_path = os.path.join(DEFAULT_OUTPUT_DIR, filename)
|
|
57
|
+
|
|
58
|
+
output_dir = os.path.dirname(output_path)
|
|
59
|
+
if output_dir and not os.path.exists(output_dir):
|
|
60
|
+
os.makedirs(output_dir)
|
|
61
|
+
|
|
62
|
+
qs = DocEndpoint.objects.select_related('tag').order_by('path', 'method')
|
|
63
|
+
if app_label:
|
|
64
|
+
qs = qs.filter(app_label=app_label)
|
|
65
|
+
|
|
66
|
+
# Endpoint overviews
|
|
67
|
+
endpoints = []
|
|
68
|
+
for ep in qs:
|
|
69
|
+
params = []
|
|
70
|
+
for p in ep.parameters.select_related('param_def').all():
|
|
71
|
+
params.append({
|
|
72
|
+
'name': p.param_def.name,
|
|
73
|
+
'location': p.location,
|
|
74
|
+
'description_override': p.description_override,
|
|
75
|
+
'example': p.example,
|
|
76
|
+
})
|
|
77
|
+
response_fields = []
|
|
78
|
+
for f in ep.response_fields.all():
|
|
79
|
+
response_fields.append({
|
|
80
|
+
'name': f.name,
|
|
81
|
+
'data_type': f.data_type,
|
|
82
|
+
'nullable': f.nullable,
|
|
83
|
+
'description': f.description,
|
|
84
|
+
'example': f.example,
|
|
85
|
+
})
|
|
86
|
+
endpoints.append({
|
|
87
|
+
'path': ep.path,
|
|
88
|
+
'method': ep.method,
|
|
89
|
+
'app_label': ep.app_label,
|
|
90
|
+
'overview': ep.overview,
|
|
91
|
+
'tag': ep.tag.slug if ep.tag else None,
|
|
92
|
+
'published': ep.published,
|
|
93
|
+
'parameters': params,
|
|
94
|
+
'response_fields': response_fields,
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
# Parameter definition registry
|
|
98
|
+
param_defs = [
|
|
99
|
+
{
|
|
100
|
+
'name': d.name,
|
|
101
|
+
'param_type': d.param_type,
|
|
102
|
+
'description': d.description,
|
|
103
|
+
}
|
|
104
|
+
for d in DocParameterDef.objects.order_by('name')
|
|
105
|
+
]
|
|
106
|
+
|
|
107
|
+
output = {
|
|
108
|
+
'param_defs': param_defs,
|
|
109
|
+
'endpoints': endpoints,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
with open(output_path, 'w') as f:
|
|
113
|
+
json.dump(output, f, indent=2)
|
|
114
|
+
|
|
115
|
+
total_response_fields = sum(len(e['response_fields']) for e in endpoints)
|
|
116
|
+
self.stdout.write(self.style.SUCCESS('docca config exported to %s' % output_path))
|
|
117
|
+
self.stdout.write(' Endpoints: %d' % len(endpoints))
|
|
118
|
+
self.stdout.write(' Parameter defs: %d' % len(param_defs))
|
|
119
|
+
self.stdout.write(' Response fields: %d' % total_response_fields)
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Management command to import dokks configuration from a JSON file."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from django.core.management.base import BaseCommand, CommandError
|
|
7
|
+
from django.db import transaction
|
|
8
|
+
|
|
9
|
+
from docca.models import DocEndpoint, DocParameter, DocParameterDef, DocResponseField, DocTag
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Command(BaseCommand):
|
|
13
|
+
"""Import manager-controlled fields into dokks models from a JSON file.
|
|
14
|
+
|
|
15
|
+
Reads a file produced by ``dumpdocs``. The file contains two sections:
|
|
16
|
+
|
|
17
|
+
``param_defs``
|
|
18
|
+
Canonical parameter definitions — name, type, and description.
|
|
19
|
+
Matched by name. Only ``param_type`` and ``description`` are
|
|
20
|
+
written; existing records are updated.
|
|
21
|
+
|
|
22
|
+
``endpoints``
|
|
23
|
+
Matched by (path, method). Only manager-owned fields are written:
|
|
24
|
+
``overview``, ``tag``, ``published``. For each endpoint's
|
|
25
|
+
``parameters`` list, ``description_override`` and ``example`` are
|
|
26
|
+
updated; syncdocs-managed fields (``required``, ``location``) are
|
|
27
|
+
never touched here.
|
|
28
|
+
|
|
29
|
+
Runs inside a transaction — rolled back entirely on any error unless
|
|
30
|
+
``--dry-run`` is used.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
help = 'Import dokks configuration from a JSON file produced by dumpdocs.'
|
|
34
|
+
|
|
35
|
+
def add_arguments(self, parser):
|
|
36
|
+
parser.add_argument('input', type=str)
|
|
37
|
+
parser.add_argument('--dry-run', action='store_true')
|
|
38
|
+
|
|
39
|
+
def handle(self, **options):
|
|
40
|
+
input_path = options['input']
|
|
41
|
+
dry_run = options['dry_run']
|
|
42
|
+
|
|
43
|
+
if not os.path.exists(input_path):
|
|
44
|
+
raise CommandError('File not found: %s' % input_path)
|
|
45
|
+
|
|
46
|
+
with open(input_path, 'r') as f:
|
|
47
|
+
try:
|
|
48
|
+
data = json.load(f)
|
|
49
|
+
except json.JSONDecodeError as exc:
|
|
50
|
+
raise CommandError('Failed to parse JSON: %s' % exc)
|
|
51
|
+
|
|
52
|
+
if dry_run:
|
|
53
|
+
self.stdout.write(self.style.WARNING('Dry run — no changes will be written.'))
|
|
54
|
+
|
|
55
|
+
counts = {
|
|
56
|
+
'param_defs': 0,
|
|
57
|
+
'endpoints_updated': 0,
|
|
58
|
+
'endpoints_skipped': 0,
|
|
59
|
+
'params_updated': 0,
|
|
60
|
+
'response_fields_updated': 0,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
try:
|
|
64
|
+
with transaction.atomic():
|
|
65
|
+
# --- Parameter definition registry ---
|
|
66
|
+
for record in data.get('param_defs', []):
|
|
67
|
+
name = record.get('name')
|
|
68
|
+
if not name:
|
|
69
|
+
continue
|
|
70
|
+
obj, _ = DocParameterDef.objects.get_or_create(name=name)
|
|
71
|
+
obj.param_type = record.get('param_type', obj.param_type)
|
|
72
|
+
obj.description = record.get('description', obj.description)
|
|
73
|
+
if not dry_run:
|
|
74
|
+
obj.save(update_fields=['param_type', 'description'])
|
|
75
|
+
counts['param_defs'] += 1
|
|
76
|
+
|
|
77
|
+
# --- Endpoint overviews ---
|
|
78
|
+
for record in data.get('endpoints', []):
|
|
79
|
+
path = record.get('path')
|
|
80
|
+
method = record.get('method')
|
|
81
|
+
if not path or not method:
|
|
82
|
+
continue
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
ep = DocEndpoint.objects.get(path=path, method=method)
|
|
86
|
+
except DocEndpoint.DoesNotExist:
|
|
87
|
+
self.stdout.write(
|
|
88
|
+
self.style.WARNING('Not in DB, skipped: %s %s' % (method, path))
|
|
89
|
+
)
|
|
90
|
+
counts['endpoints_skipped'] += 1
|
|
91
|
+
continue
|
|
92
|
+
|
|
93
|
+
ep.overview = record.get('overview') or ''
|
|
94
|
+
ep.published = record.get('published', True)
|
|
95
|
+
|
|
96
|
+
tag_slug = record.get('tag')
|
|
97
|
+
if tag_slug:
|
|
98
|
+
try:
|
|
99
|
+
ep.tag = DocTag.objects.get(slug=tag_slug)
|
|
100
|
+
except DocTag.DoesNotExist:
|
|
101
|
+
self.stdout.write(
|
|
102
|
+
self.style.WARNING(
|
|
103
|
+
'Tag "%s" not found, skipped for %s %s' % (tag_slug, method, path)
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
else:
|
|
107
|
+
ep.tag = None
|
|
108
|
+
|
|
109
|
+
if not dry_run:
|
|
110
|
+
ep.save(update_fields=['overview', 'published', 'tag'])
|
|
111
|
+
counts['endpoints_updated'] += 1
|
|
112
|
+
|
|
113
|
+
# Per-endpoint parameter overrides (and creation for manually documented params)
|
|
114
|
+
for p_record in record.get('parameters', []):
|
|
115
|
+
p_name = p_record.get('name')
|
|
116
|
+
p_location = p_record.get('location')
|
|
117
|
+
if not p_name or not p_location:
|
|
118
|
+
continue
|
|
119
|
+
try:
|
|
120
|
+
param_def, _ = DocParameterDef.objects.get_or_create(
|
|
121
|
+
name=p_name,
|
|
122
|
+
defaults={'param_type': 'string'},
|
|
123
|
+
)
|
|
124
|
+
if not dry_run:
|
|
125
|
+
param, created = DocParameter.objects.get_or_create(
|
|
126
|
+
endpoint=ep,
|
|
127
|
+
param_def=param_def,
|
|
128
|
+
location=p_location,
|
|
129
|
+
defaults={'required': p_record.get('required', False)},
|
|
130
|
+
)
|
|
131
|
+
else:
|
|
132
|
+
created = False
|
|
133
|
+
param = DocParameter(
|
|
134
|
+
endpoint=ep, param_def=param_def, location=p_location
|
|
135
|
+
)
|
|
136
|
+
param.description_override = p_record.get('description_override', '')
|
|
137
|
+
param.example = p_record.get('example', '')
|
|
138
|
+
if not dry_run:
|
|
139
|
+
param.save(update_fields=['description_override', 'example'])
|
|
140
|
+
counts['params_updated'] += 1
|
|
141
|
+
except Exception as exc:
|
|
142
|
+
self.stdout.write(
|
|
143
|
+
self.style.WARNING(
|
|
144
|
+
'Parameter "%s" error for %s %s: %s' % (p_name, method, path, exc)
|
|
145
|
+
)
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
# Response field descriptions and examples
|
|
149
|
+
for f_record in record.get('response_fields', []):
|
|
150
|
+
f_name = f_record.get('name')
|
|
151
|
+
if not f_name:
|
|
152
|
+
continue
|
|
153
|
+
try:
|
|
154
|
+
rf = DocResponseField.objects.get(endpoint=ep, name=f_name)
|
|
155
|
+
rf.description = f_record.get('description', rf.description)
|
|
156
|
+
rf.example = f_record.get('example', rf.example)
|
|
157
|
+
if not dry_run:
|
|
158
|
+
rf.save(update_fields=['description', 'example'])
|
|
159
|
+
counts['response_fields_updated'] += 1
|
|
160
|
+
except DocResponseField.DoesNotExist:
|
|
161
|
+
# Field not yet in DB (e.g. manually documented endpoint)
|
|
162
|
+
# Create it with manager-owned fields only
|
|
163
|
+
if not dry_run:
|
|
164
|
+
DocResponseField.objects.create(
|
|
165
|
+
endpoint=ep,
|
|
166
|
+
name=f_name,
|
|
167
|
+
data_type=f_record.get('data_type', 'string'),
|
|
168
|
+
nullable=f_record.get('nullable', False),
|
|
169
|
+
description=f_record.get('description', ''),
|
|
170
|
+
example=f_record.get('example', ''),
|
|
171
|
+
)
|
|
172
|
+
counts['response_fields_updated'] += 1
|
|
173
|
+
|
|
174
|
+
if dry_run:
|
|
175
|
+
raise _DryRunRollback()
|
|
176
|
+
|
|
177
|
+
except _DryRunRollback:
|
|
178
|
+
pass
|
|
179
|
+
|
|
180
|
+
action = 'Would update' if dry_run else 'Updated'
|
|
181
|
+
self.stdout.write(self.style.SUCCESS('%s dokks config from %s' % (action, input_path)))
|
|
182
|
+
self.stdout.write(' Parameter defs: %d' % counts['param_defs'])
|
|
183
|
+
self.stdout.write(' Endpoints: %d' % counts['endpoints_updated'])
|
|
184
|
+
self.stdout.write(' Skipped: %d' % counts['endpoints_skipped'])
|
|
185
|
+
self.stdout.write(' Param overrides: %d' % counts['params_updated'])
|
|
186
|
+
self.stdout.write(' Response fields: %d' % counts['response_fields_updated'])
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class _DryRunRollback(Exception):
|
|
190
|
+
pass
|