From c0c501399260e315dd2d0b72cb6825481bf91311 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 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'ishtar_common/models.py') 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( -- cgit v1.2.3