Repository: digitalfabrik/integreat-cms

Issue:
API: parents endpoint
Endpoint to get parents of a page up to the root. This must be compatible to Integreat API v3.

https://github.com/Integreat/cms/wiki/REST-APIv3-Documentation#parents-of-a-post

@maxammann @Simple-codinger is this used by any app? Otherwise we could move this to another milestone.


Discussion:
See: https://github.com/Integreat/cms-django/issues/17
So we decided that this is not immediately necessary as there are no plans to change the API in the foreseeable future.
@klinzo Is this endpoint in any way relevant for the app anymore?
@ulliholtgrave Yes, it is used in the web app and therefore absolutely crucial. Without it, the webapp won't work.

E.g. this site https://webnext.integreat.app/augsburg/en/work-and-education uses this request https://cms-test.integreat-app.de/augsburg/en/wp-json/extensions/v3/parents?&url=/augsburg/en/work-and-education
Good to know :see_no_evil: 
There is also a sentry report for this: https://sentry.tuerantuer.org/organizations/digitalfabrik/issues/416/?query=is%3Aunresolved

Resolving patch:
diff --git a/integreat_cms/api/urls.py b/integreat_cms/api/urls.py
--- a/integreat_cms/api/urls.py
+++ b/integreat_cms/api/urls.py
@@ -20,7 +20,7 @@
 from .v3.imprint import imprint
 from .v3.languages import languages
 from .v3.locations import locations
-from .v3.pages import pages, children, single_page
+from .v3.pages import pages, children, parents, single_page
 from .v3.pdf_export import pdf_export
 from .v3.push_notifications import sent_push_notifications
 from .v3.regions import regions, liveregions, hiddenregions, pushnew
@@ -36,6 +36,7 @@
         name="api_single_page",
     ),
     url(r"^children/?$", children, name="api_children"),
+    url(r"^parents/?$", parents, name="api_parents"),
     url(
         r"^pdf/?$",
         pdf_export,
diff --git a/integreat_cms/api/v3/pages.py b/integreat_cms/api/v3/pages.py
--- a/integreat_cms/api/v3/pages.py
+++ b/integreat_cms/api/v3/pages.py
@@ -200,3 +200,33 @@ def children(request, region_slug, language_slug):
             if public_translation:
                 result.append(transform_page(public_translation))
     return JsonResponse(result, safe=False)
+
+
+@json_response
+# pylint: disable=unused-argument
+def parents(request, region_slug, language_slug):
+    """
+    Retrieves all ancestors (parent and all nodes up to the root node) of a page
+
+    :param request: The request that has been sent to the Django server
+    :type request: ~django.http.HttpRequest
+
+    :param region_slug: Slug defining the region
+    :type region_slug: str
+
+    :param language_slug: Code to identify the desired language
+    :type language_slug: str
+
+    :raises ~django.http.Http404: HTTP status 404 if the request is malformed or no page with the given id or url exists.
+
+    :return: JSON with the requested page ancestors
+    :rtype: ~django.http.JsonResponse
+    """
+    current_page = get_single_page(request, language_slug)
+    result = []
+    for ancestor in current_page.get_ancestors(include_self=False):
+        public_translation = ancestor.get_public_translation(language_slug)
+        if not public_translation:
+            raise Http404("No Page matches the given url or id.")
+        result.append(transform_page(public_translation))
+    return JsonResponse(result, safe=False)


Test patch: