Auto-create alias pages after page is completely saved #454
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #346
Fixes: #391
Also provides a better fix for #245
CC: MozillaFoundation/foundation.mozilla.org#7356 @TheoChevalier
When you tried to publish a page that contains new inline panel items, Django would crash with an integrity error.
This behaviour is caused by:
.copy_for_translation
to create the alias pages during thepost_save
signal that was triggered when the source page is published.copy_for_translation
calls.copy_child_relation
in django-modelcluster to copy the inline panel objects to the new alias.copy_child_relation
does not copy the objects, but instead just reassigns them to the new page. This relies on the fact that the page being copied wouldn't be saved again in this request.copy_child_relation
, the.copy_for_translation
then sets all thelocale
fields of the inline panel objects to the target language. But as they are actually the same Python objects as the source page, this inadvertently updates them on the source page as wellpost_save
signal is finished, Django then attempts to save the inline panel objects on the source page. This leads to the integrity error because their locale is set to the target language instead of the source, and there is a unique constraint on that field which raises a conflict with the already saved inline panel objects on the alias page.The workaround to this I have implemented here is to create the alias pages using the
after_create_page
hook instead of thepost_save
signal. This is always run after the many to many objects are already saved avoiding the underlying bug in django-modelcluster.