Fix tagged page page copy regression in _copy_m2m_relations (#7224)

Fixes #7230
pull/7253/head
Chris Pollard 2021-05-28 17:29:55 -04:00 zatwierdzone przez Matt Westcott
rodzic 967626f9a2
commit c9a55d8b1b
7 zmienionych plików z 77 dodań i 2 usunięć

Wyświetl plik

@ -17,6 +17,7 @@ Changelog
* Fix: Prevent “Forgotten password” link from overlapping with field on mobile devices (Helen Chapman)
* Fix: Snippet admin urls are now namespaced to avoid ambiguity with the primary key component of the url (Matt Westcott)
* Fix: Save order of promoted search results (Hardcodd)
* Fix: Prevent error on copying pages with ClusterTaggableManager relations and multi-level inheritance (Chris Pollard)
2.13.1 (01.06.2021)

Wyświetl plik

@ -516,6 +516,7 @@ Contributors
* Tidjani Dia
* Jan Seifert
* hardcodd
* Chris Pollard
Translators
===========

Wyświetl plik

@ -28,6 +28,7 @@ Bug fixes
* The Wagtail admin urls will now respect the ``APPEND_SLASH`` setting (Tidjani Dia)
* Prevent “Forgotten password” link from overlapping with field on mobile devices (Helen Chapman)
* Snippet admin urls are now namespaced to avoid ambiguity with the primary key component of the url (Matt Westcott)
* Prevent error on copying pages with ClusterTaggableManager relations and multi-level inheritance (Chris Pollard)
Upgrade considerations
======================

Wyświetl plik

@ -124,7 +124,7 @@ def _copy_m2m_relations(source, target, exclude_fields=None, update_attrs=None):
if field.many_to_many and field.name not in exclude_fields and not field.auto_created and not isinstance(field, ParentalManyToManyField):
try:
# Do not copy m2m links with a through model that has a ParentalKey to the model being copied - these will be copied as child objects
through_model_parental_links = [field for field in field.through._meta.get_fields() if isinstance(field, ParentalKey) and (field.related_model == source.__class__ or field.related_model in source._meta.parents)]
through_model_parental_links = [field for field in field.through._meta.get_fields() if isinstance(field, ParentalKey) and issubclass(source.__class__, field.related_model)]
if through_model_parental_links:
continue
except AttributeError:

Wyświetl plik

@ -28,7 +28,7 @@ from wagtail.tests.testapp.models import (
CustomPageQuerySet, EventCategory, EventIndex, EventPage, EventPageSpeaker, GenericSnippetPage,
ManyToManyBlogPage, MTIBasePage, MTIChildPage, MyCustomPage, OneToOnePage,
PageWithExcludedCopyField, SimpleChildPage, SimplePage, SimpleParentPage, SingleEventPage,
SingletonPage, StandardIndex, StreamPage, TaggedPage)
SingletonPage, StandardIndex, StreamPage, TaggedGrandchildPage, TaggedPage)
from wagtail.tests.utils import WagtailTestUtils
@ -1534,6 +1534,36 @@ class TestCopyPage(TestCase):
for item_id in new_tagged_item_ids
]))
def test_copy_subclassed_page_copies_tags(self):
# create and publish a TaggedGrandchildPage under Events
event_index = Page.objects.get(url_path='/home/events/')
sub_tagged_page = TaggedGrandchildPage(title='My very special tagged page', slug='my-special-tagged-page')
sub_tagged_page.tags.add('wagtail', 'bird')
event_index.add_child(instance=sub_tagged_page)
sub_tagged_page.save_revision().publish()
old_tagged_item_ids = [item.id for item in sub_tagged_page.tagged_items.all()]
# there should be two items here, with defined (truthy) IDs
self.assertEqual(len(old_tagged_item_ids), 2)
self.assertTrue(all(old_tagged_item_ids))
# copy to underneath homepage
homepage = Page.objects.get(url_path='/home/')
new_sub_tagged_page = sub_tagged_page.copy(to=homepage)
self.assertNotEqual(sub_tagged_page.id, new_sub_tagged_page.id)
# new page should also have two tags
new_tagged_item_ids = [item.id for item in new_sub_tagged_page.tagged_items.all()]
self.assertEqual(len(new_tagged_item_ids), 2)
self.assertTrue(all(new_tagged_item_ids))
# new tagged_item IDs should differ from old ones
self.assertTrue(all([
item_id not in old_tagged_item_ids
for item_id in new_tagged_item_ids
]))
def test_copy_page_with_m2m_relations(self):
# create and publish a ManyToManyBlogPage under Events
event_index = Page.objects.get(url_path='/home/events/')

Wyświetl plik

@ -0,0 +1,34 @@
# Generated by Django 3.2.3 on 2021-05-28 21:21
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('tests', '0059_deadlystreampage'),
]
operations = [
migrations.CreateModel(
name='TaggedChildPage',
fields=[
('taggedpage_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='tests.taggedpage')),
],
options={
'abstract': False,
},
bases=('tests.taggedpage',),
),
migrations.CreateModel(
name='TaggedGrandchildPage',
fields=[
('taggedchildpage_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='tests.taggedchildpage')),
],
options={
'abstract': False,
},
bases=('tests.taggedchildpage',),
),
]

Wyświetl plik

@ -889,6 +889,14 @@ class TaggedPage(Page):
]
class TaggedChildPage(TaggedPage):
pass
class TaggedGrandchildPage(TaggedChildPage):
pass
class SingletonPage(Page):
@classmethod
def can_create_at(cls, parent):