django-admin-api-mcp 0.1.0a2__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.
Files changed (49) hide show
  1. admin_api/__init__.py +9 -0
  2. admin_api/actions.py +115 -0
  3. admin_api/admin.py +108 -0
  4. admin_api/apps.py +9 -0
  5. admin_api/auth.py +62 -0
  6. admin_api/changelist.py +194 -0
  7. admin_api/checks.py +35 -0
  8. admin_api/contrib/__init__.py +1 -0
  9. admin_api/contrib/subadmin.py +37 -0
  10. admin_api/custom_views.py +458 -0
  11. admin_api/decorators.py +26 -0
  12. admin_api/deletion.py +54 -0
  13. admin_api/detail.py +127 -0
  14. admin_api/encoders.py +59 -0
  15. admin_api/errors.py +44 -0
  16. admin_api/jsonschema.py +216 -0
  17. admin_api/management/__init__.py +0 -0
  18. admin_api/management/commands/__init__.py +0 -0
  19. admin_api/management/commands/create_api_token.py +40 -0
  20. admin_api/management/commands/mcp_serve.py +86 -0
  21. admin_api/mcp/__init__.py +7 -0
  22. admin_api/mcp/bridge.py +71 -0
  23. admin_api/mcp/confirm.py +54 -0
  24. admin_api/mcp/handlers.py +665 -0
  25. admin_api/mcp/server.py +75 -0
  26. admin_api/mcp/sites.py +198 -0
  27. admin_api/mcp/stdio.py +53 -0
  28. admin_api/mcp/tools.py +84 -0
  29. admin_api/mcp/views.py +69 -0
  30. admin_api/middleware.py +18 -0
  31. admin_api/migrations/0001_initial.py +40 -0
  32. admin_api/migrations/__init__.py +0 -0
  33. admin_api/mixins.py +144 -0
  34. admin_api/models.py +113 -0
  35. admin_api/nested.py +122 -0
  36. admin_api/resolver.py +95 -0
  37. admin_api/scopes.py +121 -0
  38. admin_api/services.py +423 -0
  39. admin_api/sites.py +225 -0
  40. admin_api/views/__init__.py +0 -0
  41. admin_api/views/changelist.py +50 -0
  42. admin_api/views/custom.py +31 -0
  43. admin_api/views/detail.py +39 -0
  44. admin_api/views/nested.py +70 -0
  45. admin_api/views/registry.py +15 -0
  46. admin_api/writes.py +479 -0
  47. django_admin_api_mcp-0.1.0a2.dist-info/METADATA +274 -0
  48. django_admin_api_mcp-0.1.0a2.dist-info/RECORD +49 -0
  49. django_admin_api_mcp-0.1.0a2.dist-info/WHEEL +4 -0
@@ -0,0 +1,50 @@
1
+ """REST adapters over :mod:`admin_api.services`: parse the HTTP request, call the service,
2
+ wrap the result in a ``JsonResponse``."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from django.http import JsonResponse
7
+ from django.views import View
8
+
9
+ from .. import services
10
+ from ..writes import parse_json_body
11
+
12
+
13
+ class ModelApiView(View):
14
+ api_site = None
15
+ # nested admins (admin_api.views.nested): child segments and the parent pks, one per level
16
+ segments: tuple = ()
17
+ parents: tuple = ()
18
+
19
+ def get_model_admin(self, request, app_label, model_name):
20
+ return services.get_model_admin(
21
+ self.api_site, request, app_label, model_name, segments=self.segments, parents=self.parents
22
+ )
23
+
24
+
25
+ class ChangeListView(ModelApiView):
26
+ """``GET /v1/<app>/<model>/`` (Django's changelist as JSON), ``POST`` (create)."""
27
+
28
+ def get(self, request, app_label, model_name):
29
+ model_admin = self.get_model_admin(request, app_label, model_name)
30
+ return JsonResponse(services.changelist(self.api_site, model_admin, request))
31
+
32
+ def post(self, request, app_label, model_name):
33
+ model_admin = self.get_model_admin(request, app_label, model_name)
34
+ return JsonResponse(services.create(model_admin, request, lambda: parse_json_body(request)), status=201)
35
+
36
+
37
+ class ActionView(ModelApiView):
38
+ """``POST /v1/<app>/<model>/actions/<name>/``"""
39
+
40
+ def post(self, request, app_label, model_name, action_name):
41
+ model_admin = self.get_model_admin(request, app_label, model_name)
42
+ return JsonResponse(services.run_action(model_admin, request, action_name, parse_json_body(request)))
43
+
44
+
45
+ class SchemaView(ModelApiView):
46
+ """``GET /v1/<app>/<model>/schema/``: what the list endpoint accepts and returns."""
47
+
48
+ def get(self, request, app_label, model_name):
49
+ model_admin = self.get_model_admin(request, app_label, model_name)
50
+ return JsonResponse(services.schema(self.api_site, model_admin, request))
@@ -0,0 +1,31 @@
1
+ from __future__ import annotations
2
+
3
+ from django.http import JsonResponse
4
+
5
+ from .. import custom_views
6
+ from ..errors import MethodNotAllowed
7
+ from ..writes import parse_json_body
8
+ from .changelist import ModelApiView
9
+
10
+
11
+ class CustomViewView(ModelApiView):
12
+ """``/v1/<app>/<model>/views/<name>/`` and ``/v1/<app>/<model>/<pk>/views/<name>/``: a method
13
+ decorated with ``@admin_api.view``. GET takes the parameters from the query string, POST from
14
+ a JSON body; write views are POST only."""
15
+
16
+ http_method_names = ["get", "post"]
17
+
18
+ def get(self, request, app_label, model_name, view_name, object_id=None):
19
+ return self._run(request, app_label, model_name, view_name, object_id, request.GET)
20
+
21
+ def post(self, request, app_label, model_name, view_name, object_id=None):
22
+ return self._run(request, app_label, model_name, view_name, object_id, None)
23
+
24
+ def _run(self, request, app_label, model_name, view_name, object_id, data):
25
+ model_admin = self.get_model_admin(request, app_label, model_name)
26
+ spec = custom_views.get_view(model_admin, request, view_name)
27
+ if request.method not in spec.methods:
28
+ raise MethodNotAllowed(f"Allowed: {', '.join(spec.methods)}")
29
+ if data is None:
30
+ data = parse_json_body(request)
31
+ return JsonResponse(custom_views.run_view(model_admin, request, view_name, object_id=object_id, data=data))
@@ -0,0 +1,39 @@
1
+ from __future__ import annotations
2
+
3
+ from django.http import JsonResponse
4
+
5
+ from .. import services
6
+ from ..writes import parse_json_body
7
+ from .changelist import ModelApiView
8
+
9
+
10
+ class ObjectView(ModelApiView):
11
+ """``/v1/<app>/<model>/<pk>/``: GET (detail), PATCH/PUT (update), DELETE."""
12
+
13
+ def get(self, request, app_label, model_name, object_id):
14
+ model_admin = self.get_model_admin(request, app_label, model_name)
15
+ return JsonResponse(services.detail(model_admin, request, object_id))
16
+
17
+ def patch(self, request, app_label, model_name, object_id):
18
+ return self._update(request, app_label, model_name, object_id, partial=True)
19
+
20
+ def put(self, request, app_label, model_name, object_id):
21
+ return self._update(request, app_label, model_name, object_id, partial=False)
22
+
23
+ def _update(self, request, app_label, model_name, object_id, *, partial):
24
+ model_admin = self.get_model_admin(request, app_label, model_name)
25
+ # the body is parsed lazily, after the permission check
26
+ payload = services.update(model_admin, request, object_id, lambda: parse_json_body(request), partial=partial)
27
+ return JsonResponse(payload)
28
+
29
+ def delete(self, request, app_label, model_name, object_id):
30
+ model_admin = self.get_model_admin(request, app_label, model_name)
31
+ return JsonResponse(services.delete(model_admin, request, object_id))
32
+
33
+
34
+ class DeletePreviewView(ModelApiView):
35
+ """``GET /v1/<app>/<model>/<pk>/delete-preview/``: what a DELETE would remove."""
36
+
37
+ def get(self, request, app_label, model_name, object_id):
38
+ model_admin = self.get_model_admin(request, app_label, model_name)
39
+ return JsonResponse(services.delete_preview(model_admin, request, object_id))
@@ -0,0 +1,70 @@
1
+ """REST routing for nested admins (see :mod:`admin_api.nested`).
2
+
3
+ ``/v1/<app>/<model>/<pk>/<child>/[<pk>/<child>/…]`` followed by the same tails as a top-level
4
+ model: ``""`` (changelist), ``schema/``, ``actions/<name>/``, ``views/<name>/``, ``<pk>/``,
5
+ ``<pk>/delete-preview/``, ``<pk>/views/<name>/``.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import re
11
+
12
+ from ..errors import MethodNotAllowed, NotFound
13
+ from .changelist import ActionView, ChangeListView, SchemaView
14
+ from .custom import CustomViewView
15
+ from .detail import DeletePreviewView, ObjectView
16
+
17
+ ROUTES = {
18
+ "changelist": (ChangeListView, ("GET", "POST")),
19
+ "schema": (SchemaView, ("GET",)),
20
+ "action": (ActionView, ("POST",)),
21
+ "object": (ObjectView, ("GET", "PATCH", "PUT", "DELETE")),
22
+ "delete_preview": (DeletePreviewView, ("GET",)),
23
+ "view": (CustomViewView, ("GET", "POST")),
24
+ "object_view": (CustomViewView, ("GET", "POST")),
25
+ }
26
+ NESTED_METHODS = ("GET", "POST", "PATCH", "PUT", "DELETE")
27
+ _SEGMENT_RE = re.compile(r"^[a-z_][a-z0-9_]*$") # a model_name
28
+
29
+
30
+ def parse_nested(nested: str) -> tuple[list[str], list[str], str, dict]:
31
+ """``"1/sales/7/salesitem"`` → ``(["sales", "salesitem"], ["1", "7"], "changelist", {})``."""
32
+ parts = nested.split("/")
33
+ segments, parents = [], []
34
+
35
+ def is_tail(rest):
36
+ if len(rest) == 3:
37
+ return rest[1] == "views"
38
+ return len(rest) == 2 and (rest[0] in ("actions", "views") or rest[1] == "delete-preview")
39
+
40
+ while len(parts) >= 2 and not is_tail(parts):
41
+ parents.append(parts[0])
42
+ segments.append(parts[1])
43
+ parts = parts[2:]
44
+ if not segments or not all(map(_SEGMENT_RE.match, segments)) or not all(parents) or not all(parts):
45
+ raise NotFound("No such API endpoint.")
46
+ if not parts:
47
+ return segments, parents, "changelist", {}
48
+ if parts == ["schema"]:
49
+ return segments, parents, "schema", {}
50
+ if len(parts) == 2 and parts[0] == "actions":
51
+ return segments, parents, "action", {"action_name": parts[1]}
52
+ if len(parts) == 2 and parts[0] == "views":
53
+ return segments, parents, "view", {"view_name": parts[1]}
54
+ if len(parts) == 3:
55
+ return segments, parents, "object_view", {"object_id": parts[0], "view_name": parts[2]}
56
+ if len(parts) == 1:
57
+ return segments, parents, "object", {"object_id": parts[0]}
58
+ return segments, parents, "delete_preview", {"object_id": parts[0]}
59
+
60
+
61
+ def nested_view(api_site):
62
+ def view(request, app_label, model_name, nested):
63
+ segments, parents, kind, kwargs = parse_nested(nested)
64
+ view_class, methods = ROUTES[kind]
65
+ if request.method not in methods:
66
+ raise MethodNotAllowed(f"Allowed: {', '.join(methods)}")
67
+ inner = view_class.as_view(api_site=api_site, segments=tuple(segments), parents=tuple(parents))
68
+ return inner(request, app_label=app_label, model_name=model_name, **kwargs)
69
+
70
+ return view
@@ -0,0 +1,15 @@
1
+ from __future__ import annotations
2
+
3
+ from django.http import JsonResponse
4
+ from django.views import View
5
+
6
+ from .. import services
7
+
8
+
9
+ class RegistryView(View):
10
+ """``GET /v1/``: the apps/models the current user may use, with their permissions."""
11
+
12
+ api_site = None
13
+
14
+ def get(self, request):
15
+ return JsonResponse(services.registry(self.api_site, request))