-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1088 from digitalfabrik/refactoring/page-performance
Improve performance of page tree
- Loading branch information
Showing
187 changed files
with
7,611 additions
and
2,173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
Please run this script via the dev tool duplicate_pages.sh to make sure all environment | ||
variables are passed correctly and Django can be set up. | ||
""" | ||
import copy | ||
import logging | ||
import random | ||
import string | ||
import sys | ||
|
||
from django.utils.text import slugify | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
if __name__ != "django.core.management.commands.shell": | ||
logger.error("Please run this script via the dev tool duplicate_pages.sh") | ||
sys.exit() | ||
|
||
# pylint: disable=wrong-import-position | ||
from integreat_cms.cms.models import Region, Page | ||
|
||
|
||
def duplicate_page(old_page, new_parent=None): | ||
""" | ||
Duplicate a page and insert it as child of the given new parent | ||
:param old_page: The old page which should be copied | ||
:type old_page: ~integreat_cms.cms.models.pages.page.Page | ||
:param new_parent: The new parent where the duplicate should reside | ||
:type new_parent: ~integreat_cms.cms.models.pages.page.Page | ||
:return: The copied page | ||
:rtype: ~integreat_cms.cms.models.pages.page.Page | ||
""" | ||
if new_parent: | ||
# Re-query from database to update tree structure | ||
new_parent = Page.objects.get(id=new_parent.id) | ||
logger.debug("Duplicating page %r with new parent %r", old_page, new_parent) | ||
translations = old_page.translations.all() | ||
logger.debug("Old translations: %r", translations) | ||
new_page = copy.deepcopy(old_page) | ||
new_page.id = None | ||
# pylint: disable=protected-access | ||
new_page._state.adding = True | ||
if new_parent: | ||
new_page = new_parent.add_child(instance=new_page) | ||
else: | ||
new_page = Page.add_root(instance=new_page) | ||
# Fix parent field | ||
new_page = Page.objects.get(id=new_page.id) | ||
new_page.save() | ||
for translation in translations: | ||
rand_str = "".join(random.choices(string.printable, k=5)) | ||
new_title = translation.title.split(" - ")[0] + f" - {rand_str}" | ||
translation.id = None | ||
translation.page = new_page | ||
translation.title = new_title | ||
translation.slug = slugify(new_title) | ||
translation.save() | ||
logger.debug("New translations: %r", new_page.translations.all()) | ||
return new_page | ||
|
||
|
||
def duplicate_pages(region, old_parent=None, new_parent=None): | ||
""" | ||
Duplicate pages recursively for the given region | ||
:param region: The given region | ||
:type region: ~integreat_cms.cms.models.regions.region.Region | ||
:param old_parent: The old parent page which descendants should be copied | ||
:type old_parent: ~integreat_cms.cms.models.pages.page.Page | ||
:param new_parent: The new parent where the duplicates should reside | ||
:type new_parent: ~integreat_cms.cms.models.pages.page.Page | ||
""" | ||
logger.info( | ||
"Duplicating pages for old parent %r and new parent %r", | ||
old_parent, | ||
new_parent, | ||
) | ||
for old_page in region.pages.filter(parent=old_parent): | ||
new_page = duplicate_page(old_page, new_parent=new_parent) | ||
duplicate_pages(region, old_parent=old_page, new_parent=new_page) | ||
|
||
|
||
selected_region = Region.objects.first() | ||
logger.info("Duplicating pages for region %r", selected_region) | ||
duplicate_pages(selected_region) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
|
||
# This script duplicates all pages of the first region in the database | ||
|
||
# Import utility functions | ||
# shellcheck source=./dev-tools/_functions.sh | ||
source "$(dirname "${BASH_SOURCE[0]}")/_functions.sh" | ||
|
||
require_installed | ||
require_database | ||
|
||
# Initialize the Redis cache to make sure the cache is invalidated | ||
configure_redis_cache | ||
|
||
deescalate_privileges pipenv run integreat-cms-cli shell --verbosity "${SCRIPT_VERBOSITY}" < "${DEV_TOOL_DIR}/_duplicate_pages.py" | ||
echo "✔ Duplicated pages" | print_success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .json_debug_toolbar_middleware import JsonDebugToolbarMiddleware |
42 changes: 42 additions & 0 deletions
42
integreat_cms/api/middleware/json_debug_toolbar_middleware.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import json | ||
from django.http import HttpResponse | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class JsonDebugToolbarMiddleware: | ||
""" | ||
The Django Debug Toolbar usually only works for views that return HTML. | ||
This middleware wraps any JSON response in HTML if the request | ||
has a 'debug' query parameter (e.g. http://localhost:8000/api/augsburg/de/pages?debug) | ||
""" | ||
|
||
def __init__(self, get_response): | ||
""" | ||
Initialize the middleware for the current view | ||
:param get_response: A callable to get the response for the current request | ||
:type get_response: ~collections.abc.Callable | ||
""" | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
""" | ||
Call the middleware for the current request | ||
:param request: Django request | ||
:type request: ~django.http.HttpRequest | ||
:return: The modified response | ||
:rtype: ~django.http.HttpResponse | ||
""" | ||
response = self.get_response(request) | ||
if "debug" in request.GET: | ||
if response["Content-Type"] == "application/json": | ||
content = json.dumps( | ||
json.loads(response.content), sort_keys=True, indent=2 | ||
) | ||
response = HttpResponse( | ||
f"<!DOCTYPE html><html><body><pre>{content}</pre></body></html>" | ||
) | ||
|
||
return response |
36 changes: 0 additions & 36 deletions
36
integreat_cms/api/tests/expected-outputs/augsburg_de_children.json
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.