image library migration step 1
All checks were successful
Build and Deploy / build (push) Successful in 1m7s
Django Tests / test (push) Successful in 1m10s
Build and Deploy / deploy (push) Successful in 6s

This commit is contained in:
Tobias Brunner 2025-07-04 17:26:09 +02:00
parent 07bea333bc
commit 1a2bbb1c35
No known key found for this signature in database
23 changed files with 413 additions and 57 deletions

View file

@ -182,12 +182,13 @@ class ImageReference(models.Model):
This helps track usage and provides a consistent interface.
"""
image = models.ForeignKey(
image_library = models.ForeignKey(
ImageLibrary,
on_delete=models.SET_NULL,
null=True,
blank=True,
help_text="Select an image from the library",
related_name="%(class)s_references",
)
class Meta:
@ -202,23 +203,23 @@ class ImageReference(models.Model):
if self.pk:
try:
old_instance = self.__class__.objects.get(pk=self.pk)
old_image = old_instance.image
old_image = old_instance.image_library
except self.__class__.DoesNotExist:
pass
super().save(*args, **kwargs)
# Update usage counts
if old_image and old_image != self.image:
if old_image and old_image != self.image_library:
old_image.decrement_usage()
if self.image and self.image != old_image:
self.image.increment_usage()
if self.image_library and self.image_library != old_image:
self.image_library.increment_usage()
def delete(self, *args, **kwargs):
"""
Override delete to update usage count.
"""
if self.image:
self.image.decrement_usage()
if self.image_library:
self.image_library.decrement_usage()
super().delete(*args, **kwargs)