0001-jsondatastore-add-a-search-endpoint-29889.patch
passerelle/apps/jsondatastore/models.py | ||
---|---|---|
100 | 100 |
class Meta: |
101 | 101 |
verbose_name = _('JSON Data Store') |
102 | 102 | |
103 |
@endpoint(perm='can_access', name='data', pattern=r'$', |
|
104 |
description=_('Listing')) |
|
105 |
def list(self, request, name_id=None, q=None, **kwargs): |
|
103 |
def filter(self, name_id=None, q=None, **kwargs): |
|
106 | 104 |
objects = JsonData.objects.filter(datastore=self) |
107 | 105 |
if name_id is not None: |
108 | 106 |
objects = objects.filter(name_id=name_id) |
... | ... | |
111 | 109 |
objects = [o for o in objects if q in simplify(o.text)] |
112 | 110 |
for key, value in kwargs.items(): |
113 | 111 |
objects = [o for o in objects if o.content.get(key) == value] |
112 |
return objects |
|
113 | ||
114 |
@endpoint(perm='can_access', name='data', pattern=r'$', |
|
115 |
description=_('Listing')) |
|
116 |
def list(self, request, name_id=None, q=None, **kwargs): |
|
117 |
objects = self.filter(name_id, q, **kwargs) |
|
114 | 118 |
return {'data': [x.to_json() for x in objects]} |
115 | 119 | |
120 |
@endpoint(perm='can_access', name='search', pattern=r'$', |
|
121 |
description=_('List content')) |
|
122 |
def search(self, request, name_id=None, q=None, |
|
123 |
url_field='url', text_field='text', description_field='description', |
|
124 |
**kwargs): |
|
125 |
objects = self.filter(name_id, q, **kwargs) |
|
126 |
data = [] |
|
127 |
for obj in objects: |
|
128 |
data.append({ |
|
129 |
'url': obj.content.get(url_field), |
|
130 |
'text': obj.content.get(text_field) or obj.text, |
|
131 |
'description': obj.content.get(description_field)}) |
|
132 |
return {'err': 0, 'data': data} |
|
133 | ||
116 | 134 |
@endpoint(perm='can_access', methods=['post'], name='data', |
117 | 135 |
pattern=r'create$', |
118 | 136 |
example_pattern='create', |
tests/test_jsondatastore.py | ||
---|---|---|
239 | 239 |
resp = app.get('/jsondatastore/foobar/data/', |
240 | 240 |
params={'q': 'na'}) |
241 | 241 |
assert resp.json['data'] == [] |
242 | ||
243 |
def test_jsondatastore_search(app, jsondatastore): |
|
244 |
jsondatastore.text_value_template = '{{name}}' |
|
245 |
jsondatastore.save() |
|
246 |
resp = app.post_json('/jsondatastore/foobar/data/create?name_id=name1', |
|
247 |
params={'me': '1', 'name': 'foÖ', 'url': 'url1'}) |
|
248 |
resp = app.post_json('/jsondatastore/foobar/data/create?name_id=name2', |
|
249 |
params={'me': '2', 'name': 'foo', 'url': 'url2'}) |
|
250 |
resp = app.post_json('/jsondatastore/foobar/data/create?name_id=name1', |
|
251 |
params={'me': '3', 'name': 'bar', 'url': 'url3'}) |
|
252 | ||
253 |
# JSON conformes with combo/apps/search/README |
|
254 |
resp = app.get('/jsondatastore/foobar/search/', |
|
255 |
params={'me': '2'}) |
|
256 |
assert resp.json['data'] == [{'text': 'foo', 'url': 'url2', 'description': None}] |
|
257 | ||
258 |
# specify different fields to return |
|
259 |
resp = app.get('/jsondatastore/foobar/search/', |
|
260 |
params={'me': '2', |
|
261 |
'url_field': 'name', |
|
262 |
'text_field': 'me', |
|
263 |
'description_field': 'me'}) |
|
264 |
assert resp.json['data'] == [{'url': 'foo', 'text': '2', 'description': '2'}] |
|
265 | ||
266 |
# filter on main text field |
|
267 |
resp = app.get('/jsondatastore/foobar/search/', |
|
268 |
params={'q': 'Oô'}) |
|
269 |
assert sorted([d['url'] for d in resp.json['data']]) == ['url1', 'url2'] |
|
242 |
- |