2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-21 06:16:29 +00:00

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

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