diff options
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/models.py | 46 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/blocks/window_image.html | 27 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/blocks/window_image_odt.html | 10 | ||||
-rw-r--r-- | ishtar_common/utils_migrations.py | 15 |
4 files changed, 90 insertions, 8 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) 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 %} <div class="lightgallery-captions"> - {% for image in item.images.all %} - <div id="lightgallery-{{window_id}}-caption-{{forloop.counter0}}"> + {% if item.main_image %}{% with image=item.main_image %} + <div id="lightgallery-{{window_id}}-caption-0"> + <span class="close">×</span> + {% include "ishtar/blocks/window_image_detail.html" %} + <a href="{% url 'edit-document' image.pk %}"> + {% trans "Modify" %} <i class="fa fa-pencil"></i> + </a> + </div> + {% endwith %}{% endif %} + {% for image in item.images_without_main_image.all %} + <div id="lightgallery-{{window_id}}-caption-{{forloop.counter}}"> <span class="close">×</span> {% include "ishtar/blocks/window_image_detail.html" %} <a href="{% url 'edit-document' image.pk %}"> @@ -14,10 +23,16 @@ {% endfor %} </div> <div id="lightgallery-{{window_id}}"> - {% for image in item.images.all %} - <a data-sub-html="#lightgallery-{{window_id}}-caption-{{forloop.counter0}}" href="{{image.image.url}}"{% if not forloop.first %} - class="lightgallery-subimage"{% endif %}> - <img{% if forloop.first %} class='card-img-top'{% endif %} src="{{BASE_URL}}{{image.thumbnail.url}}"> + {% if item.main_image %}{% with image=item.main_image %} + <a data-sub-html="#lightgallery-{{window_id}}-caption-0" + href="{{image.image.url}}"> + <img class='card-img-top' src="{{BASE_URL}}{{image.thumbnail.url}}"> + </a> + {% endwith %}{% endif %} + {% for image in item.images_without_main_image.all %} + <a data-sub-html="#lightgallery-{{window_id}}-caption-{{forloop.counter}}" + href="{{image.image.url}}" class="lightgallery-subimage"> + <img src="{{BASE_URL}}{{image.thumbnail.url}}"> </a> {% endfor %} </div> 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 %} <div class="lightgallery-captions"> - {% for image in item.images.all %} - <div id="lightgallery-{{window_id}}-caption-{{forloop.counter0}}"> + {% if item.main_image %}{% with image=item.main_image %} + <div id="lightgallery-{{window_id}}-caption-0"> + <img src="{{BASE_URL}}{{image.thumbnail.url}}"> + {% include "ishtar/blocks/window_image_detail.html" %} + </div> + {% endwith %}{% endif %} + {% for image in item.images_without_main_image.all %} + <div id="lightgallery-{{window_id}}-caption-{{forloop.counter}}"> <img src="{{BASE_URL}}{{image.thumbnail.url}}"> {% include "ishtar/blocks/window_image_detail.html" %} </div> 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() + |