Add test case for tag field inside InlinePanel

Fixes #5414 - thanks to @johannesvogel for the original test case.
This commit is contained in:
Matt Westcott 2020-01-07 15:12:28 +00:00 committed by Matt Westcott
parent f7530cf5e3
commit ac72dec52a
3 changed files with 153 additions and 2 deletions

View file

@ -14,8 +14,8 @@ from wagtail.admin.tests.pages.timestamps import submittable_timestamp
from wagtail.core.models import GroupPagePermission, Page, PageRevision
from wagtail.core.signals import page_published
from wagtail.tests.testapp.models import (
BusinessChild, BusinessIndex, BusinessSubIndex, DefaultStreamPage, SimplePage,
SingletonPage, SingletonPageViaMaxCount, StandardChild, StandardIndex)
BusinessChild, BusinessIndex, BusinessSubIndex, DefaultStreamPage, PersonPage,
SimplePage, SingletonPage, SingletonPageViaMaxCount, StandardChild, StandardIndex)
from wagtail.tests.utils import WagtailTestUtils
@ -895,3 +895,32 @@ class TestIssue2994(TestCase, WagtailTestUtils):
new_page = DefaultStreamPage.objects.get(slug='issue-2994-test')
self.assertEqual(1, len(new_page.body))
self.assertEqual('hello world', new_page.body[0].value)
class TestInlinePanelWithTags(TestCase, WagtailTestUtils):
# https://github.com/wagtail/wagtail/issues/5414#issuecomment-567080707
def setUp(self):
self.root_page = Page.objects.get(id=2)
self.user = self.login()
def test_create(self):
post_data = {
'title': 'Mr Benn',
'slug': 'mr-benn',
'first_name': 'William',
'last_name': 'Benn',
'addresses-TOTAL_FORMS': 1,
'addresses-INITIAL_FORMS': 0,
'addresses-MIN_NUM_FORMS': 0,
'addresses-MAX_NUM_FORMS': 1000,
'addresses-0-address': "52 Festive Road, London",
'addresses-0-tags': "shopkeeper, bowler-hat",
'action-publish': "Publish",
}
response = self.client.post(
reverse('wagtailadmin_pages:add', args=('tests', 'personpage', self.root_page.id)), post_data
)
self.assertRedirects(response, reverse('wagtailadmin_explore', args=(self.root_page.id, )))
new_page = PersonPage.objects.get(slug='mr-benn')
self.assertEqual(new_page.addresses.first().tags.count(), 2)

View file

@ -0,0 +1,66 @@
# Generated by Django 2.2.6 on 2020-01-07 14:24
from django.db import migrations, models
import django.db.models.deletion
import modelcluster.contrib.taggit
import modelcluster.fields
import wagtail.search.index
class Migration(migrations.Migration):
dependencies = [
('wagtailcore', '0042_index_on_pagerevision_approved_go_live_at'),
('taggit', '0003_taggeditem_add_unique_index'),
('tests', '0045_add_formsubmission_verbose_name_plural'),
]
operations = [
migrations.CreateModel(
name='Address',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('sort_order', models.IntegerField(blank=True, editable=False, null=True)),
('address', models.CharField(max_length=255, verbose_name='Address')),
],
options={
'verbose_name': 'Address',
'verbose_name_plural': 'Addresses',
},
bases=(wagtail.search.index.Indexed, models.Model),
),
migrations.CreateModel(
name='PersonPage',
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')),
('first_name', models.CharField(max_length=255, verbose_name='First Name')),
('last_name', models.CharField(max_length=255, verbose_name='Last Name')),
],
options={
'verbose_name': 'Person',
'verbose_name_plural': 'Persons',
},
bases=('wagtailcore.page',),
),
migrations.CreateModel(
name='AddressTag',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content_object', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='tagged_items', to='tests.Address')),
('tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tests_addresstag_items', to='taggit.Tag')),
],
options={
'abstract': False,
},
),
migrations.AddField(
model_name='address',
name='person',
field=modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='addresses', to='tests.PersonPage', verbose_name='Person'),
),
migrations.AddField(
model_name='address',
name='tags',
field=modelcluster.contrib.taggit.ClusterTaggableManager(blank=True, help_text='A comma-separated list of tags.', through='tests.AddressTag', to='taggit.Tag', verbose_name='Tags'),
),
]

View file

@ -1355,3 +1355,59 @@ class SimpleChildPage(Page):
parent_page_types = ['tests.SimpleParentPage', Page]
max_count_per_parent = 1
class PersonPage(Page):
first_name = models.CharField(
max_length=255,
verbose_name='First Name',
)
last_name = models.CharField(
max_length=255,
verbose_name='Last Name',
)
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('first_name'),
FieldPanel('last_name'),
], 'Person'),
InlinePanel('addresses', label='Address'),
]
class Meta:
verbose_name = 'Person'
verbose_name_plural = 'Persons'
class Address(index.Indexed, ClusterableModel, Orderable):
address = models.CharField(
max_length=255,
verbose_name='Address',
)
tags = ClusterTaggableManager(
through='tests.AddressTag',
blank=True,
)
person = ParentalKey(
to='tests.PersonPage',
related_name='addresses',
verbose_name='Person'
)
panels = [
FieldPanel('address'),
FieldPanel('tags'),
]
class Meta:
verbose_name = 'Address'
verbose_name_plural = 'Addresses'
class AddressTag(TaggedItemBase):
content_object = ParentalKey(
to='tests.Address',
on_delete=models.CASCADE,
related_name='tagged_items'
)