summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/models.py8
-rw-r--r--ishtar_common/utils.py21
2 files changed, 29 insertions, 0 deletions
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):