0001-jsondatastore-add-a-new-get_by_attribute-endpoint-20.patch
passerelle/apps/jsondatastore/models.py | ||
---|---|---|
24 | 24 |
from jsonfield import JSONField |
25 | 25 | |
26 | 26 |
from passerelle.base.models import BaseResource |
27 |
from passerelle.utils.api import endpoint |
|
27 |
from passerelle.utils.api import endpoint, APIError
|
|
28 | 28 | |
29 | 29 | |
30 | 30 |
def get_hex_uuid(): |
... | ... | |
116 | 116 |
# delete() would collide with Model.delete() |
117 | 117 |
self.get_data_object(uuid, name_id).delete() |
118 | 118 |
return {} |
119 | ||
120 |
@endpoint(perm='can_access', name='data', |
|
121 |
pattern=r'by/(?P<attribute>[\w-]+)/$', |
|
122 |
example_pattern='by/{attribute}/', |
|
123 |
description=_('Get a single object by attribute'), |
|
124 |
parameters={'attribute': {'description': _('Attribute name'), 'example_value': 'code'}, |
|
125 |
'value': {'description': _('Attribute value'), 'example_value': '12345'}, |
|
126 |
}, |
|
127 |
) |
|
128 |
def get_by_attribute(self, request, attribute, value, name_id=None): |
|
129 |
objects = JsonData.objects.filter(datastore=self) |
|
130 |
if name_id: |
|
131 |
objects = objects.filter(name_id=name_id) |
|
132 |
for data in objects: |
|
133 |
if data.content and data.content.get(attribute) == value: |
|
134 |
return {'id': data.uuid, 'text': data.text, 'content': data.content} |
|
135 |
raise APIError('no such object') |
tests/test_jsondatastore.py | ||
---|---|---|
105 | 105 |
resp = app.get('/jsondatastore/foobar/data/') |
106 | 106 |
assert len(resp.json['data']) == 1 |
107 | 107 |
assert resp.json['data'][0]['text'] == 'bar' |
108 | ||
109 |
def test_jsondatastore_get_by_attribute(app, jsondatastore): |
|
110 |
resp = app.post_json('/jsondatastore/foobar/data/create', params={'foo': 'bar'}) |
|
111 |
uuid = resp.json['id'] |
|
112 |
resp = app.post_json('/jsondatastore/foobar/data/create', params={'foo': 'bar2'}) |
|
113 |
uuid2 = resp.json['id'] |
|
114 | ||
115 |
resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar'}) |
|
116 |
assert resp.json['id'] == uuid |
|
117 | ||
118 |
resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar2'}) |
|
119 |
assert resp.json['id'] == uuid2 |
|
120 | ||
121 |
resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar3'}) |
|
122 |
assert resp.json['err'] == 1 |
|
123 | ||
124 |
resp = app.post_json('/jsondatastore/foobar/data/create?name_id=xxx', params={'foo': 'bar3'}) |
|
125 |
uuid = resp.json['id'] |
|
126 |
resp = app.post_json('/jsondatastore/foobar/data/create?name_id=yyy', params={'foo': 'bar3'}) |
|
127 |
uuid2 = resp.json['id'] |
|
128 | ||
129 |
resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar3', 'name_id': 'xxx'}) |
|
130 |
assert resp.json['id'] == uuid |
|
131 | ||
132 |
resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar3', 'name_id': 'yyy'}) |
|
133 |
assert resp.json['id'] == uuid2 |
|
134 | ||
135 |
resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar3', 'name_id': 'zzz'}) |
|
136 |
assert resp.json['err'] == 1 |
|
108 |
- |