Repository: digitalfabrik/integreat-cms

Issue:
Issue with Cache in children endpoint
**This issue needs further investigation/testing**. For Malte the cache is currently disabled and the performance is acceptable. The priority of this issue depends on the performance of the migrated Integreat CMS.

### Describe the Bug
When calling random pages we sometimes get the following stack trace:
```
Feb 21 22:53:01 ERROR   django.request - Internal Server Error: /leakarlsruhefelsstrasse/de/wp-json/extensions/v3/children/
Traceback (most recent call last):
  File "/opt/integreat-cms/.venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/opt/integreat-cms/.venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/integreat-cms/.venv/lib/python3.9/site-packages/integreat_cms/api/decorators.py", line 178, in wrap
    return func(request, *args, **kwargs)
  File "/opt/integreat-cms/.venv/lib/python3.9/site-packages/integreat_cms/api/decorators.py", line 124, in wrap
    return function(request, *args, **kwargs)
  File "/opt/integreat-cms/.venv/lib/python3.9/site-packages/integreat_cms/api/v3/pages.py", line 205, in children
    result.append(transform_page(public_translation))
  File "/opt/integreat-cms/.venv/lib/python3.9/site-packages/integreat_cms/api/v3/pages.py", line 22, in transform_page
    parent_page = page_translation.page.cached_parent
  File "/opt/integreat-cms/.venv/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/opt/integreat-cms/.venv/lib/python3.9/site-packages/integreat_cms/cms/models/abstract_tree_node.py", line 210, in cached_parent
    return self.get_cached_ancestors()[-1]
IndexError: list index out of range
```
This trace persists until the cache is flushed.

### Steps to Reproduce

Happens randomly. Visit pages until it happens.

For the time being we could use a simple filter instead of a cache operation in https://github.com/digitalfabrik/integreat-cms/blob/develop/integreat_cms/api/v3/pages.py#L231


Discussion:

Resolving patch:
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
@@ -19,7 +19,7 @@ def transform_page(page_translation):
     :return: data necessary for API
     :rtype: dict
     """
-    parent_page = page_translation.page.cached_parent
+    parent_page = page_translation.page.parent
     if parent_page:
         parent_absolute_url = parent_page.get_public_translation(
             page_translation.language.slug
@@ -228,7 +228,7 @@ def parents(request, region_slug, language_slug):
     """
     current_page = get_single_page(request, language_slug)
     result = []
-    for ancestor in current_page.get_cached_ancestors(include_self=False):
+    for ancestor in current_page.get_ancestors():
         public_translation = ancestor.get_public_translation(language_slug)
         if not public_translation:
             raise Http404("No Page matches the given url or id.")


Test patch: