2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 20:16:44 +00:00

Delete template files from cache as they are uploaded

This commit is contained in:
Oliver 2022-02-18 00:01:49 +11:00
parent 6aec40b9ba
commit 7c64cb950c
2 changed files with 14 additions and 8 deletions

View File

@ -226,11 +226,10 @@ class ReportPrintMixin:
else: else:
outputs.append(report.render(request)) outputs.append(report.render(request))
except TemplateDoesNotExist as e: except TemplateDoesNotExist as e:
template = str(e) template = str(e)
if not template: if not template:
template = report.template template = report.template
return Response( return Response(
{ {
'error': _(f"Template file '{template}' is missing or does not exist"), 'error': _(f"Template file '{template}' is missing or does not exist"),

View File

@ -14,12 +14,12 @@ import datetime
from django.urls import reverse from django.urls import reverse
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import ValidationError, FieldError from django.core.exceptions import ValidationError, FieldError
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.template import Template, Context from django.template import Template, Context
from django.core.files.storage import FileSystemStorage
from django.core.validators import FileExtensionValidator from django.core.validators import FileExtensionValidator
import build.models import build.models
@ -137,17 +137,20 @@ class ReportBase(models.Model):
path = os.path.join('report', 'report_template', self.getSubdir(), filename) path = os.path.join('report', 'report_template', self.getSubdir(), filename)
fullpath = os.path.join(settings.MEDIA_ROOT, path)
fullpath = os.path.abspath(fullpath)
# If the report file is the *same* filename as the one being uploaded, # If the report file is the *same* filename as the one being uploaded,
# remove the original one from the media directory # remove the original one from the media directory
if str(filename) == str(self.template): if str(filename) == str(self.template):
fullpath = os.path.join(settings.MEDIA_ROOT, path)
fullpath = os.path.abspath(fullpath)
if os.path.exists(fullpath): if os.path.exists(fullpath):
logger.info(f"Deleting existing report template: '{filename}'") logger.info(f"Deleting existing report template: '{filename}'")
os.remove(fullpath) os.remove(fullpath)
# Ensure that the cache is cleared for this template!
cache.delete(fullpath)
return path return path
@property @property
@ -515,16 +518,20 @@ def rename_snippet(instance, filename):
path = os.path.join('report', 'snippets', filename) path = os.path.join('report', 'snippets', filename)
fullpath = os.path.join(settings.MEDIA_ROOT, path)
fullpath = os.path.abspath(fullpath)
# If the snippet file is the *same* filename as the one being uploaded, # If the snippet file is the *same* filename as the one being uploaded,
# delete the original one from the media directory # delete the original one from the media directory
if str(filename) == str(instance.snippet): if str(filename) == str(instance.snippet):
fullpath = os.path.join(settings.MEDIA_ROOT, path)
fullpath = os.path.abspath(fullpath)
if os.path.exists(fullpath): if os.path.exists(fullpath):
logger.info(f"Deleting existing snippet file: '{filename}'") logger.info(f"Deleting existing snippet file: '{filename}'")
os.remove(fullpath) os.remove(fullpath)
# Ensure that the cache is deleted for this snippet
cache.delete(fullpath)
return path return path