Skip to content

Commit

Permalink
Merge pull request #1096 from digitalfabrik/feature/api-parents-endpoint
Browse files Browse the repository at this point in the history
APIv3: add parents/ancestors end point, fixes #19
  • Loading branch information
ulliholtgrave committed Jan 11, 2022
2 parents 8368f47 + 77953f6 commit 425c365
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion integreat_cms/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions integreat_cms/api/v3/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 425c365

Please sign in to comment.