Projet

Général

Profil

0001-backoffice-use-a-django-template-to-render-data-sour.patch

Frédéric Péters, 31 juillet 2019 12:48

Télécharger (8,95 ko)

Voir les différences:

Subject: [PATCH 1/2] backoffice: use a django template to render data source
 view page (#33693)

 wcs/admin/data_sources.py                     | 105 ++++++------------
 wcs/data_sources.py                           |  13 +++
 .../wcs/backoffice/data-sources.html          |  34 ++++++
 3 files changed, 80 insertions(+), 72 deletions(-)
 create mode 100644 wcs/templates/wcs/backoffice/data-sources.html
wcs/admin/data_sources.py
19 19
from quixote.html import TemplateIO, htmltext
20 20

  
21 21
from qommon import _
22
from qommon import template
22 23
from qommon.form import *
23
from qommon.humantime import seconds2humanduration
24 24
from qommon.backoffice.menu import html_top
25 25
from wcs.data_sources import (NamedDataSource, DataSourceSelectionWidget,
26 26
        get_structured_items)
......
117 117

  
118 118
class NamedDataSourcePage(Directory):
119 119
    _q_exports = ['', 'edit', 'delete']
120
    do_not_call_in_templates = True
120 121

  
121 122
    def __init__(self, component):
122 123
        self.datasource = NamedDataSource.get(component)
......
125 126

  
126 127
    def _q_index(self):
127 128
        html_top('datasources', title=self.datasource.name)
129
        return template.QommonTemplateResponse(
130
                templates=['wcs/backoffice/data-sources.html'],
131
                context={'view': self, 'datasource': self.datasource})
132

  
133
    def preview_block(self):
134
        if self.datasource.data_source.get('type') not in ('json', 'formula'):
135
            return ''
136
        items = get_structured_items(self.datasource.data_source)
137
        if not items:
138
            return ''
128 139
        r = TemplateIO(html=True)
129

  
130
        r += htmltext('<div id="appbar">')
131
        r += htmltext('<h2>%s - ') % _('Data Source')
132
        r += self.datasource.name
133
        r += htmltext('</h2>')
134
        r += htmltext('<span class="actions">')
135
        r += htmltext('<a href="delete" rel="popup">%s</a>') % _('Delete')
136
        r += htmltext('<a href="edit">%s</a>') % _('Edit')
137
        r += htmltext('</span>')
140
        r += htmltext('<h3>%s</h3>') % _('Preview (first items only)')
141
        r += htmltext('<div class="bo-block data-source-preview">')
142
        r += htmltext('<ul>')
143
        additional_keys = set()
144
        for item in items[:10]:
145
            if not isinstance(item.get('text'), str):
146
                r += htmltext('<li><tt>%s</tt>: <i>%s (%r)</i></li>') % (
147
                        item.get('id'),
148
                        _('error: not a string'),
149
                        item.get('text'))
150
            else:
151
                r += htmltext('<li><tt>%s</tt>: %s</li>') % (
152
                        item.get('id'), item.get('text'))
153
                additional_keys |= set(item.keys())
154
        if len(items) > 10:
155
            r += htmltext('<li>...</li>')
156
        r += htmltext('</ul>')
157
        additional_keys -= set(['id', 'text'])
158
        if additional_keys:
159
            r += htmltext('<p>%s %s</p>') % (_('Additional keys are available:'),
160
                    ', '.join(sorted(additional_keys)))
138 161
        r += htmltext('</div>')
139

  
140
        if self.datasource.description:
141
            r += htmltext('<div class="bo-block">')
142
            r += self.datasource.description
143
            r += htmltext('</div>')
144

  
145
        data_source_labels = {
146
            'json': _('JSON'),
147
            'jsonp': _('JSONP'),
148
            'formula': _('Python Expression'),
149
        }
150

  
151
        if self.datasource.data_source and self.datasource.data_source.get('type') in data_source_labels:
152
            data_source_type = self.datasource.data_source.get('type')
153
            r += htmltext('<h3>%s</h3>') % _('Configuration')
154
            r += htmltext('<ul>')
155
            r += htmltext('<li>%s</li>') % _(
156
                    'Type of source: %s') % data_source_labels.get(data_source_type)
157
            if data_source_type in ('json', 'jsonp'):
158
                r += htmltext('<li>%s<a href="%s">%s</a></li>') % (
159
                        _('URL: '),
160
                        self.datasource.data_source.get('value'),
161
                        self.datasource.data_source.get('value'))
162
            elif data_source_type == 'formula':
163
                r += htmltext('<li>%s<tt>%s</tt></li>') % (
164
                        _('Python Expression: '),
165
                    self.datasource.data_source.get('value'))
166
            if self.datasource.cache_duration:
167
                r += htmltext('<li>%s %s</li>') % (
168
                        _('Cache Duration:'),
169
                        seconds2humanduration(int(self.datasource.cache_duration)))
170
            r += htmltext('</ul>')
171

  
172
            if data_source_type in ('json', 'formula'):
173
                items = get_structured_items(self.datasource.data_source)
174
                if items:
175
                    r += htmltext('<h3>%s</h3>') % _('Preview (first items only)')
176
                    r += htmltext('<div class="bo-block data-source-preview"><ul>')
177
                    r += htmltext('<ul>')
178
                    additional_keys = set()
179
                    for item in items[:10]:
180
                        if not isinstance(item.get('text'), str):
181
                            r += htmltext('<li><tt>%s</tt>: <i>%s (%r)</i></li>') % (
182
                                    item.get('id'),
183
                                    _('error: not a string'),
184
                                    item.get('text'))
185
                        else:
186
                            r += htmltext('<li><tt>%s</tt>: %s</li>') % (
187
                                    item.get('id'), item.get('text'))
188
                            additional_keys |= set(item.keys())
189
                    if len(items) > 10:
190
                        r += htmltext('<li>...</li>')
191
                    r += htmltext('</ul>')
192
                    additional_keys -= set(['id', 'text'])
193
                    if additional_keys:
194
                        r += htmltext('<p>%s %s</p>') % (_('Additional keys are available:'),
195
                                ', '.join(sorted(additional_keys)))
196
                    r += htmltext('</div>')
197
        else:
198
            # the data source field is required so this should never happen
199
            r += htmltext('<p class="infonotice">%s</p>') % _('Not configured')
200

  
201 162
        return r.getvalue()
202 163

  
203 164
    def edit(self):
wcs/data_sources.py
26 26

  
27 27
from qommon import _
28 28
from qommon.form import *
29
from qommon.humantime import seconds2humanduration
29 30
from qommon.misc import get_variadic_url
30 31
import qommon.misc
31 32
from qommon import get_logger
......
385 386
    def get_substitution_variables(cls):
386 387
        return {'data_source': DataSourcesSubstitutionProxy()}
387 388

  
389
    def type_label(self):
390
        data_source_labels = {
391
            'json': _('JSON'),
392
            'jsonp': _('JSONP'),
393
            'formula': _('Python Expression'),
394
        }
395
        data_source_type = self.data_source.get('type')
396
        return data_source_labels.get(data_source_type)
397

  
398
    def humanized_cache_duration(self):
399
        return seconds2humanduration(int(self.cache_duration))
400

  
388 401

  
389 402
class DataSourcesSubstitutionProxy(object):
390 403
    def __getattr__(self, attr):
wcs/templates/wcs/backoffice/data-sources.html
1
{% load i18n %}
2

  
3
{% block body %}
4
<div id="appbar">
5
<h2>{% trans "Data Source" %} - {{ datasource.name }}</h2>
6
<span class="actions">
7
  <a href="delete" rel="popup">{% trans "Delete" %}</a>
8
  <a href="edit">{% trans "Edit" %}</a>
9
</span>
10
</div>
11

  
12
{% if datasource.description %}
13
<div class="bo-block">{{ datasource.description }}</div>
14
{% endif %}
15

  
16
{% if datasource.data_source %}
17
<h3>{% trans "Configuration" %}</h3>
18
<ul>
19
  <li>{% trans "Type of source:" %} {{ datasource.type_label }}</li>
20
  {% if datasource.data_source.type == 'json' or datasource.data_source.type == 'jsonp' %}
21
  <li>{% trans "URL:" %} <a href="{{ datasource.data_source.value }}">{{ datasource.data_source.value }}</a></li>
22
  {% elif datasource.data_source.type == 'formula' %}
23
  <li>{% trans "Python Expression:" %} {{ datasource.data_source.value }}</li>
24
  {% endif %}
25
  {% if datasource.cache_duration %}
26
  <li>{% trans "Cache Duration:" %} {{ datasource.humanized_cache_duration }}
27
  {% endif %}
28
</ul>
29
{{ view.preview_block|safe }}
30
{% else %}
31
<p class="infonotice">{% trans "Not configured" %}</p>
32
{% endif %}
33

  
34
{% endblock %}
0
-