From 330215cf864ad3a917d51d6be15d68f360c166dc Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 20 Dec 2018 19:49:52 +0100 Subject: Manage main image: model, auto associate default image --- ishtar_common/models.py | 46 ++++++++++++++++++++++ .../templates/ishtar/blocks/window_image.html | 27 ++++++++++--- .../templates/ishtar/blocks/window_image_odt.html | 10 ++++- ishtar_common/utils_migrations.py | 15 +++++++ 4 files changed, 90 insertions(+), 8 deletions(-) (limited to 'ishtar_common') 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) diff --git a/ishtar_common/templates/ishtar/blocks/window_image.html b/ishtar_common/templates/ishtar/blocks/window_image.html index ab1013df9..62623e89d 100644 --- a/ishtar_common/templates/ishtar/blocks/window_image.html +++ b/ishtar_common/templates/ishtar/blocks/window_image.html @@ -3,8 +3,17 @@ {% include "ishtar/blocks/window_image_odt.html" %} {% else %}
- {% for image in item.images.all %} -
+ {% if item.main_image %}{% with image=item.main_image %} +
+ × + {% include "ishtar/blocks/window_image_detail.html" %} + + {% trans "Modify" %}   + +
+ {% endwith %}{% endif %} + {% for image in item.images_without_main_image.all %} +
× {% include "ishtar/blocks/window_image_detail.html" %} @@ -14,10 +23,16 @@ {% endfor %}
- {% for image in item.images.all %} - - + {% if item.main_image %}{% with image=item.main_image %} + + + + {% endwith %}{% endif %} + {% for image in item.images_without_main_image.all %} + + {% endfor %}
diff --git a/ishtar_common/templates/ishtar/blocks/window_image_odt.html b/ishtar_common/templates/ishtar/blocks/window_image_odt.html index 9c9383cdd..548d0b46f 100644 --- a/ishtar_common/templates/ishtar/blocks/window_image_odt.html +++ b/ishtar_common/templates/ishtar/blocks/window_image_odt.html @@ -1,7 +1,13 @@ {% load i18n %}
- {% for image in item.images.all %} -
+ {% if item.main_image %}{% with image=item.main_image %} +
+ + {% include "ishtar/blocks/window_image_detail.html" %} +
+ {% endwith %}{% endif %} + {% for image in item.images_without_main_image.all %} +
{% include "ishtar/blocks/window_image_detail.html" %}
diff --git a/ishtar_common/utils_migrations.py b/ishtar_common/utils_migrations.py index 40cdcb5cd..d161c1f29 100644 --- a/ishtar_common/utils_migrations.py +++ b/ishtar_common/utils_migrations.py @@ -97,3 +97,18 @@ def reinit_last_modified(apps, app_name, models): item.skip_history_when_saving = True item.save() + +def migrate_main_image(apps, app_name, model_name): + model = apps.get_model(app_name, model_name) + for item in model.objects.filter( + documents__image__isnull=False).exclude( + main_image__isnull=False).all(): + q = item.documents.filter( + image__isnull=False).exclude(image='') + 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() + -- cgit v1.2.3