image library migration step 1
This commit is contained in:
parent
07bea333bc
commit
1a2bbb1c35
23 changed files with 413 additions and 57 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue