use the saved pdf as a cache (#84288)
gitea/barbacompta/pipeline/head This commit looks good Details

Digest of the HTML content is used for versionning.
This commit is contained in:
Benjamin Dauvergne 2024-01-13 15:56:31 +01:00
parent ea330151e9
commit 386703b10b
1 changed files with 18 additions and 4 deletions

View File

@ -16,6 +16,8 @@
import datetime
import functools
import hashlib
import itertools
import os.path
from collections import defaultdict
@ -582,13 +584,25 @@ class Facture(models.Model):
return template.render({'facture': self, 'base_uri': base_uri or ''})
def pdf(self, template_name=None, base_uri=None):
html = HTML(string=self.html(template_name=template_name, base_uri=base_uri))
html_content = self.html(template_name=template_name, base_uri=base_uri)
digest = hashlib.sha256(html_content.encode()).hexdigest()[:6]
filename = digest + '-' + self.filename_with_client()
cache_filepath = None
if hasattr(settings, 'FACTURE_DIR'):
cache_filepath = os.path.join(settings.FACTURE_DIR, self.filename_with_client())
if cache_filepath and os.path.exists(cache_filepath):
with open(cache_filepath, 'rb') as fd:
return fd.read()
html = HTML(string=html_content)
pdf = html.write_pdf()
if hasattr(settings, 'FACTURE_DIR'):
filename = os.path.join(settings.FACTURE_DIR, self.filename_with_client())
with open(filename, 'wb') as fd:
if cache_filepath:
with open(cache_filepath, 'wb') as fd:
fd.write(pdf)
return pdf
def facturx_pdf(self, template_name=None, base_uri=None):