diff options
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 |
commit | 686386b1994aac7df90b12a3ab40000e02e1eda7 (patch) | |
tree | 3ae604ef9a36c7edaac48530db753810324e5494 | |
parent | fc9b4ecbba8e121e0d811a6d2e6fb64d8b27b475 (diff) | |
download | Ishtar-686386b1994aac7df90b12a3ab40000e02e1eda7.tar.bz2 Ishtar-686386b1994aac7df90b12a3ab40000e02e1eda7.zip |
Manage main image: model, auto associate default image
18 files changed, 398 insertions, 20 deletions
diff --git a/archaeological_context_records/migrations/0034_auto_20181220_1539.py b/archaeological_context_records/migrations/0034_auto_20181220_1539.py new file mode 100644 index 000000000..9574a5d6e --- /dev/null +++ b/archaeological_context_records/migrations/0034_auto_20181220_1539.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-12-20 15:39 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0079_migrate-importers'), + ('archaeological_context_records', '0033_auto_20181203_1442'), + ] + + operations = [ + migrations.AddField( + model_name='contextrecord', + name='main_image', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='main_image_context_records', to='ishtar_common.Document', verbose_name='Main image'), + ), + migrations.AddField( + model_name='historicalcontextrecord', + name='main_image', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.Document'), + ), + ] diff --git a/archaeological_context_records/migrations/0035_migrate_main_image.py b/archaeological_context_records/migrations/0035_migrate_main_image.py new file mode 100644 index 000000000..ba1adb074 --- /dev/null +++ b/archaeological_context_records/migrations/0035_migrate_main_image.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-12-20 19:12 +from __future__ import unicode_literals + +from django.db import migrations + +from ishtar_common.utils_migrations import migrate_main_image + + +def migrate_main_image_script(apps, schema): + migrate_main_image(apps, 'archaeological_context_records', 'ContextRecord') + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_context_records', '0034_auto_20181220_1539'), + ] + + operations = [ + migrations.RunPython(migrate_main_image_script) + ] diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index d7d7a618d..82527acb1 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -24,7 +24,7 @@ from django.contrib.gis.db import models from django.core.urlresolvers import reverse from django.db import connection, transaction from django.db.models import Q -from django.db.models.signals import post_delete, post_save +from django.db.models.signals import post_delete, post_save, m2m_changed from django.utils.translation import ugettext_lazy as _, pgettext, \ activate, pgettext_lazy, deactivate from django.utils.text import slugify @@ -35,7 +35,7 @@ from ishtar_common.models import Document, GeneralType, \ BaseHistorizedItem, HistoricalRecords, OwnPerms, ShortMenuItem, \ GeneralRelationType, GeneralRecordRelations, post_delete_record_relation,\ post_save_cache, ValueGetter, BulkUpdatedItem, ExternalIdManager, \ - RelationItem, Town, get_current_profile + RelationItem, Town, get_current_profile, document_attached_changed from archaeological_operations.models import Operation, Period, Parcel, \ ArchaeologicalSite @@ -429,6 +429,9 @@ class ContextRecord(BulkUpdatedItem, BaseHistorizedItem, documents = models.ManyToManyField( Document, related_name='context_records', verbose_name=_(u"Documents"), blank=True) + main_image = models.ForeignKey( + Document, related_name='main_image_context_records', + verbose_name=_(u"Main image"), blank=True, null=True) cached_label = models.TextField(_(u"Cached name"), null=True, blank=True, db_index=True) @@ -681,6 +684,8 @@ class ContextRecord(BulkUpdatedItem, BaseHistorizedItem, post_save.connect(cached_label_changed, sender=ContextRecord) +m2m_changed.connect(document_attached_changed, + sender=ContextRecord.documents.through) class RelationType(GeneralRelationType): diff --git a/archaeological_finds/migrations/0053_auto_20181220_1539.py b/archaeological_finds/migrations/0053_auto_20181220_1539.py new file mode 100644 index 000000000..a17fb75eb --- /dev/null +++ b/archaeological_finds/migrations/0053_auto_20181220_1539.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-12-20 15:39 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0079_migrate-importers'), + ('archaeological_finds', '0052_auto_20181211_1558'), + ] + + operations = [ + migrations.AddField( + model_name='find', + name='main_image', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='main_image_finds', to='ishtar_common.Document', verbose_name='Main image'), + ), + migrations.AddField( + model_name='historicalfind', + name='main_image', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.Document'), + ), + migrations.AddField( + model_name='historicaltreatment', + name='main_image', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.Document'), + ), + migrations.AddField( + model_name='historicaltreatmentfile', + name='main_image', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.Document'), + ), + migrations.AddField( + model_name='treatment', + name='main_image', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='main_image_treatments', to='ishtar_common.Document', verbose_name='Main image'), + ), + migrations.AddField( + model_name='treatmentfile', + name='main_image', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='main_image_treatment_files', to='ishtar_common.Document', verbose_name='Main image'), + ), + ] diff --git a/archaeological_finds/migrations/0054_migrate_main_image.py b/archaeological_finds/migrations/0054_migrate_main_image.py new file mode 100644 index 000000000..1a7f942c4 --- /dev/null +++ b/archaeological_finds/migrations/0054_migrate_main_image.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-12-20 19:12 +from __future__ import unicode_literals + +from django.db import migrations +from ishtar_common.utils_migrations import migrate_main_image + + +def migrate_main_image_script(apps, schema): + migrate_main_image(apps, 'archaeological_finds', 'Find') + migrate_main_image(apps, 'archaeological_finds', 'Treatment') + migrate_main_image(apps, 'archaeological_finds', 'TreatmentFile') + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_finds', '0053_auto_20181220_1539'), + ] + + operations = [ + migrations.RunPython(migrate_main_image_script) + ] diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index 393583749..33ec2df0b 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -40,7 +40,7 @@ from ishtar_common.models import Document, GeneralType, \ HistoricalRecords, OwnPerms, Person, Basket, post_save_cache, \ ValueGetter, get_current_profile, IshtarSiteProfile, PRIVATE_FIELDS, \ SpatialReferenceSystem, BulkUpdatedItem, ExternalIdManager, QuickAction, \ - MainItem + MainItem, document_attached_changed from archaeological_operations.models import AdministrativeAct, Operation from archaeological_context_records.models import ContextRecord, Dating @@ -1179,6 +1179,9 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, OwnPerms, documents = models.ManyToManyField( Document, related_name='finds', verbose_name=_(u"Documents"), blank=True) + main_image = models.ForeignKey( + Document, related_name='main_image_finds', + verbose_name=_(u"Main image"), blank=True, null=True) treatments = models.ManyToManyField( "Treatment", verbose_name=_(u"Treatments"), related_name='finds', blank=True, @@ -1954,6 +1957,9 @@ def base_find_find_changed(sender, **kwargs): m2m_changed.connect(base_find_find_changed, sender=Find.base_finds.through) +m2m_changed.connect(document_attached_changed, + sender=Find.documents.through) + class Property(LightHistorizedItem): find = models.ForeignKey(Find, verbose_name=_(u"Find")) diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py index 06f61068b..64bad203d 100644 --- a/archaeological_finds/models_treatments.py +++ b/archaeological_finds/models_treatments.py @@ -23,7 +23,8 @@ from django.conf import settings from django.contrib.gis.db import models from django.core.urlresolvers import reverse from django.db.models import Max, Q -from django.db.models.signals import post_save, post_delete, pre_delete +from django.db.models.signals import post_save, post_delete, pre_delete, \ + m2m_changed from django.template.defaultfilters import slugify from django.utils.translation import ugettext_lazy as _, pgettext_lazy, \ activate, deactivate @@ -35,7 +36,7 @@ from archaeological_warehouse.models import Warehouse, Container from ishtar_common.models import Document, GeneralType, \ ImageModel, BaseHistorizedItem, OwnPerms, HistoricalRecords, Person, \ Organization, ValueGetter, post_save_cache, ShortMenuItem, \ - DashboardFormItem, ExternalIdManager + DashboardFormItem, ExternalIdManager, document_attached_changed from ishtar_common.utils import cached_label_changed, get_current_year, \ update_data @@ -179,6 +180,9 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, documents = models.ManyToManyField( Document, related_name='treatments', verbose_name=_(u"Documents"), blank=True) + main_image = models.ForeignKey( + Document, related_name='main_image_treatments', + verbose_name=_(u"Main image"), blank=True, null=True) cached_label = models.TextField(_(u"Cached name"), null=True, blank=True, db_index=True) history = HistoricalRecords() @@ -579,6 +583,9 @@ def pre_delete_treatment(sender, **kwargs): pre_delete.connect(pre_delete_treatment, sender=Treatment) +m2m_changed.connect(document_attached_changed, + sender=Treatment.documents.through) + class AbsFindTreatments(models.Model): find = models.ForeignKey(Find, verbose_name=_(u"Find"), @@ -847,6 +854,9 @@ class TreatmentFile(DashboardFormItem, ClosedItem, BaseHistorizedItem, documents = models.ManyToManyField( Document, related_name='treatment_files', verbose_name=_(u"Documents"), blank=True) + main_image = models.ForeignKey( + Document, related_name='main_image_treatment_files', + verbose_name=_(u"Main image"), blank=True, null=True) associated_basket = models.ForeignKey( FindBasket, null=True, blank=True, on_delete=models.SET_NULL, related_name='treatment_files' @@ -956,4 +966,8 @@ class TreatmentFile(DashboardFormItem, ClosedItem, BaseHistorizedItem, super(TreatmentFile, self).save(*args, **kwargs) + +m2m_changed.connect(document_attached_changed, + sender=TreatmentFile.documents.through) + post_save.connect(cached_label_changed, sender=TreatmentFile) diff --git a/archaeological_finds/templates/ishtar/sheet_find.html b/archaeological_finds/templates/ishtar/sheet_find.html index 7171c3deb..cfbefa306 100644 --- a/archaeological_finds/templates/ishtar/sheet_find.html +++ b/archaeological_finds/templates/ishtar/sheet_find.html @@ -81,8 +81,8 @@ <div class="tab-pane fade show active" id="{{window_id}}-basefind" role="tabpanel" aria-labelledby="{{window_id}}-basefind-tab"> - {% with nb_image=item.images.count %} - {% if nb_image %} + {% with has_image=item.main_image %} + {% if has_image %} <div class="clearfix"> <div class="card float-left col-12 col-md-6 col-lg-4"> {% include "ishtar/blocks/window_image.html" %} @@ -105,12 +105,12 @@ <div class="tab-content"> {% for base_find in item.base_finds.all %} - {% with first=forloop.first|add:nb_image %} + {% with first=forloop.first|add:has_image %} {% include "ishtar/sheet_basefind.html" %} {% endwith %} {% endfor %} </div> - {% if nb_image %} + {% if has_image %} </div> {% endif %} {% endwith %} diff --git a/archaeological_operations/migrations/0042_auto_20181220_1539.py b/archaeological_operations/migrations/0042_auto_20181220_1539.py new file mode 100644 index 000000000..a9d9c786b --- /dev/null +++ b/archaeological_operations/migrations/0042_auto_20181220_1539.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-12-20 15:39 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0079_migrate-importers'), + ('archaeological_operations', '0041_auto_20181203_1442'), + ] + + operations = [ + migrations.AddField( + model_name='archaeologicalsite', + name='main_image', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='main_image_sites', to='ishtar_common.Document', verbose_name='Main image'), + ), + migrations.AddField( + model_name='historicalarchaeologicalsite', + name='main_image', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.Document'), + ), + migrations.AddField( + model_name='historicaloperation', + name='main_image', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='ishtar_common.Document'), + ), + migrations.AddField( + model_name='operation', + name='main_image', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='main_image_operations', to='ishtar_common.Document', verbose_name='Main image'), + ), + migrations.AlterField( + model_name='archaeologicalsite', + name='affmar_number', + field=models.CharField(blank=True, max_length=100, null=True, verbose_name='AffMar number'), + ), + migrations.AlterField( + model_name='archaeologicalsite', + name='drassm_number', + field=models.CharField(blank=True, max_length=100, null=True, verbose_name='DRASSM number'), + ), + migrations.AlterField( + model_name='historicalarchaeologicalsite', + name='affmar_number', + field=models.CharField(blank=True, max_length=100, null=True, verbose_name='AffMar number'), + ), + migrations.AlterField( + model_name='historicalarchaeologicalsite', + name='drassm_number', + field=models.CharField(blank=True, max_length=100, null=True, verbose_name='DRASSM number'), + ), + migrations.AlterField( + model_name='historicaloperation', + name='drassm_code', + field=models.CharField(blank=True, max_length=100, null=True, verbose_name='DRASSM code'), + ), + migrations.AlterField( + model_name='operation', + name='drassm_code', + field=models.CharField(blank=True, max_length=100, null=True, verbose_name='DRASSM code'), + ), + ] diff --git a/archaeological_operations/migrations/0043_migrate_main_image.py b/archaeological_operations/migrations/0043_migrate_main_image.py new file mode 100644 index 000000000..a8eb7b969 --- /dev/null +++ b/archaeological_operations/migrations/0043_migrate_main_image.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-12-20 19:12 +from __future__ import unicode_literals + +from django.db import migrations +from ishtar_common.utils_migrations import migrate_main_image + + +def migrate_main_image_script(apps, schema): + migrate_main_image(apps, 'archaeological_operations', 'Operation') + migrate_main_image(apps, 'archaeological_operations', 'ArchaeologicalSite') + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_operations', '0042_auto_20181220_1539'), + ] + + operations = [ + migrations.RunPython(migrate_main_image_script) + ] diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index d8233ba0f..0b506e12b 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -36,7 +36,8 @@ from ishtar_common.models import BaseHistorizedItem, Dashboard, \ HistoricalRecords, IshtarUser, LightHistorizedItem, \ OperationType, Organization, OwnPerms, Person, PersonType, \ post_delete_record_relation, post_save_cache, RelationItem, \ - ShortMenuItem, SourceType, Town, ValueGetter, get_current_profile + ShortMenuItem, SourceType, Town, ValueGetter, get_current_profile, \ + document_attached_changed from ishtar_common.utils import cached_label_changed, \ force_cached_label_changed, mode @@ -267,6 +268,9 @@ class ArchaeologicalSite(BaseHistorizedItem, OwnPerms, ValueGetter, documents = models.ManyToManyField( Document, related_name="sites", verbose_name=_(u"Documents"), blank=True) + main_image = models.ForeignKey( + Document, related_name='main_image_sites', + verbose_name=_(u"Main image"), blank=True, null=True) cached_label = models.TextField(_(u"Cached name"), null=True, blank=True, db_index=True) @@ -420,6 +424,9 @@ class ArchaeologicalSite(BaseHistorizedItem, OwnPerms, ValueGetter, post_save.connect(cached_label_changed, sender=ArchaeologicalSite) +m2m_changed.connect(document_attached_changed, + sender=ArchaeologicalSite.documents.through) + def get_values_town_related(item, prefix, values): values[prefix + 'parcellist'] = item.render_parcels() @@ -845,6 +852,9 @@ class Operation(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter, documents = models.ManyToManyField( Document, related_name='operations', verbose_name=_(u"Documents"), blank=True) + main_image = models.ForeignKey( + Document, related_name='main_image_operations', + verbose_name=_(u"Main image"), blank=True, null=True) cached_label = models.CharField(_(u"Cached name"), max_length=500, null=True, blank=True, db_index=True) archaeological_sites = models.ManyToManyField( @@ -1386,6 +1396,8 @@ class Operation(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter, m2m_changed.connect(force_cached_label_changed, sender=Operation.towns.through) +m2m_changed.connect(document_attached_changed, + sender=Operation.documents.through) def operation_post_save(sender, **kwargs): diff --git a/archaeological_warehouse/migrations/0027_warehouse_main_image.py b/archaeological_warehouse/migrations/0027_warehouse_main_image.py new file mode 100644 index 000000000..fc1dc13b7 --- /dev/null +++ b/archaeological_warehouse/migrations/0027_warehouse_main_image.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-12-20 15:39 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0079_migrate-importers'), + ('archaeological_warehouse', '0026_auto_20181203_1442'), + ] + + operations = [ + migrations.AddField( + model_name='warehouse', + name='main_image', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='main_image_warehouses', to='ishtar_common.Document', verbose_name='Main image'), + ), + ] diff --git a/archaeological_warehouse/migrations/0028_migrate_main_image.py b/archaeological_warehouse/migrations/0028_migrate_main_image.py new file mode 100644 index 000000000..4cb6cda7e --- /dev/null +++ b/archaeological_warehouse/migrations/0028_migrate_main_image.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-12-20 19:12 +from __future__ import unicode_literals + +from django.db import migrations +from ishtar_common.utils_migrations import migrate_main_image + + +def migrate_main_image_script(apps, schema): + migrate_main_image(apps, 'archaeological_warehouse', 'Warehouse') + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_warehouse', '0027_warehouse_main_image'), + ] + + operations = [ + migrations.RunPython(migrate_main_image_script) + ] diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 92eaf5d2f..3b157ee0c 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -23,7 +23,7 @@ from django.conf import settings from django.contrib.gis.db import models from django.core.urlresolvers import reverse from django.db.models import Q -from django.db.models.signals import post_save, post_delete +from django.db.models.signals import post_save, post_delete, m2m_changed from django.template.defaultfilters import slugify from django.utils.translation import ugettext_lazy as _, pgettext_lazy, \ activate, deactivate @@ -31,7 +31,8 @@ from django.utils.translation import ugettext_lazy as _, pgettext_lazy, \ from ishtar_common.data_importer import post_importer_action from ishtar_common.models import Document, GeneralType, get_external_id, \ LightHistorizedItem, OwnPerms, Address, Person, post_save_cache, \ - ImageModel, DashboardFormItem, ExternalIdManager, ShortMenuItem + ImageModel, DashboardFormItem, ExternalIdManager, ShortMenuItem, \ + document_attached_changed from ishtar_common.utils import cached_label_changed @@ -87,6 +88,9 @@ class Warehouse(Address, DashboardFormItem, OwnPerms, documents = models.ManyToManyField( Document, related_name='warehouses', verbose_name=_(u"Documents"), blank=True) + main_image = models.ForeignKey( + Document, related_name='main_image_warehouses', + verbose_name=_(u"Main image"), blank=True, null=True) external_id = models.TextField(_(u"External ID"), blank=True, null=True) auto_external_id = models.BooleanField( _(u"External ID is set automatically"), default=False) @@ -252,6 +256,10 @@ class Warehouse(Address, DashboardFormItem, OwnPerms, return +m2m_changed.connect(document_attached_changed, + sender=Warehouse.documents.through) + + class Collection(LightHistorizedItem): name = models.CharField(_(u"Name"), max_length=200, null=True, blank=True) 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() + |