diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-02-14 15:56:05 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-02-14 17:38:09 +0100 |
commit | e7c1f8b74f92778f16721a6ad358f4f248f2a9f6 (patch) | |
tree | 58d2d98b4ed38e5204ae54409357eee8ec576fd0 | |
parent | aef3dfa74b0e2a0a7563d4798ebcf6b0f9998b02 (diff) | |
download | Ishtar-e7c1f8b74f92778f16721a6ad358f4f248f2a9f6.tar.bz2 Ishtar-e7c1f8b74f92778f16721a6ad358f4f248f2a9f6.zip |
🐛 fix VALUES serialization for preventive archaeological files
-rw-r--r-- | archaeological_files/models.py | 14 | ||||
-rw-r--r-- | ishtar_common/models.py | 8 | ||||
-rw-r--r-- | ishtar_common/utils.py | 21 |
3 files changed, 43 insertions, 0 deletions
diff --git a/archaeological_files/models.py b/archaeological_files/models.py index 5e586aa28..e753908f9 100644 --- a/archaeological_files/models.py +++ b/archaeological_files/models.py @@ -19,6 +19,7 @@ import datetime from collections import OrderedDict +import json from django.apps import apps from django.conf import settings @@ -1755,6 +1756,19 @@ class PreventiveFileEquipmentServiceCost(models.Model): verbose_name_plural = _("Equipment requirements") ADMIN_SECTION = _("Preventive") + def toJSON(self): + return json.dumps({ + "file": str(self.file), + "equipment_service_cost": str(self.equipment_service_cost), + "quantity_by_day_planned": self.quantity_by_day_planned, + "days_planned": self.days_planned, + "quantity_by_day_worked": self.quantity_by_day_worked, + "days_worked": self.days_worked, + }, + sort_keys=True, + indent=4 + ) + @property def quantity_planned(self): if self.equipment_service_cost.flat_rate: diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 688197c73..abff81056 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -93,6 +93,7 @@ from ishtar_common.utils import ( task, generate_pdf_preview, revoke_old_task, + InlineClass ) from ishtar_common.utils_secretary import IshtarSecretaryRenderer @@ -138,6 +139,7 @@ from ishtar_common.utils import ( cached_label_changed, generate_relation_graph, max_size_help, + JSON_SERIALIZATION ) from ishtar_common.models_common import ( @@ -2324,6 +2326,12 @@ class DocumentTemplate(models.Model): filtr = [] values = c_object.get_values(filtr=filtr) if not filtr or "VALUES" in filtr: + for k in values: + if k in JSON_SERIALIZATION: + values[k] = JSON_SERIALIZATION[k](values[k]) + elif values[k] and isinstance(values[k], list) \ + and hasattr(values[k][0], "toJSON"): + values[k] = [v.toJSON() for v in values[k]] values["VALUES"] = json.dumps( values, indent=4, diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 95a7ebbaa..e69ef0290 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -112,10 +112,31 @@ class InlineClass: """ Dynamic class used in templates """ + def __init__(self, dct): for k in dct: setattr(self, k, dct[k]) + def toJSON(self): + return json.dumps( + self, + default=lambda o: o.__dict__, + sort_keys=True, + indent=4 + ) + + +def json_used_equipments(value): + res = [] + for gp in value: + res.append(gp[:4] + [cost.toJSON() for cost in gp[-1]]) + return res + + +JSON_SERIALIZATION = { + "used_equipments": json_used_equipments +} + def fake_task(*args): def fake(func): |