Prevent part images from auto deleting

- Part images can be used for multiple parts
This commit is contained in:
Oliver Walters 2021-01-27 22:31:21 +11:00
parent b90311acea
commit 220777611a
3 changed files with 27 additions and 20 deletions

View File

@ -203,7 +203,7 @@ INSTALLED_APPS = [
'corsheaders', # Cross-origin Resource Sharing for DRF 'corsheaders', # Cross-origin Resource Sharing for DRF
'crispy_forms', # Improved form rendering 'crispy_forms', # Improved form rendering
'import_export', # Import / export tables to file 'import_export', # Import / export tables to file
'django_cleanup', # Automatically delete orphaned MEDIA files 'django_cleanup.apps.CleanupConfig', # Automatically delete orphaned MEDIA files
'qr_code', # Generate QR codes 'qr_code', # Generate QR codes
'mptt', # Modified Preorder Tree Traversal 'mptt', # Modified Preorder Tree Traversal
'markdownx', # Markdown editing 'markdownx', # Markdown editing

View File

@ -6,6 +6,7 @@ Part database model definitions
from __future__ import unicode_literals from __future__ import unicode_literals
import os import os
import logging
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -51,6 +52,9 @@ import common.models
import part.settings as part_settings import part.settings as part_settings
logger = logging.getLogger(__name__)
class PartCategory(InvenTreeTree): class PartCategory(InvenTreeTree):
""" PartCategory provides hierarchical organization of Part objects. """ PartCategory provides hierarchical organization of Part objects.
@ -335,11 +339,14 @@ class Part(MPTTModel):
if self.pk: if self.pk:
previous = Part.objects.get(pk=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? # Are there any (other) parts which reference the image?
n_refs = Part.objects.filter(image=previous.image).exclude(pk=self.pk).count() n_refs = Part.objects.filter(image=previous.image).exclude(pk=self.pk).count()
if n_refs == 0: if n_refs == 0:
logger.info(f"Deleting unused image file '{previous.image}'")
previous.image.delete(save=False) previous.image.delete(save=False)
self.clean() self.clean()
@ -710,7 +717,7 @@ class Part(MPTTModel):
null=True, null=True,
blank=True, blank=True,
variations={'thumbnail': (128, 128)}, variations={'thumbnail': (128, 128)},
delete_orphans=True, delete_orphans=False,
) )
default_location = TreeForeignKey( default_location = TreeForeignKey(

View File

@ -15,7 +15,7 @@ pygments==2.2.0 # Syntax highlighting
tablib==0.13.0 # Import / export data files tablib==0.13.0 # Import / export data files
django-crispy-forms==1.8.1 # Form helpers django-crispy-forms==1.8.1 # Form helpers
django-import-export==2.0.0 # Data import / export for admin interface 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 django-qr-code==1.2.0 # Generate QR codes
flake8==3.8.3 # PEP checking flake8==3.8.3 # PEP checking
pep8-naming==0.11.1 # PEP naming convention extension pep8-naming==0.11.1 # PEP naming convention extension