mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-04-11 02:20:59 +00:00
Prevent DB lookups with local cache of filters
This commit is contained in:
parent
2939a5015d
commit
e82eb54101
1 changed files with 6 additions and 1 deletions
|
|
@ -4,6 +4,8 @@ from wagtail.wagtailimages.models import Filter
|
|||
|
||||
register = template.Library()
|
||||
|
||||
# Local cache of filters, avoid hitting the DB
|
||||
filters = {}
|
||||
|
||||
@register.tag(name="image")
|
||||
def image(parser, token):
|
||||
|
|
@ -30,9 +32,12 @@ def image(parser, token):
|
|||
class ImageNode(template.Node):
|
||||
def __init__(self, image_var_name, filter_spec, output_var_name=None):
|
||||
self.image_var = template.Variable(image_var_name)
|
||||
self.filter, created = Filter.objects.get_or_create(spec=filter_spec)
|
||||
self.output_var_name = output_var_name
|
||||
|
||||
if filter_spec not in filters:
|
||||
filters[filter_spec], _ = Filter.objects.get_or_create(spec=filter_spec)
|
||||
self.filter = filters[filter_spec]
|
||||
|
||||
def render(self, context):
|
||||
try:
|
||||
image = self.image_var.resolve(context)
|
||||
|
|
|
|||
Loading…
Reference in a new issue