From 25e280824b2217b95ec755fa409a165992f546a9 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 18 Oct 2017 12:52:28 +0200 Subject: Json fields: manage dynamic json fields display in the sheets (refs #3077) --- ishtar_common/models.py | 39 +++++++++++++++++++++- .../templates/ishtar/blocks/sheet_json.html | 11 ++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 ishtar_common/templates/ishtar/blocks/sheet_json.html (limited to 'ishtar_common') diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 6a59adb77..c3ba4fdd0 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -918,7 +918,7 @@ class JsonDataSection(models.Model): class Meta: verbose_name = _(u"Json data - Menu") verbose_name_plural = _(u"Json data - Menus") - ordering = ['name'] + ordering = ['order', 'name'] def __unicode__(self): return u"{} - {}".format(self.content_type, self.name) @@ -940,6 +940,7 @@ class JsonDataField(models.Model): class Meta: verbose_name = _(u"Json data - Field") verbose_name_plural = _(u"Json data - Fields") + ordering = ['order', 'name'] def __unicode__(self): return u"{} - {}".format(self.content_type, self.name) @@ -962,6 +963,42 @@ class JsonData(models.Model): if not self.data: self.data = {} + @property + def json_sections(self): + sections = [] + try: + content_type = ContentType.objects.get_for_model(self) + except ContentType.DoesNotExists: + return sections + fields = list(JsonDataField.objects.filter( + content_type=content_type, display=True, section__isnull=True + ).all()) # no section fields + + fields += list(JsonDataField.objects.filter( + content_type=content_type, display=True, section__isnull=False + ).order_by('section__order', 'order').all()) + + for field in fields: + value = None + data = self.data.copy() + for key in field.key.split('__'): + if key in data: + value = copy.copy(data[key]) + data = data[key] + else: + value = None + break + if not value: + continue + if type(value) in (list, tuple): + value = u" ; ".join([unicode(v) for v in value]) + section_name = field.section.name if field.section else None + if not sections or section_name != sections[-1][0]: + # if section name is identical it is the same + sections.append((section_name, [])) + sections[-1][1].append((field.name, value)) + return sections + class Imported(models.Model): imports = models.ManyToManyField( diff --git a/ishtar_common/templates/ishtar/blocks/sheet_json.html b/ishtar_common/templates/ishtar/blocks/sheet_json.html new file mode 100644 index 000000000..31e6acb84 --- /dev/null +++ b/ishtar_common/templates/ishtar/blocks/sheet_json.html @@ -0,0 +1,11 @@ +{% load i18n window_field %} +{% for json_section, json_fields in item.json_sections %} +{% if json_section %} +

{{json_section}}

+{% endif %} +{% for label, value in json_fields %} +{% if forloop.first %}{% endif %} +{% endfor %} +{% endfor %} -- cgit v1.2.3