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):
|