mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-18 04:01:11 +00:00
set signal handlers for custom document models
This commit is contained in:
parent
0cee606243
commit
49a416431e
4 changed files with 72 additions and 1 deletions
40
wagtail/tests/testapp/migrations/0020_customdocument.py
Normal file
40
wagtail/tests/testapp/migrations/0020_customdocument.py
Normal file
|
|
@ -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),
|
||||
),
|
||||
]
|
||||
|
|
@ -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()),
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
Loading…
Reference in a new issue