0001-opengis-add-test-button-for-indexing-template-58360.patch
passerelle/apps/opengis/forms.py | ||
---|---|---|
41 | 41 |
fields = '__all__' |
42 | 42 |
exclude = ['resource', 'computed_properties'] |
43 | 43 | |
44 |
def __init__(self, *args, **kwargs): |
|
45 |
super().__init__(*args, **kwargs) |
|
46 |
if self.instance.pk: |
|
47 |
self.fields[ |
|
48 |
'indexing_template' |
|
49 |
].widget.template_name = 'opengis/widgets/indexing_template_widget.html' |
|
50 | ||
44 | 51 |
def clean_filter_expression(self): |
45 | 52 |
filter_expression = self.cleaned_data['filter_expression'] |
46 | 53 |
if filter_expression: |
passerelle/apps/opengis/migrations/0012_query_indexing_template.py | ||
---|---|---|
26 | 26 |
name='indexing_template', |
27 | 27 |
field=models.TextField( |
28 | 28 |
blank=True, |
29 |
help_text='Template used to generate contents used in fulltext search.', |
|
30 | 29 |
verbose_name='Indexing template', |
31 | 30 |
), |
32 | 31 |
), |
passerelle/apps/opengis/models.py | ||
---|---|---|
488 | 488 |
filter_expression = models.TextField(_('XML filter'), blank=True) |
489 | 489 |
indexing_template = models.TextField( |
490 | 490 |
verbose_name=_('Indexing template'), |
491 |
help_text=_('Template used to generate contents used in fulltext search.'), |
|
492 | 491 |
blank=True, |
493 | 492 |
) |
494 | 493 |
computed_properties = JSONField(blank=True, default=dict) |
passerelle/apps/opengis/templates/opengis/query_form.html | ||
---|---|---|
27 | 27 |
</tbody> |
28 | 28 |
</table> |
29 | 29 |
<button id="add-computed-property-form" type="button">{% trans "Add another computed property" %}</button> |
30 |
<script |
|
31 |
id="indexing-template-script" |
|
32 |
data-url="{% url 'opengis-query-test-indexing-template' slug=object.resource.slug pk=object.pk %}" |
|
33 |
> |
|
34 |
$('button#test-indexing-template').click(function (e) { |
|
35 |
$('div.template-result').show(); |
|
36 |
$.ajax({ |
|
37 |
url : $('#indexing-template-script').data('url'), |
|
38 |
data: {'template': $('#id_indexing_template').val()}, |
|
39 |
type: 'GET', |
|
40 |
success: function(data) { |
|
41 |
$('span.template-result').html(data['data'].join(', ')); |
|
42 |
} |
|
43 |
}); |
|
44 |
}); |
|
45 |
</script> |
|
30 | 46 |
{% endblock %} |
passerelle/apps/opengis/templates/opengis/widgets/indexing_template_widget.html | ||
---|---|---|
1 |
{% load i18n %} |
|
2 | ||
3 |
<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}> |
|
4 |
{% if widget.value %}{{ widget.value }}{% endif %}</textarea> |
|
5 |
<div class="hint">{% trans 'Template used to generate contents used in fulltext search.' %}</div> |
|
6 |
<button id="test-indexing-template" type="button">{% trans "Test template" %}</button> |
|
7 |
<div class="template-result" hidden="true">{% trans 'Template result preview:' %} <span class="template-result">{% trans "loading" %}...</span></div> |
passerelle/apps/opengis/urls.py | ||
---|---|---|
26 | 26 |
views.QueryDelete.as_view(), |
27 | 27 |
name='opengis-query-delete', |
28 | 28 |
), |
29 |
url( |
|
30 |
r'^(?P<slug>[\w,-]+)/query/(?P<pk>\d+)/test-indexing-template/$', |
|
31 |
views.TestIndexingTemplate.as_view(), |
|
32 |
name='opengis-query-test-indexing-template', |
|
33 |
), |
|
29 | 34 |
] |
passerelle/apps/opengis/views.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU Affero General Public License |
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
from django.http import HttpResponseRedirect |
|
18 |
from django.views.generic import CreateView, DeleteView, UpdateView |
|
17 |
from django.http import HttpResponseRedirect, JsonResponse |
|
18 |
from django.template import Context, Template, TemplateSyntaxError |
|
19 |
from django.views.generic import CreateView, DeleteView, DetailView, UpdateView |
|
19 | 20 | |
20 | 21 |
from passerelle.base.mixins import ResourceChildViewMixin |
22 |
from passerelle.utils.conversion import simplify |
|
21 | 23 | |
22 | 24 |
from . import models |
23 | 25 |
from .forms import ComputedPropertyFormSet, QueryForm |
... | ... | |
87 | 89 | |
88 | 90 |
class QueryDelete(ResourceChildViewMixin, DeleteView): |
89 | 91 |
model = models.Query |
92 | ||
93 | ||
94 |
class TestIndexingTemplate(ResourceChildViewMixin, DetailView): |
|
95 |
model = models.Query |
|
96 | ||
97 |
def get(self, request, *args, **kwargs): |
|
98 |
template = request.GET.get('template') |
|
99 |
if not template: |
|
100 |
return JsonResponse({'data': []}) |
|
101 | ||
102 |
try: |
|
103 |
template = Template(template) |
|
104 |
except TemplateSyntaxError: |
|
105 |
return JsonResponse({'data': []}) |
|
106 | ||
107 |
results = [] |
|
108 |
features = self.get_object().features.values_list('data', flat=True)[:3] |
|
109 |
for feature in features: |
|
110 |
context = Context(feature.get('properties', {})) |
|
111 |
results.append(simplify(template.render(context))) |
|
112 | ||
113 |
return JsonResponse({'data': results}) |
tests/test_opengis.py | ||
---|---|---|
1013 | 1013 |
import_site(serialization) |
1014 | 1014 |
assert OpenGIS.objects.count() == 1 |
1015 | 1015 |
assert Query.objects.count() == 1 |
1016 | ||
1017 | ||
1018 |
@mock.patch('passerelle.utils.Request.get') |
|
1019 |
def test_opengis_test_indexing_template_view(mocked_get, admin_user, app, connector, query): |
|
1020 |
url = '/manage/opengis/%s/query/%s/test-indexing-template/' % (connector.slug, query.pk) |
|
1021 |
mocked_get.side_effect = geoserver_geolocated_responses |
|
1022 |
query.update_cache() |
|
1023 | ||
1024 |
app = login(app) |
|
1025 |
resp = app.get(url + '?template=') |
|
1026 |
assert resp.json['data'] == [] |
|
1027 | ||
1028 |
resp = app.get(url + '?template={{a}b}}') |
|
1029 |
assert resp.json['data'] == [] |
|
1030 | ||
1031 |
resp = app.get(url + '?template={{nom_commune}} {{nom_voie}}') |
|
1032 |
assert resp.json['data'] == ['grenoble', 'grenoble', 'grenoble place victor hugo'] |
|
1033 | ||
1034 |
resp = app.get('/manage/opengis/%s/query/%s/' % (connector.slug, query.pk)) |
|
1035 |
assert url in resp.text |
|
1036 |
assert 'Test template' in resp.text |
|
1016 |
- |