infoportugal / wagtail-modeltranslation

Simple app to patch modeltranslation (https://github.com/deschler/django-modeltranslation) into Wagtail CMS.
151 stars 119 forks source link

FIX Renaming a parent page breaks the path to children ones #428

Closed mbayane closed 3 months ago

mbayane commented 4 months ago

FIX Renaming a parent page breaks the path to children ones ( When a parent slug page update the children path breaks , adding or delete charactere depending the difference between len(new_path_url) and len(old_path_url)

DiogoMarques29 commented 4 months ago

Hey @mbayane,

Thanks for the PR.

I noticed that this change might introduce a performance issue because we are executing a save query for each descendant. Depending on the size of the tree, this could lead to timeout issues.

Could you please refactor the code so that the update happens in a single query? Here’s a suggested approach:

descendants = Page.objects.rewrite(False).filter(path__startswith=page.path).exclude(
    **{localized_url_path: None}).exclude(pk=page.pk)

update_descendants = []
for descendant in descendants:
    old_descendant_url_path = getattr(descendant, localized_url_path)
    if old_descendant_url_path.startswith(old_url_path):
        new_descendant_url_path = new_url_path + old_descendant_url_path[len(old_url_path):]
        setattr(descendant, localized_url_path, new_descendant_url_path)
        update_descendants.append(descendant)

# Use bulk update for performance
Page.objects.bulk_update(update_descendants, [localized_url_path])

This should help maintain performance by reducing the number of database queries.

Thanks again for your contribution!

mbayane commented 4 months ago

update_descendants

Hello @DiogoMarques29,

Thanks for your reply and suggestions.

i will update it shortly

Regards

mbayane commented 4 months ago

Hello @DiogoMarques29,

Changes pushed

Thanks

DiogoMarques29 commented 3 months ago

Had to close and open PR so that the checks run correctly.