diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-11-18 16:46:37 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-02-28 12:15:21 +0100 |
commit | b27fabdea6c6bfba75521dc84f5a321e61e6ef52 (patch) | |
tree | 2b4452bc7fec7de7a90e81d9055a8b9971b18c5c /ishtar_common/models.py | |
parent | 4fa501cb189c61d94a2ca53a604f3db7a212153c (diff) | |
download | Ishtar-b27fabdea6c6bfba75521dc84f5a321e61e6ef52.tar.bz2 Ishtar-b27fabdea6c6bfba75521dc84f5a321e61e6ef52.zip |
Complex index generation with JINJA2 templates
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 95 |
1 files changed, 67 insertions, 28 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 6e8e0409e..08009bb41 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -56,9 +56,9 @@ from django.core.cache import cache from django.core.exceptions import ObjectDoesNotExist, ValidationError, \ MultipleObjectsReturned from django.core.files.uploadedfile import SimpleUploadedFile -from django.core.urlresolvers import reverse, NoReverseMatch -from django.db.models import Q, Max, Count, F -from django.db.models.signals import post_save, post_delete, m2m_changed +from django.core.urlresolvers import reverse +from django.db.models import Q, Max, Count +from django.db.models.signals import post_save, post_delete from django.db.utils import DatabaseError from django.template import Context, Template from django.template.defaultfilters import slugify @@ -85,8 +85,8 @@ from ishtar_common.utils import get_cache, create_slug, \ generate_relation_graph, max_size_help from ishtar_common.models_common import GeneralType, HierarchicalType, \ - BaseHistorizedItem, LightHistorizedItem, FullSearch, Imported, \ - FixAssociated, SearchAltName, HistoryError, OwnPerms, Cached, \ + BaseHistorizedItem, LightHistorizedItem, FullSearch, \ + SearchAltName, OwnPerms, Cached, \ Address, post_save_cache, TemplateItem, SpatialReferenceSystem, \ DashboardFormItem, document_attached_changed, SearchAltName, \ DynamicRequest, GeoItem, CompleteIdentifierItem, SearchVectorConfig, \ @@ -542,7 +542,8 @@ class JsonDataField(models.Model): help_text=_("Value of the key in the JSON schema. For hierarchical " "key use \"__\" to explain it. For instance for the key " "'my_subkey' with data such as {'my_key': {'my_subkey': " - "'value'}}, its value will be reached with my_key__my_subkey.")) + "'value'}}, its value will be reached with " + "my_key__my_subkey.")) display = models.BooleanField(_("Display"), default=True) value_type = models.CharField(_("Type"), default="T", max_length=10, choices=JSON_VALUE_TYPES) @@ -811,7 +812,8 @@ class IshtarSiteProfile(models.Model, Cached): operation_custom_index = models.TextField( _("Operation custom index key"), default="", - help_text=_("Key to be used to manage operation custom index.")) + help_text=_("Keys to be used to manage operation custom index. " + "Separate keys with a semicolon.")) site_complete_identifier = models.TextField( _("Archaeological site complete identifier"), default="", @@ -820,8 +822,8 @@ class IshtarSiteProfile(models.Model, Cached): site_custom_index = models.TextField( _("Archaeological site custom index key"), default="", - help_text=_("Key to be used to manage archaeological site custom" - " index.")) + help_text=_("Key to be used to manage archaeological site custom " + "index. Separate keys with a semicolon.")) file_external_id = models.TextField( _("File external id"), default="{year}-{numeric_reference}", @@ -838,7 +840,7 @@ class IshtarSiteProfile(models.Model, Cached): _("Archaeological file custom index key"), default="", help_text=_("Key to be used to manage archaeological file custom " - "index.")) + "index. Separate keys with a semicolon.")) parcel_external_id = models.TextField( _("Parcel external id"), default="{associated_file__external_id}{operation__code_patriarche}-" @@ -861,7 +863,8 @@ class IshtarSiteProfile(models.Model, Cached): contextrecord_custom_index = models.TextField( _("Context record custom index key"), default="", - help_text=_("Key to be used to manage context record custom index.")) + help_text=_("Key to be used to manage context record custom index. " + "Separate keys with a semicolon.")) base_find_external_id = models.TextField( _("Base find external id"), default="{context_record__external_id}-{label}", @@ -876,7 +879,8 @@ class IshtarSiteProfile(models.Model, Cached): basefind_custom_index = models.TextField( _("Base find custom index key"), default="", - help_text=_("Key to be used to manage base find custom index.")) + help_text=_("Key to be used to manage base find custom index. " + "Separate keys with a semicolon.")) find_external_id = models.TextField( _("Find external id"), default="{get_first_base_find__context_record__external_id}-{label}", @@ -891,7 +895,8 @@ class IshtarSiteProfile(models.Model, Cached): find_custom_index = models.TextField( _("Find custom index key"), default="", - help_text=_("Key to be used to manage find custom index.")) + help_text=_("Key to be used to manage find custom index. " + "Separate keys with a semicolon.")) container_external_id = models.TextField( _("Container external id"), default="{parent_external_id}-{container_type__txt_idx}-" @@ -907,7 +912,8 @@ class IshtarSiteProfile(models.Model, Cached): container_custom_index = models.TextField( _("Container custom index key"), default="", - help_text=_("Key to be used to manage container custom index.")) + help_text=_("Key to be used to manage container custom index. " + "Separate keys with a semicolon.")) warehouse_external_id = models.TextField( _("Warehouse external id"), default="{name|slug}", @@ -922,7 +928,8 @@ class IshtarSiteProfile(models.Model, Cached): warehouse_custom_index = models.TextField( _("Warehouse custom index key"), default="", - help_text=_("Key to be used to manage warehouse custom index.")) + help_text=_("Key to be used to manage warehouse custom index. " + "Separate keys with a semicolon.")) document_external_id = models.TextField( _("Document external id"), default="{index}", @@ -937,7 +944,8 @@ class IshtarSiteProfile(models.Model, Cached): document_custom_index = models.TextField( _("Document custom index key"), default="", - help_text=_("Key to be used to manage document custom index.")) + help_text=_("Key to be used to manage document custom index. " + "Separate keys with a semicolon.")) person_raw_name = models.TextField( _("Raw name for person"), default="{name|upper} {surname}", @@ -1313,7 +1321,7 @@ class Dashboard(object): for idx in last_ids: try: obj = self.model.objects.get(pk=idx['id']) - except: + except self.model.DoesNotExist: # deleted object are always referenced in history continue obj.history_date = idx['hd'] @@ -1387,12 +1395,12 @@ class Dashboard(object): self.operation_mode = str( Operation.objects.get(pk=operation_mode_pk)) - def get_average(self, vals=[]): + def get_average(self, vals=None): if not vals: vals = self.numbers[:] return sum(vals) / len(vals) - def get_variance(self, vals=[]): + def get_variance(self, vals=None): if not vals: vals = self.numbers[:] avrg = self.get_average(vals) @@ -2610,11 +2618,12 @@ class SourceType(HierarchicalType): coins_type = models.CharField(_("COInS export - type"), default='document', max_length=100) coins_genre = models.CharField(_("COInS export - genre"), blank=True, - max_length=100) + default='', max_length=100) is_localized = models.BooleanField( _("Is localized"), default=False, help_text=_("Setting a language for this type of document is relevant") ) + code = models.CharField(_("Code"), blank=True, default='', max_length=100) class Meta: verbose_name = _("Document type") @@ -3074,16 +3083,46 @@ class Document(BaseHistorizedItem, CompleteIdentifierItem, OwnPerms, ImageModel, def __str__(self): return self.title + @property + def operation_codes(self): + Operation = apps.get_model("archaeological_operations", "Operation") + return "|".join( + sorted([Operation.objects.get(pk=ope_id).code_patriarche + for ope_id in self.get_related_operation_ids()])) + + def get_related_operation_ids(self): + operations = list( + self.operations.values_list("id", flat=True).all()) + operations += list( + self.context_records.values_list( + "operation_id", flat=True).all()) + operations += list( + self.finds.values_list( + "base_finds__context_record__operation_id", flat=True).all()) + return list(set(operations)) + def get_index_operation(self): - current_operation = None - ope_nb = self.operations.count() - if ope_nb > 1: + operations = self.get_related_operation_ids() + if len(operations) != 1: return - elif ope_nb == 1: - key = 'operations__pk' - cr_nb = self.context_records.count() - if cr_nb: - self.context_records + current_operation = operations[0] + q = Document.objects.exclude(pk=self.pk).filter( + Q(operations__id=current_operation) | + Q(context_records__operation_id=current_operation) | + Q(finds__base_finds__context_record__operation_id=current_operation) + ).order_by("-custom_index") + current_index = None + for doc in q.all(): + if not doc.custom_index: + continue + if len(doc.get_related_operation_ids()) != 1: + continue + current_index = doc.custom_index + break # ordered by "-custom_index" so max current index is reached + if not current_index: + return 1 + else: + return current_index + 1 def natural_key(self): return (self.external_id,) |