diff --git a/wagtail/tests/testapp/migrations/0020_customdocument.py b/wagtail/tests/testapp/migrations/0020_customdocument.py new file mode 100644 index 000000000..397b8e264 --- /dev/null +++ b/wagtail/tests/testapp/migrations/0020_customdocument.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.6 on 2017-04-12 18:00 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import taggit.managers +import wagtail.wagtailcore.models +import wagtail.wagtailsearch.index + + +class Migration(migrations.Migration): + + dependencies = [ + ('taggit', '0002_auto_20150616_2121'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('wagtailcore', '0032_add_bulk_delete_page_permission'), + ('tests', '0019_richtextfieldwithfeaturespage'), + ] + + operations = [ + migrations.CreateModel( + name='CustomDocument', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=255, verbose_name='title')), + ('file', models.FileField(upload_to='documents', verbose_name='file')), + ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')), + ('collection', models.ForeignKey(default=wagtail.wagtailcore.models.get_root_collection_id, on_delete=django.db.models.deletion.CASCADE, related_name='+', to='wagtailcore.Collection', verbose_name='collection')), + ('tags', taggit.managers.TaggableManager(blank=True, help_text=None, through='taggit.TaggedItem', to='taggit.Tag', verbose_name='tags')), + ('uploaded_by_user', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL, verbose_name='uploaded by user')), + ], + options={ + 'abstract': False, + 'verbose_name': 'document', + }, + bases=(wagtail.wagtailsearch.index.Indexed, models.Model), + ), + ] diff --git a/wagtail/tests/testapp/models.py b/wagtail/tests/testapp/models.py index dc62f00fd..9a5853251 100644 --- a/wagtail/tests/testapp/models.py +++ b/wagtail/tests/testapp/models.py @@ -30,6 +30,7 @@ from wagtail.wagtailcore.blocks import CharBlock, RichTextBlock from wagtail.wagtailcore.fields import RichTextField, StreamField from wagtail.wagtailcore.models import Orderable, Page, PageManager from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel +from wagtail.wagtaildocs.models import AbstractDocument, Document from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField, AbstractFormSubmission from wagtail.wagtailimages.blocks import ImageChooserBlock from wagtail.wagtailimages.edit_handlers import ImageChooserPanel @@ -673,6 +674,10 @@ class CustomRendition(AbstractRendition): ) +class CustomDocument(AbstractDocument): + admin_form_fields = Document.admin_form_fields + + class StreamModel(models.Model): body = StreamField([ ('text', CharBlock()), diff --git a/wagtail/wagtaildocs/signal_handlers.py b/wagtail/wagtaildocs/signal_handlers.py index 2c69c6802..55969a1cb 100644 --- a/wagtail/wagtaildocs/signal_handlers.py +++ b/wagtail/wagtaildocs/signal_handlers.py @@ -4,7 +4,7 @@ from django import VERSION as DJANGO_VERSION from django.db import transaction from django.db.models.signals import post_delete -from wagtail.wagtaildocs.models import Document +from wagtail.wagtaildocs.models import get_document_model TRANSACTION_ON_COMMIT_AVAILABLE = (1, 8) < DJANGO_VERSION[:2] @@ -22,4 +22,5 @@ def post_delete_file_cleanup(instance, **kwargs): def register_signal_handlers(): + Document = get_document_model() post_delete.connect(post_delete_file_cleanup, sender=Document) diff --git a/wagtail/wagtaildocs/tests/test_models.py b/wagtail/wagtaildocs/tests/test_models.py index d195a3b99..1e03b264d 100644 --- a/wagtail/wagtaildocs/tests/test_models.py +++ b/wagtail/wagtaildocs/tests/test_models.py @@ -7,6 +7,7 @@ from django.contrib.auth.models import Group, Permission from django.core.files.base import ContentFile from django.db import transaction from django.test import TestCase, TransactionTestCase +from django.test.utils import override_settings from wagtail.wagtailcore.models import Collection, GroupCollectionPermission from wagtail.wagtaildocs import models, signal_handlers @@ -152,3 +153,27 @@ class TestFilesDeletedForDefaultModels(TransactionTestCase): self.assertTrue(document.file.storage.exists(document.file.name)) document.delete() self.assertFalse(document.file.storage.exists(document.file.name)) + + +@override_settings(WAGTAILDOCS_DOCUMENT_MODEL='tests.CustomDocument') +class TestFilesDeletedForCustomModels(TestFilesDeletedForDefaultModels): + def setUp(self): + # Required to create root collection because the TransactionTestCase + # does not make initial data loaded in migrations available and + # serialized_rollback=True causes other problems in the test suite. + # ref: https://docs.djangoproject.com/en/1.10/topics/testing/overview/#rollback-emulation + Collection.objects.get_or_create( + name="Root", + path='0001', + depth=1, + numchild=0, + ) + + #: Sadly signal receivers only get connected when starting django. + #: We will re-attach them here to mimic the django startup behavior + #: and get the signals connected to our custom model.. + signal_handlers.register_signal_handlers() + + def test_document_model(self): + cls = get_document_model() + self.assertEqual('%s.%s' % (cls._meta.app_label, cls.__name__), 'tests.CustomDocument')