diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-10-18 12:52:28 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-10-18 12:52:28 +0200 |
commit | c0c501399260e315dd2d0b72cb6825481bf91311 (patch) | |
tree | acc2befd68fab9d757a1e02e035595eefd28de45 /ishtar_common/models.py | |
parent | d49dc63a205f3e8281ada8b9fabb3d7ceeec80ba (diff) | |
download | Ishtar-c0c501399260e315dd2d0b72cb6825481bf91311.tar.bz2 Ishtar-c0c501399260e315dd2d0b72cb6825481bf91311.zip |
Json fields: manage dynamic json fields display in the sheets (refs #3077)
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 39 |
1 files changed, 38 insertions, 1 deletions
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( |