Look up Filter objects on first call to an image tag, instead of on compilation.

This should make it possible to run ./manage.py compress without a database configured.
Also, the module-level cache of filter objects is removed, which was breaking tests on mysql - the filter objects were persisting beyond the teardown/recreation of the database.
This commit is contained in:
Matt Westcott 2015-03-27 14:12:24 +00:00
parent 3d2a9ce18f
commit a712c79c6b

View file

@ -1,12 +1,10 @@
from django import template
from django.utils.functional import cached_property
from wagtail.wagtailimages.models import Filter, SourceImageIOError
register = template.Library()
# Local cache of filters, avoid hitting the DB
filters = {}
@register.tag(name="image")
def image(parser, token):
@ -37,10 +35,12 @@ class ImageNode(template.Node):
self.image_var = template.Variable(image_var_name)
self.output_var_name = output_var_name
self.attrs = attrs
self.filter_spec = filter_spec
if filter_spec not in filters:
filters[filter_spec], _ = Filter.objects.get_or_create(spec=filter_spec)
self.filter = filters[filter_spec]
@cached_property
def filter(self):
_filter, _ = Filter.objects.get_or_create(spec=self.filter_spec)
return _filter
def render(self, context):
try: