summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-12-20 19:49:52 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2019-01-11 17:30:46 +0100
commit686386b1994aac7df90b12a3ab40000e02e1eda7 (patch)
tree3ae604ef9a36c7edaac48530db753810324e5494 /ishtar_common/models.py
parentfc9b4ecbba8e121e0d811a6d2e6fb64d8b27b475 (diff)
downloadIshtar-686386b1994aac7df90b12a3ab40000e02e1eda7.tar.bz2
Ishtar-686386b1994aac7df90b12a3ab40000e02e1eda7.zip
Manage main image: model, auto associate default image
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)