diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-09-04 17:03:52 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-09-04 17:03:52 +0200 |
commit | 46799d5bca1f1d1f43e88e9d34817ae25221063c (patch) | |
tree | a56f84759a60b86ef28c4aca68311fc7fa942e14 | |
parent | 30303bbc898814a1e54eaba7410bdc9fc9669d60 (diff) | |
download | Ishtar-46799d5bca1f1d1f43e88e9d34817ae25221063c.tar.bz2 Ishtar-46799d5bca1f1d1f43e88e9d34817ae25221063c.zip |
✨ templates: clean and add fields for operations (refs #6435)
-rw-r--r-- | archaeological_context_records/models.py | 5 | ||||
-rw-r--r-- | archaeological_finds/models_finds.py | 25 | ||||
-rw-r--r-- | archaeological_operations/models.py | 32 | ||||
-rw-r--r-- | ishtar_common/models.py | 37 |
4 files changed, 78 insertions, 21 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index d9bc520f2..21981fc78 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -530,6 +530,7 @@ class ContextRecord( RelationItem, ): SLUG = "contextrecord" + ALT_SLUG = "context_record" APP = "archaeological-context-records" MODEL = "context-record" SHOW_URL = "show-contextrecord" @@ -792,6 +793,7 @@ class ContextRecord( HISTORICAL_M2M = ["datings", "documentations", "excavation_technics", "identifications"] CACHED_LABELS = ["cached_label", "cached_periods", "cached_related_context_records"] DOWN_MODEL_UPDATE = ["base_finds"] + GET_VALUES_EXTRA = ValueGetter.GET_VALUES_EXTRA + ["context_record"] QA_LOCK = QuickAction( url="contextrecord-qa-lock", @@ -1080,9 +1082,10 @@ class ContextRecord( no_base_finds = True if "no_base_finds" in kwargs: no_base_finds = kwargs["no_base_finds"] - if prefix and no_base_finds or kwargs.get("force_no_base_finds", True): + if prefix and no_base_finds or kwargs.get("force_no_base_finds", False): return values + kwargs["no_base_finds"] = True if not filtr or prefix + "base_finds" in filtr: values[prefix + "base_finds"] = [ bf.get_values(prefix=prefix, no_values=True, filtr=None, **kwargs) diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index d3b1934e1..72ce12401 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -714,10 +714,19 @@ class BaseFind( def get_extra_values(self, prefix="", no_values=False, filtr=None, **kwargs): values = {} + kwargs["force_no_base_finds"] = True no_find = kwargs.get("no_find", False) if not filtr or prefix + "complete_id" in filtr: values[prefix + "complete_id"] = self.complete_id() - if no_find: + exclude = kwargs.get("exclude", []) + if (not filtr or prefix + "context_record" in filtr) and \ + "context_record" not in exclude: + exclude.append("context_record") + kwargs["exclude"] = exclude + values[prefix + "context_record"] = self.context_record.get_values( + filtr=filtr, **kwargs + ) + if no_find or "finds" in exclude: return values if not filtr or prefix + "finds" in filtr: kwargs["no_base_finds"] = True @@ -2750,10 +2759,21 @@ class Find( ] = self.get_material_types_recommendations() if no_base_finds: return values + # by default attach first basefind data bf = self.get_first_base_find() if not bf: return values + if not filtr: + filtr = [] + exclude = kwargs.get("exclude", []) + exclude += [ + e for e in ("finds", "base_find_finds", "find", "base_find_find") + if e not in exclude + ] + kwargs["exclude"] = exclude + kwargs["no_find"] = True + alt_filtr = [k[len("base_find_"):] for k in filtr if k.startswith("base_find_")] alt_filtr += filtr v = bf.get_values(prefix=prefix, no_values=True, filtr=alt_filtr, **kwargs) @@ -2766,7 +2786,8 @@ class Find( new_values.update(dict((('base_find_' + k, v) for k, v in v.items()))) new_values.update(values) values = new_values - kwargs["no_find"] = True + + # list all base finds if necessary values[prefix + "base_finds"] = [ base_find.get_values(no_values=True, filtr=filtr, **kwargs) for base_find in self.base_finds.distinct().order_by("-pk").all() diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index b7b955b80..301b9c6ca 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -1847,6 +1847,15 @@ class Operation( def get_document_containers_values(self, filtr, exclude) -> list: # Container value return self._get_containers_values(self.document_containers_q, filtr, exclude) + def get_finds_values(self, filtr, exclude) -> list: # finds value + if "operation" not in exclude: + exclude.append("operation") + finds = [] + for f in self.finds_q.exclude(base_finds__isnull=True).order_by( + "base_finds__context_record_id").distinct().all(): + finds.append(f.get_values(filtr=filtr, exclude=exclude, no_operation=True)) + return finds + def get_extra_values(self, prefix="", no_values=False, filtr=None, **kwargs): values = {} values = get_values_town_related(self, prefix, values, filtr=filtr) @@ -1856,10 +1865,17 @@ class Operation( containers = [] if (not filtr or ( f"{prefix}containers" in filtr or f"{prefix}all_containers" in filtr)) and ( - f"{prefix}containers" not in exclude + f"{prefix}containers" not in exclude and "containers" not in exclude ): containers = self.get_containers_values(filtr, exclude) values[f"{prefix}containers"] = containers[:] + finds = [] + if (not filtr or f"{prefix}finds" in filtr) and ( + f"{prefix}finds" not in exclude and not prefix.endswith("operation_")): + if "context_record" in exclude: + exclude.pop(exclude.index("context_record")) + finds = self.get_finds_values(filtr, exclude) + values[f"{prefix}finds"] = finds[:] document_containers = [] if (not filtr or ( f"{prefix}document_containers" in filtr or f"{prefix}all_containers" in filtr)) and ( @@ -1878,9 +1894,8 @@ class Operation( if prefix: return values # context_records only when there is no prefix - if ( - not filtr or "context_records" in filtr - ) and "context_records" not in exclude: + if (not filtr or "context_records" in filtr) and \ + "context_records" not in exclude: kwargs["no_base_finds"] = False values["context_records"] = [ cr.get_values(prefix=prefix, no_values=True, filtr=None, **kwargs) @@ -2138,6 +2153,15 @@ class Operation( ) @property + def finds_q(self): # -> Find queryset + """ + Finds related to this operation + """ + Find = apps.get_model("archaeological_finds", "Find") + q = Find.objects.filter(base_finds__context_record__operation=self) + return q + + @property def containers_q(self): # -> Container queryset """ Containers with finds related to this operation diff --git a/ishtar_common/models.py b/ishtar_common/models.py index d4cac83df..6dc6d16c0 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -312,6 +312,8 @@ class ValueGetter: def get_values(self, prefix="", no_values=False, filtr=None, **kwargs): if not prefix: prefix = self._prefix + if not filtr: + filtr = [] extra_args = ["getvalues", str(self.pk), prefix, "1" if no_values else "0"] if filtr: extra_args += filtr @@ -333,14 +335,16 @@ class ValueGetter: and prefix + "qrcode_path" not in exclude ): values[prefix + "qrcode_path"] = self.qrcode_path - for field_name in get_all_field_names(self): + + for field_name in sorted(get_all_field_names(self) + self.GET_VALUES_EXTRA): try: value = getattr(self, field_name) except (AttributeError, MultipleObjectsReturned): continue if ( - field_name in self.GET_VALUES_EXCLUDE_FIELDS - or prefix + field_name in exclude + (field_name in self.GET_VALUES_EXCLUDE_FIELDS + or prefix + field_name in exclude) or + kwargs.get(f"no_{field_name}", False) ): continue if filtr and not any( @@ -349,6 +353,17 @@ class ValueGetter: continue if hasattr(value, "get_values"): new_prefix = prefix + field_name + "_" + new_exclude = exclude[:] + slugs = [getattr(self, "SLUG", ""), getattr(self, "ALT_SLUG", "")] + for slug in slugs: + if not slug: + continue + for s in (slug, new_prefix + slug, slug + "s", + new_prefix + slug + "s"): + if s not in new_exclude: + new_exclude.append(s) + new_kwargs = kwargs.copy() + new_kwargs["exclude"] = new_exclude values.update(value.get_values(new_prefix, filtr=filtr, **kwargs)) if hasattr(self, "get_values_for_" + field_name): values[prefix + field_name] = getattr( @@ -357,12 +372,6 @@ class ValueGetter: else: values[prefix + field_name] = value values.update(self._get_values_documents(prefix=prefix, filtr=filtr)) - for extra_field in self.GET_VALUES_EXTRA: - if filtr and not any( - extra_field for f in filtr if f.startswith(prefix + extra_field) - ): - continue - values[prefix + extra_field] = getattr(self, extra_field) or "" for key, val in values.items(): if val is None: val = "" @@ -376,7 +385,8 @@ class ValueGetter: val = "" values[key] = val values.update( - self.get_extra_values(prefix=prefix, no_values=no_values, filtr=filtr, **kwargs) + self.get_extra_values(prefix=prefix, no_values=no_values, filtr=filtr, + **kwargs) ) # do not provide KEYS and VALUES for sub-items if (prefix and prefix != self._prefix) or no_values: @@ -2400,14 +2410,14 @@ class DocumentTemplate(models.Model): get_values = "VALUES" in filtr if get_values: filtr = [] - extension = "txt" if get_values else self.template.name.split(".")[-1] + extension = "json" if get_values else self.template.name.split(".")[-1] tempdir = tempfile.mkdtemp("-ishtardocs") output_name = ( tempdir + os.path.sep + slugify(self.name.replace(" ", "_").lower()) + "-" - + datetime.date.today().strftime("%Y-%m-%d") + + datetime.datetime.now().strftime("%Y-%m-%d-%H%M") + "." + extension ) @@ -2425,8 +2435,7 @@ class DocumentTemplate(models.Model): sort_keys=True, skipkeys=True, ensure_ascii=False, - separators=("", " : "), - ).replace(" " * 4, "\t") + ) # initialize missing values if filtr: for k in filtr: |