Merge pull request #1258 from SchrodingersGat/part-image-fix

Prevent part images from auto deleting
This commit is contained in:
Oliver 2021-01-28 00:18:44 +11:00 committed by GitHub
commit 14d24ebe07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 20 deletions

View File

@ -203,7 +203,7 @@ INSTALLED_APPS = [
'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
'django_cleanup.apps.CleanupConfig', # Automatically delete orphaned MEDIA files
'qr_code', # Generate QR codes
'mptt', # Modified Preorder Tree Traversal
'markdownx', # Markdown editing

View File

@ -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(

View File

@ -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