Browse Source

show_object* template factorisation

+ bugfixes in show_object_detailled
Yann Weber 8 years ago
parent
commit
4e71cc3ad4

+ 6
- 26
lodel/plugins/webui/templates/listing/show_object.html View File

@@ -1,15 +1,16 @@
1 1
 {% extends "base.html" %}
2
+{% import 'macros/show_object.html' as show_object %}
2 3
 {% import 'components/components.html' as components %}
3 4
 {% set my_class = leapi.name2class(classname) %}
4
-{% set uidfield = my_class.uid_fieldname()[0] %}
5
-{% set objects = my_class.get(('%s = %s') % (uidfield, lodel_id)) %}
6
-{% set obj = objects.pop() %}
5
+{% set obj = my_class.get_from_uid(lodel_id) %}
6
+{% if obj is none %}
7
+    ERROR <!-- conception failure, the controller should test this before calling the template -->
8
+{% endif %}
7 9
 {% if my_class.is_abstract() %}
8 10
 {% set classname = obj.data('classname') %}
9 11
 {% set my_class = my_class.name2class(classname) %}
10 12
 {% endif %}
11 13
 {% block title %}Object {{ lodel_id }} {% endblock %}
12
-{% import "components/components.html" as components %}
13 14
 {% block content %}
14 15
 <ol class="breadcrumb">
15 16
   <li><a href="/{{ root_url }}/">Home</a></li>
@@ -18,26 +19,5 @@
18 19
   <li class="active">{{ lodel_id }}</li>
19 20
 </ol>
20 21
 <h1 class="h1_lodel">Lodel 2 - {{ classname }} with uid {{ lodel_id }}</h1>
21
-<ul>
22
-    <!-- To get a component HTML code, it is necessary to call : components.<macro_name>(args) -->
23
-    {% for fieldname, fieldvalue in obj.fields(include_ro = True).items() %}
24
-        {% if fieldvalue is not none %}
25
-           {% if fieldvalue.base_type == 'ref' %}
26
-                {% if obj.data(fieldname) is iterable %}
27
-                    <li>{{ fieldname }}
28
-                    {% set l_classe = fieldvalue.allowed_classes[0] %}
29
-                        <ul>
30
-                    {% for rel in obj.data(fieldname) %}
31
-                            {% set casttype = l_classe.data_handler(l_classe.uid_fieldname()[0]).cast_type %}
32
-                            {% set rel2 = casttype(rel) %}
33
-                            <li><a href="show_object?classname={{ l_classe.__name__ }}&lodel_id={{ rel2 }}" >{{ rel2 }}</a></li>
34
-                    {% endfor %}
35
-                        </ul></li>
36
-                {% endif %}
37
-            {% else %}
38
-                <li> {{ fieldname }} : {{ obj.data(fieldname) }} </li>
39
-            {% endif %}
40
-        {% endif %}
41
-    {% endfor %}
42
-</ul>
22
+    {{ show_object.object_ul(obj) }}
43 23
 {% endblock %}

+ 6
- 22
lodel/plugins/webui/templates/listing/show_object_detailled.html View File

@@ -1,7 +1,12 @@
1 1
 {% extends "base.html" %}
2
+{% import 'macros/show_object.html' as show_object %}
2 3
 {% import 'components/components.html' as components %}
3 4
 {% import "listing/display_obj.html" as edit %}
4 5
 {% set my_class = leapi.name2class(classname) %}
6
+{% set obj = my_class.get_from_uid(lodel_id) %}
7
+{% if obj is none %}
8
+    ERROR <!-- conception failure, the controller should test this before calling the template -->
9
+{% endif %}
5 10
 {% set uidfield = my_class.uid_fieldname()[0] %}
6 11
 {% set objects = my_class.get(('%s = %s') % (uidfield, lodel_id)) %}
7 12
 {% set obj = objects.pop() %}
@@ -18,26 +23,5 @@
18 23
   <li class="active">{{ lodel_id }}</li>
19 24
 </ol>
20 25
 <h1 class="h1_lodel">Lodel 2 - {{ classname }} with uid {{ lodel_id }}</h1>
21
-<ul>
22
-    <!-- To get a component HTML code, it is necessary to call : components.<macro_name>(args) -->
23
-    {% for fieldname, fieldvalue in obj.fields(include_ro = True).items() %}
24
-        {% if fieldvalue is not none %}
25
-           {% if fieldvalue.base_type == 'ref' %}
26
-                {% if obj.data(fieldname) is iterable %}
27
-                    <li>{{ fieldname }}
28
-                    {% set l_classe = fieldvalue.allowed_classes[0] %}
29
-                        <ul>
30
-                    {% set linked_objs=l_classe.get(("%s in (%s)") % (l_classe.uid_fieldname()[0], obj.data(fieldname)|join(','))) %}
31
-                    {% for linked_obj in linked_objs %}
32
-                            {{ edit.display(linked_obj) }}
33
-                            <br/>
34
-                    {% endfor %}
35
-                        </ul></li>
36
-                {% endif %}
37
-            {% else %}
38
-                <li> {{ fieldname }} : {{ obj.data(fieldname) }} </li>
39
-            {% endif %}
40
-        {% endif %}
41
-    {% endfor %}
42
-</ul>
26
+    {{ show_object.object_ul(obj, 2) }}
43 27
 {% endblock %}

+ 38
- 0
lodel/plugins/webui/templates/macros/show_object.html View File

@@ -0,0 +1,38 @@
1
+<!-- Produce a <li></li> with objects informations -->
2
+{% macro object_ul(leo, max_depth = 0, exclude = []) -%}
3
+    <ul>
4
+        {% if max_depth <= 0 %}
5
+            <!-- No need to go deeper -->
6
+            {% for fname, fvalue in leo.datas(True).items() %}
7
+                <li><em>{{ fname }}</em> : {{fvalue}}</li>
8
+            {%endfor%}
9
+        {% else %}
10
+            {% for fname in leo.fieldnames(True) %}
11
+                {% set fvalue = leo.data(fname) %}
12
+                {% set fdh = leo.data_handler(fname) %}
13
+                {% if fvalue is not none and fdh.is_reference() %}
14
+                    {% if fdh.is_singlereference() %}
15
+                        {% set referenced = fdh.get_referenced(fvalue) %}
16
+                        {% if (referenced.__class__.__name__, referenced.uid) in exclude %}
17
+                            <li><em>{{fname}}</em> : {{fvalue}}</li>
18
+                        {% else %}
19
+                            <li><em>{{fname}}</em> : {{object_ul(referenced, max_depth - 1, exclude + [(leo.__class__.__name__, leo.uid())] )}}</li>
20
+                        {% endif %}
21
+                    {% else %}
22
+                        <li><em>{{fname}}</em> : <ul>
23
+                        {% for referenced in fdh.get_referenced(fvalue) %}
24
+                            {% if (referenced.__class__.__name__, referenced.uid) in exclude %}
25
+                                <li><em>{{fname}}</em> : {{fvalue}}</li>
26
+                            {% else %}
27
+                                <li><em>{{fname}}</em> : {{object_ul(referenced, max_depth - 1, exclude + [(leo.__class__.__name__, leo.uid())] )}}</li>
28
+                            {% endif %}
29
+                        {% endfor %}
30
+                        </ul></li>
31
+                    {% endif %}
32
+                {% else %}
33
+                    <li><em>{{fname}}</em> : {{fvalue}}</li>
34
+                {% endif %}
35
+            {% endfor %}
36
+        {% endif %}
37
+    </ul>
38
+{%- endmacro %}

Loading…
Cancel
Save