0001-jsondatastore-return-datetimes-in-results.patch
passerelle/apps/jsondatastore/models.py | ||
---|---|---|
54 | 54 |
self.text = template.render(context).strip() |
55 | 55 |
return super(JsonData, self).save(*args, **kwargs) |
56 | 56 | |
57 |
def to_json(self): |
|
58 |
return { |
|
59 |
'id': self.uuid, |
|
60 |
'text': self.text, |
|
61 |
'content': self.content, |
|
62 |
'creation_datetime': self.creation_datetime, |
|
63 |
'last_update_datetime': self.last_update_datetime, |
|
64 |
} |
|
65 | ||
57 | 66 | |
58 | 67 |
class JsonDataStore(BaseResource): |
59 | 68 |
category = _('Data Sources') |
... | ... | |
69 | 78 |
objects = JsonData.objects.filter(datastore=self) |
70 | 79 |
if name_id is not None: |
71 | 80 |
objects = objects.filter(name_id=name_id) |
72 |
return {'data': [{'id': x.uuid, 'text': x.text, 'content': x.content} for x in objects]}
|
|
81 |
return {'data': [x.to_json() for x in objects]}
|
|
73 | 82 | |
74 | 83 |
@endpoint(perm='can_access', methods=['post'], name='data', |
75 | 84 |
pattern=r'create$', |
... | ... | |
111 | 120 |
elif request.method == 'PATCH': |
112 | 121 |
data.content.update(json.loads(request.body)) |
113 | 122 |
data.save() |
114 |
return {'id': data.uuid, 'text': data.text, 'content': data.content}
|
|
123 |
return data.to_json()
|
|
115 | 124 | |
116 | 125 |
@endpoint(perm='can_access', methods=['post'], name='data', |
117 | 126 |
description=_('Delete'), |
... | ... | |
138 | 147 |
objects = objects.filter(name_id=name_id) |
139 | 148 |
for data in objects: |
140 | 149 |
if data.content and data.content.get(attribute) == value: |
141 |
return {'id': data.uuid, 'text': data.text, 'content': data.content}
|
|
150 |
return data.to_json()
|
|
142 | 151 |
raise APIError('no such object') |
tests/test_jsondatastore.py | ||
---|---|---|
1 |
import datetime |
|
2 |
import mock |
|
1 | 3 |
import pytest |
2 | 4 | |
3 | 5 |
from django.contrib.contenttypes.models import ContentType |
... | ... | |
148 | 150 | |
149 | 151 |
resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar3', 'name_id': 'zzz'}) |
150 | 152 |
assert resp.json['err'] == 1 |
153 | ||
154 | ||
155 |
def test_jsondatastore_datetimes(app, jsondatastore): |
|
156 |
creation_time = datetime.datetime(2019, 1, 1, 0, 0, 0) |
|
157 | ||
158 |
with mock.patch('django.utils.timezone.now') as mocked: |
|
159 |
mocked.return_value = creation_time |
|
160 |
resp = app.post_json('/jsondatastore/foobar/data/create', params={'foo': 'bar'}) |
|
161 |
uuid = resp.json['id'] |
|
162 |
update_time = creation_time + datetime.timedelta(1) |
|
163 |
mocked.return_value = update_time |
|
164 | ||
165 |
resp = app.post_json('/jsondatastore/foobar/data/%s/' % uuid, params={'foo': 'bar2'}) |
|
166 |
assert resp.json['creation_datetime'].strip('Z') == creation_time.isoformat() |
|
167 |
assert resp.json['last_update_datetime'].strip('Z') == update_time.isoformat() |
|
168 | ||
169 |
resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar2'}) |
|
170 |
assert resp.json['creation_datetime'].strip('Z') == creation_time.isoformat() |
|
171 |
assert resp.json['last_update_datetime'].strip('Z') == update_time.isoformat() |
|
172 | ||
173 |
resp = app.get('/jsondatastore/foobar/data/') |
|
174 |
assert resp.json['data'][0]['creation_datetime'].strip('Z') == creation_time.isoformat() |
|
175 |
assert resp.json['data'][0]['last_update_datetime'].strip('Z') == update_time.isoformat() |
|
151 |
- |