Skip to content
Snippets Groups Projects
Commit 24859102 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Cache QR codes image generation

The generation of the QR code image is quite slow and makes the page
displaying them take a long time to load when it includes more a few.
The data never changes and can easily be cached (the repr() of the class
is used in the cache key, so if the name or id changes, the cache will
be recomputed - there is no need for a timeout).
parent a9fe8bec
No related branches found
No related tags found
No related merge requests found
......@@ -146,7 +146,7 @@ def qrcodes(kind='Action'):
raise utils.CSEntryError(f"Unknown model '{kind}'", status_code=422)
items = db.session.query(model).order_by(model.name)
images = [{'name': item.name,
'data': utils.image_to_base64(item.image())}
'data': item.base64_image()}
for item in items]
return render_template('inventory/qrcodes.html', kind=kind, images=images)
......
......@@ -22,7 +22,7 @@ from citext import CIText
from flask import current_app
from flask_login import UserMixin
from wtforms import ValidationError
from .extensions import db, login_manager, ldap_manager
from .extensions import db, login_manager, ldap_manager, cache
from .plugins import FlaskUserPlugin
from .validators import ICS_ID_RE, HOST_NAME_RE, VLAN_NAME_RE
from . import utils
......@@ -209,14 +209,25 @@ class QRCodeMixin:
data = ':'.join(['CSE', self.__tablename__, self.name])
return qrcode.make(data, version=1, box_size=5)
@cache.memoize(timeout=0)
def base64_image(self):
"""Return the QRCode image as base64 string"""
return utils.image_to_base64(self.image())
def __str__(self):
return self.name
def __repr__(self):
# The cache.memoize decorator performs a repr() on the passed in arguments
# __repr__ is used as part of the cache key and shall be a uniquely identifying string
# See https://flask-caching.readthedocs.io/en/latest/#memoization
return f'{self.__class__.__name__}(id={self.id}, name={self.name})'
def to_dict(self):
return {
'id': self.id,
'name': self.name,
'qrcode': utils.image_to_base64(self.image()),
'qrcode': self.base64_image(),
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment