InvenTree/InvenTree/company/apps.py
Oliver Walters 0f42916521 Improve settings.py
- Load database config from either config.yaml or environment variables
- Mix and match, if you want!
- Move to use logging module rather than just printing stuff
- Error if required database parameters are not required
2020-11-13 13:38:01 +11:00

47 lines
1.4 KiB
Python

from __future__ import unicode_literals
import os
import logging
from django.apps import AppConfig
from django.db.utils import OperationalError, ProgrammingError
from django.conf import settings
logger = logging.getLogger(__name__)
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
logger.debug("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):
logger.info("InvenTree: Generating thumbnail for Company '{c}'".format(c=company.name))
try:
company.image.render_variations(replace=False)
except FileNotFoundError:
logger.warning("Image file missing")
company.image = None
company.save()
except (OperationalError, ProgrammingError):
# Getting here probably meant the database was in test mode
pass