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

@ -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', [

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