Switch to Rendition.filter_spec as the active field for looking up filters

pull/3207/head
Matt Westcott 2016-11-14 18:28:28 +00:00
rodzic cae51db1fc
commit 978cd2e95b
5 zmienionych plików z 68 dodań i 40 usunięć

Wyświetl plik

@ -45,7 +45,7 @@ Here's an example:
class Meta:
unique_together = (
('image', 'filter', 'focal_point_key'),
('image', 'filter_spec', 'focal_point_key'),
)

Wyświetl plik

@ -82,6 +82,29 @@ Bug fixes
Upgrade considerations
======================
``unique_together`` constraint on custom image rendition models needs updating
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If your project is using a custom image model (see :ref:`custom_image_model`), you will need to
update the ``unique_together`` option on the corresponding Rendition model when upgrading to Wagtail 1.8. Change the line:
.. code-block:: python
unique_together = (
('image', 'filter', 'focal_point_key'),
)
to:
.. code-block:: python
unique_together = (
('image', 'filter_spec', 'focal_point_key'),
)
You will then be able to run ``manage.py makemigrations`` and ``manage.py migrate`` as normal.
``wagtail.wagtailimages.models.get_image_model`` has moved
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wyświetl plik

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.11 on 2016-11-11 17:04
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('wagtailimages', '0015_fill_filter_spec_field'),
]
operations = [
migrations.AlterField(
model_name='rendition',
name='filter_spec',
field=models.CharField(db_index=True, max_length=255),
),
migrations.AlterUniqueTogether(
name='rendition',
unique_together=set([('image', 'filter_spec', 'focal_point_key')]),
),
]

Wyświetl plik

@ -14,7 +14,6 @@ from django.core.files import File
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import post_delete, pre_save
from django.db.utils import DatabaseError
from django.dispatch.dispatcher import receiver
from django.forms.widgets import flatatt
from django.utils.encoding import python_2_unicode_compatible
@ -277,7 +276,7 @@ class AbstractImage(CollectionMember, index.Indexed, models.Model):
try:
rendition = self.renditions.get(
filter=filter,
filter_spec=filter.spec,
focal_point_key=cache_key,
)
except Rendition.DoesNotExist:
@ -304,7 +303,7 @@ class AbstractImage(CollectionMember, index.Indexed, models.Model):
output_filename = output_filename_without_extension + '.' + output_extension
rendition, created = self.renditions.get_or_create(
filter=filter,
filter_spec=filter.spec,
focal_point_key=cache_key,
defaults={'file': File(generated_image.f, name=output_filename)}
)
@ -485,8 +484,8 @@ class Filter(models.Model):
class AbstractRendition(models.Model):
filter = models.ForeignKey(Filter, related_name='+', null=True, blank=True)
filter_spec = models.CharField(max_length=255, db_index=True, blank=True, default='')
filter = models.ForeignKey(Filter, related_name='+', null=True, blank=True) # DEPRECATED - RemovedInWagtail19Warning
filter_spec = models.CharField(max_length=255, db_index=True)
file = models.ImageField(upload_to=get_rendition_upload_to, width_field='width', height_field='height')
width = models.IntegerField(editable=False)
height = models.IntegerField(editable=False)
@ -533,39 +532,23 @@ class AbstractRendition(models.Model):
filename = self.file.field.storage.get_valid_name(filename)
return os.path.join(folder_name, filename)
def save(self, *args, **kwargs):
# populate the `filter_spec` field with the spec string of the filter. In Wagtail 1.8
# Filter will be dropped as a model, and lookups will be done based on this string instead
self.filter_spec = self.filter.spec
return super(AbstractRendition, self).save(*args, **kwargs)
@classmethod
def check(cls, **kwargs):
errors = super(AbstractRendition, cls).check(**kwargs)
# If a filter_spec column exists on this model, and contains null entries, warn that
# a data migration needs to be performed to populate it
try:
null_filter_spec_exists = cls.objects.filter(filter_spec='').exists()
except DatabaseError:
# The database is not in a state where the above lookup makes sense;
# this is entirely expected, because system checks are performed before running
# migrations. We're only interested in the specific case where the column exists
# in the db and contains nulls.
null_filter_spec_exists = False
if null_filter_spec_exists:
errors.append(
checks.Warning(
"Custom image model %r needs a data migration to populate filter_src" % cls,
hint="The database representation of image filters has been changed, and a data "
"migration needs to be put in place before upgrading to Wagtail 1.8, in order to "
"avoid data loss. See http://docs.wagtail.io/en/latest/releases/1.7.html#filter-spec-migration",
obj=cls,
id='wagtailimages.W001',
if not cls._meta.abstract:
if not any(
set(constraint) == set(['image', 'filter_spec', 'focal_point_key'])
for constraint in cls._meta.unique_together
):
errors.append(
checks.Error(
"Custom rendition model %r has an invalid unique_together setting" % cls,
hint="Custom rendition models must include the constraint "
"('image', 'filter_spec', 'focal_point_key') in their unique_together definition.",
obj=cls,
id='wagtailimages.E001',
)
)
)
return errors
@ -578,7 +561,7 @@ class Rendition(AbstractRendition):
class Meta:
unique_together = (
('image', 'filter', 'focal_point_key'),
('image', 'filter_spec', 'focal_point_key'),
)

Wyświetl plik

@ -208,10 +208,8 @@ class TestRenditions(TestCase):
self.assertEqual(rendition.height, 300)
# check that the rendition has been recorded under the correct filter,
# via both the Rendition.filter_spec attribute (which will come into action
# in Wagtail 1.8) and the linked Filter model (which will be retired in 1.8)
# via the Rendition.filter_spec attribute (in active use as of Wagtail 1.8)
self.assertEqual(rendition.filter_spec, 'width-400')
self.assertEqual(rendition.filter.spec, 'width-400')
def test_resize_to_max(self):
rendition = self.image.get_rendition('max-100x100')
@ -487,7 +485,7 @@ class TestIssue312(TestCase):
IntegrityError,
Rendition.objects.create,
image=rend1.image,
filter=rend1.filter,
filter_spec=rend1.filter_spec,
width=rend1.width,
height=rend1.height,
focal_point_key=rend1.focal_point_key,