summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r--ishtar_common/models.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 6eb36acdb..6cf2846a6 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -1390,6 +1390,16 @@ class DocumentItem(object):
return self.documents.filter(
image__isnull=False).exclude(image="").order_by("pk")
+ @property
+ def images_without_main_image(self):
+ if not hasattr(self, 'documents'):
+ return Document.objects.none()
+ if not hasattr(self, 'main_image'):
+ return self.images
+ return self.documents.filter(
+ image__isnull=False).exclude(
+ image="", pk=self.main_image.pk).order_by("pk")
+
def get_extra_actions(self, request):
"""
For sheet template: return "Add document / image" action
@@ -4042,6 +4052,42 @@ class Document(OwnPerms, ImageModel, FullSearch, Imported):
self.save(no_path_change=True)
+def document_attached_changed(sender, **kwargs):
+ # associate a default main image
+ instance = kwargs.get("instance", None)
+ model = kwargs.get("model", None)
+ pk_set = kwargs.get("pk_set", None)
+ if not instance or not model:
+ return
+
+ if hasattr(instance, "documents"):
+ items = [instance]
+ else:
+ if not pk_set:
+ return
+ try:
+ items = [model.objects.get(pk=pk) for pk in pk_set]
+ except model.DoesNotExist:
+ return
+
+ for item in items:
+ q = item.documents.filter(
+ image__isnull=False).exclude(image='')
+ if item.main_image:
+ if q.filter(pk=item.main_image.pk).count():
+ return
+ # the association has disappear not the main image anymore
+ item.main_image = None
+ item.skip_history_when_saving = True
+ item.save()
+ if not q.count():
+ return
+ # by default get the lowest pk
+ item.main_image = q.order_by('pk').all()[0]
+ item.skip_history_when_saving = True
+ item.save()
+
+
post_save.connect(cached_label_changed, sender=Document)