summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
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
commit25e280824b2217b95ec755fa409a165992f546a9 (patch)
treeacc2befd68fab9d757a1e02e035595eefd28de45
parentdf3570e2416675a61b161b766113f9603e239820 (diff)
downloadIshtar-25e280824b2217b95ec755fa409a165992f546a9.tar.bz2
Ishtar-25e280824b2217b95ec755fa409a165992f546a9.zip
Json fields: manage dynamic json fields display in the sheets (refs #3077)
-rw-r--r--archaeological_operations/templates/ishtar/sheet_operation.html2
-rw-r--r--ishtar_common/models.py39
-rw-r--r--ishtar_common/templates/ishtar/blocks/sheet_json.html11
3 files changed, 51 insertions, 1 deletions
diff --git a/archaeological_operations/templates/ishtar/sheet_operation.html b/archaeological_operations/templates/ishtar/sheet_operation.html
index 5a02236a3..e46db74c7 100644
--- a/archaeological_operations/templates/ishtar/sheet_operation.html
+++ b/archaeological_operations/templates/ishtar/sheet_operation.html
@@ -71,6 +71,8 @@
{% field "Abstract" item.abstract "<pre>" "</pre>" %}
{% field "Comment about scientific documentation" item.scientific_documentation_comment "<pre>" "</pre>" %}
+{% include "ishtar/blocks/sheet_json.html" %}
+
{% if not next %}
{% if item.towns.count %}
<h3>{% trans "Localisation"%}</h3>
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 %}
+<h3>{{json_section}}</h3>
+{% endif %}
+{% for label, value in json_fields %}
+{% if forloop.first %}<ul class='form-flex'>{% endif %}
+ {% field_li label value %}
+{% if forloop.last %}</ul>{% endif %}
+{% endfor %}
+{% endfor %}