Skip to content

Commit

Permalink
Set new tree_id when cloning regions (fixes #1259)
Browse files Browse the repository at this point in the history
  • Loading branch information
timobrembeck committed Mar 3, 2022
1 parent c4e8825 commit f708ba1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion integreat_cms/cms/forms/regions/region_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.apps import apps

from ....gvz_api.utils import GvzRegion
from ...models import Region, PageTranslation, LanguageTreeNode
from ...models import Region, Page, PageTranslation, LanguageTreeNode
from ...utils.matomo_api_manager import MatomoException
from ...utils.slug_utils import generate_unique_slug_helper
from ...utils.translation_utils import ugettext_many_lazy as __
Expand Down Expand Up @@ -346,6 +346,17 @@ def duplicate_pages(
logger.debug(
"%s Source page %r started", "| " * (level + 1) + "├" + "─", target_page
)
# If the page is a root page, we need to assign a new tree id
if not target_parent:
last_root_page = Page.get_last_root_node()
override_tree_id = last_root_page.tree_id + 1
logger.debug(
"%s Page is a root page, assigning new tree_id %r",
"| " * (level + 1) + "├" + "─",
override_tree_id,
)
else:
override_tree_id = None
# Store the source page id into a buffer (if we store the whole object instance instead of only the id,
# it will also change when we change target_page, because both variables would reference the same object)
source_page_id = target_page.pk
Expand All @@ -355,6 +366,8 @@ def duplicate_pages(
target_page.region = target_region
# Delete the primary key to duplicate the object instance instead of updating it
target_page.pk = None
# Set new tree id
target_page.tree_id = override_tree_id or target_parent.tree_id
# Check if the page is valid
target_page.full_clean()
# Save duplicated page
Expand Down

0 comments on commit f708ba1

Please sign in to comment.