lxcor-docca 0.1.0__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.
- docca/__init__.py +0 -0
- docca/admin/__init__.py +5 -0
- docca/admin/doc_endpoint.py +47 -0
- docca/admin/doc_parameter_def.py +13 -0
- docca/admin/doc_response_field.py +12 -0
- docca/admin/doc_tag.py +12 -0
- docca/apps.py +5 -0
- docca/management/__init__.py +0 -0
- docca/management/commands/__init__.py +0 -0
- docca/management/commands/dumpdocs.py +119 -0
- docca/management/commands/loaddocs.py +190 -0
- docca/management/commands/syncdocs.py +473 -0
- docca/migrations/0001_initial.py +65 -0
- docca/migrations/0002_add_overview.py +18 -0
- docca/migrations/0003_add_parameter_def.py +84 -0
- docca/migrations/0004_add_response_field.py +56 -0
- docca/migrations/0005_add_slug.py +52 -0
- docca/migrations/__init__.py +0 -0
- docca/models/__init__.py +7 -0
- docca/models/doc_endpoint.py +101 -0
- docca/models/doc_parameter.py +65 -0
- docca/models/doc_parameter_def.py +48 -0
- docca/models/doc_response_field.py +67 -0
- docca/models/doc_tag.py +31 -0
- docca/templatetags/__init__.py +0 -0
- docca/templatetags/docca_tags.py +141 -0
- docca/urls.py +11 -0
- docca/views.py +172 -0
- lxcor_docca-0.1.0.dist-info/LICENSE +21 -0
- lxcor_docca-0.1.0.dist-info/METADATA +91 -0
- lxcor_docca-0.1.0.dist-info/RECORD +33 -0
- lxcor_docca-0.1.0.dist-info/WHEEL +5 -0
- lxcor_docca-0.1.0.dist-info/top_level.txt +1 -0
docca/__init__.py
ADDED
|
File without changes
|
docca/admin/__init__.py
ADDED
|
@@ -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']
|
docca/admin/doc_tag.py
ADDED
|
@@ -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')
|
docca/apps.py
ADDED
|
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
|