From 60e789c15e79ce5f6d65271e2da46a818f88e120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Sun, 3 Dec 2023 17:54:26 +0100 Subject: [PATCH] implement editor view_as_api v2 --- src/c3nav/editor/newapi/endpoints.py | 34 +++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/c3nav/editor/newapi/endpoints.py b/src/c3nav/editor/newapi/endpoints.py index d4e3db19..abd87616 100644 --- a/src/c3nav/editor/newapi/endpoints.py +++ b/src/c3nav/editor/newapi/endpoints.py @@ -1,3 +1,5 @@ +from django.urls import Resolver404, resolve +from django.utils.translation import gettext_lazy as _ from ninja import Router as APIRouter from c3nav.api.exceptions import API404 @@ -96,8 +98,26 @@ def level_geometries(request, level_id: EditorID, update_cache_key: UpdateCacheK # todo: need a way to pass the changeset if it's not a session API key +def resolve_editor_path_api(request, path): + resolved = None + if path: + try: + resolved = resolve('/editor/'+path+'/') + except Resolver404: + pass -@editor_api_router.get('/{path:path}/', summary="access the editor UI programmatically", + if not resolved: + try: + resolved = resolve('/editor/'+path) + except Resolver404: + pass + + request.sub_resolver_match = resolved + + return resolved + + +@editor_api_router.get('/{path:path}', summary="access the editor UI programmatically", response={200: dict, **API404.dict(), **auth_permission_responses}, openapi_extra={"security": [{"APITokenAuth": ["editor_access"]}]}) @newapi_etag() # todo: correct? @@ -107,11 +127,19 @@ def view_as_api(request, path: str): `path` is the path after /editor/. this is a mess. good luck. if you actually want to use this, poke us so we might add better documentation. """ + resolved = resolve_editor_path_api(request, path) - raise NotImplementedError + if not resolved: + raise API404(str(_('No matching editor view endpoint found.'))) + + if not getattr(resolved.func, 'api_hybrid', False): + raise API404(_('Matching editor view point does not provide an API.')) + + response = resolved.func(request, api=True, *resolved.args, **resolved.kwargs) + return response -@editor_api_router.post('/{path:path}/', summary="access the editor UI programmatically", +@editor_api_router.post('/{path:path}', summary="access the editor UI programmatically", response={200: dict, **API404.dict(), **auth_permission_responses}, openapi_extra={"security": [{"APITokenAuth": ["editor_access", "write"]}]}) @newapi_etag() # todo: correct?