From 220777611a0d0ee757e97fde5b598a1826e4f725 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 27 Jan 2021 22:31:21 +1100 Subject: [PATCH] Prevent part images from auto deleting - Part images can be used for multiple parts --- InvenTree/InvenTree/settings.py | 34 ++++++++++++++++----------------- InvenTree/part/models.py | 11 +++++++++-- requirements.txt | 2 +- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 107cc7ff16..06908e76bf 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -196,23 +196,23 @@ INSTALLED_APPS = [ 'users.apps.UsersConfig', # Third part add-ons - 'django_filters', # Extended filter functionality - 'dbbackup', # Database backup / restore - 'rest_framework', # DRF (Django Rest Framework) - 'rest_framework.authtoken', # Token authentication for API - 'corsheaders', # Cross-origin Resource Sharing for DRF - 'crispy_forms', # Improved form rendering - 'import_export', # Import / export tables to file - 'django_cleanup', # Automatically delete orphaned MEDIA files - 'qr_code', # Generate QR codes - 'mptt', # Modified Preorder Tree Traversal - 'markdownx', # Markdown editing - 'markdownify', # Markdown template rendering - 'django_tex', # LaTeX output - 'django_admin_shell', # Python shell for the admin interface - 'djmoney', # django-money integration - 'djmoney.contrib.exchange', # django-money exchange rates - 'error_report', # Error reporting in the admin interface + 'django_filters', # Extended filter functionality + 'dbbackup', # Database backup / restore + 'rest_framework', # DRF (Django Rest Framework) + 'rest_framework.authtoken', # Token authentication for API + 'corsheaders', # Cross-origin Resource Sharing for DRF + 'crispy_forms', # Improved form rendering + 'import_export', # Import / export tables to file + 'django_cleanup.apps.CleanupConfig', # Automatically delete orphaned MEDIA files + 'qr_code', # Generate QR codes + 'mptt', # Modified Preorder Tree Traversal + 'markdownx', # Markdown editing + 'markdownify', # Markdown template rendering + 'django_tex', # LaTeX output + 'django_admin_shell', # Python shell for the admin interface + 'djmoney', # django-money integration + 'djmoney.contrib.exchange', # django-money exchange rates + 'error_report', # Error reporting in the admin interface ] MIDDLEWARE = CONFIG.get('middleware', [ diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index f35cc98d39..8ba7ba799d 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -6,6 +6,7 @@ Part database model definitions from __future__ import unicode_literals import os +import logging from django.utils.translation import gettext_lazy as _ from django.core.exceptions import ValidationError @@ -51,6 +52,9 @@ import common.models import part.settings as part_settings +logger = logging.getLogger(__name__) + + class PartCategory(InvenTreeTree): """ PartCategory provides hierarchical organization of Part objects. @@ -335,11 +339,14 @@ class Part(MPTTModel): if self.pk: previous = Part.objects.get(pk=self.pk) - if previous.image and not self.image == previous.image: + # Image has been changed + if previous.image is not None and not self.image == previous.image: + # Are there any (other) parts which reference the image? n_refs = Part.objects.filter(image=previous.image).exclude(pk=self.pk).count() if n_refs == 0: + logger.info(f"Deleting unused image file '{previous.image}'") previous.image.delete(save=False) self.clean() @@ -710,7 +717,7 @@ class Part(MPTTModel): null=True, blank=True, variations={'thumbnail': (128, 128)}, - delete_orphans=True, + delete_orphans=False, ) default_location = TreeForeignKey( diff --git a/requirements.txt b/requirements.txt index 76610600a6..e4ffe6be75 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,7 +15,7 @@ pygments==2.2.0 # Syntax highlighting tablib==0.13.0 # Import / export data files django-crispy-forms==1.8.1 # Form helpers django-import-export==2.0.0 # Data import / export for admin interface -django-cleanup==4.0.0 # Manage deletion of old / unused uploaded files +django-cleanup==5.1.0 # Manage deletion of old / unused uploaded files django-qr-code==1.2.0 # Generate QR codes flake8==3.8.3 # PEP checking pep8-naming==0.11.1 # PEP naming convention extension