From e0655f61d8c2ff1ffabcfb75e0e6837a739ba1cc Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 7 Apr 2020 11:23:30 +1000 Subject: [PATCH] Check if Company thumbnails are created on Company app start --- InvenTree/company/apps.py | 31 ++++++++++++++++++++++++++++++- InvenTree/company/models.py | 2 +- InvenTree/part/apps.py | 3 ++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/InvenTree/company/apps.py b/InvenTree/company/apps.py index c55451e882..25f3bd96dc 100644 --- a/InvenTree/company/apps.py +++ b/InvenTree/company/apps.py @@ -1,7 +1,36 @@ from __future__ import unicode_literals -from django.apps import AppConfig +import os +from django.apps import AppConfig +from django.db.utils import OperationalError, ProgrammingError +from django.conf import settings class CompanyConfig(AppConfig): name = 'company' + + def ready(self): + """ + This function is called whenever the Company app is loaded. + """ + + self.generate_company_thumbs() + + def generate_company_thumbs(self): + + from .models import Company + + print("InvenTree: Checking Company image thumbnails") + + try: + for company in Company.objects.all(): + if company.image: + url = company.image.thumbnail.name + loc = os.path.join(settings.MEDIA_ROOT, url) + + if not os.path.exists(loc): + print("InvenTree: Generating thumbnail for Company '{c}'".format(c=company.name)) + company.image.render_variations(replace=False) + except (OperationalError, ProgrammingError): + print("Could not generate Company thumbnails") + \ No newline at end of file diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index a7dd2e53e8..59a6a2b37a 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -96,7 +96,7 @@ class Company(models.Model): upload_to=rename_company_image, null=True, blank=True, - variations={'thumnbnail': (128, 128)}, + variations={'thumbnail': (128, 128)}, delete_orphans=True, ) diff --git a/InvenTree/part/apps.py b/InvenTree/part/apps.py index 7b61443d24..95193a9527 100644 --- a/InvenTree/part/apps.py +++ b/InvenTree/part/apps.py @@ -20,13 +20,14 @@ class PartConfig(AppConfig): def generate_part_thumbnails(self): from .models import Part - print("Checking Part image thumbnails") + print("InvenTree: Checking Part image thumbnails") try: for part in Part.objects.all(): if part.image: url = part.image.thumbnail.name loc = os.path.join(settings.MEDIA_ROOT, url) + if not os.path.exists(loc): print("InvenTree: Generating thumbnail for Part '{p}'".format(p=part.name)) part.image.render_variations(replace=False)