From eb1c4ad699d5ff5dd26bd58312366beee6cd1c0d Mon Sep 17 00:00:00 2001
From: Valentin Deniaud <vdeniaud@entrouvert.com>
Date: Wed, 2 Oct 2019 14:54:55 +0200
Subject: [PATCH] jsondatastore: return datetimes in results

---
 passerelle/apps/jsondatastore/models.py | 15 ++++++++++++---
 tests/test_jsondatastore.py             | 21 +++++++++++++++++++++
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/passerelle/apps/jsondatastore/models.py b/passerelle/apps/jsondatastore/models.py
index 62d60fe..b3fcc3c 100644
--- a/passerelle/apps/jsondatastore/models.py
+++ b/passerelle/apps/jsondatastore/models.py
@@ -54,6 +54,15 @@ class JsonData(models.Model):
             self.text = template.render(context).strip()
         return super(JsonData, self).save(*args, **kwargs)
 
+    def to_json(self):
+        return {
+            'id': self.uuid,
+            'text': self.text,
+            'content': self.content,
+            'creation_datetime': self.creation_datetime,
+            'last_update_datetime': self.last_update_datetime,
+        }
+
 
 class JsonDataStore(BaseResource):
     category = _('Data Sources')
@@ -69,7 +78,7 @@ class JsonDataStore(BaseResource):
         objects = JsonData.objects.filter(datastore=self)
         if name_id is not None:
             objects = objects.filter(name_id=name_id)
-        return {'data': [{'id': x.uuid, 'text': x.text, 'content': x.content} for x in objects]}
+        return {'data': [x.to_json() for x in objects]}
 
     @endpoint(perm='can_access', methods=['post'], name='data',
               pattern=r'create$',
@@ -111,7 +120,7 @@ class JsonDataStore(BaseResource):
         elif request.method == 'PATCH':
             data.content.update(json.loads(request.body))
             data.save()
-        return {'id': data.uuid, 'text': data.text, 'content': data.content}
+        return data.to_json()
 
     @endpoint(perm='can_access', methods=['post'], name='data',
               description=_('Delete'),
@@ -138,5 +147,5 @@ class JsonDataStore(BaseResource):
             objects = objects.filter(name_id=name_id)
         for data in objects:
             if data.content and data.content.get(attribute) == value:
-                return {'id': data.uuid, 'text': data.text, 'content': data.content}
+                return data.to_json()
         raise APIError('no such object')
diff --git a/tests/test_jsondatastore.py b/tests/test_jsondatastore.py
index 2fb3e14..87d8cd8 100644
--- a/tests/test_jsondatastore.py
+++ b/tests/test_jsondatastore.py
@@ -1,6 +1,7 @@
 import pytest
 
 from django.contrib.contenttypes.models import ContentType
+from django.core.serializers.json import DjangoJSONEncoder
 
 from passerelle.apps.jsondatastore.models import JsonDataStore, JsonData
 from passerelle.base.models import ApiUser, AccessRight
@@ -148,3 +149,23 @@ def test_jsondatastore_get_by_attribute(app, jsondatastore):
 
     resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar3', 'name_id': 'zzz'})
     assert resp.json['err'] == 1
+
+
+def test_jsondatastore_datetimes(app, jsondatastore):
+    encoder = DjangoJSONEncoder()
+    resp = app.post_json('/jsondatastore/foobar/data/create', params={'foo': 'bar'})
+    uuid = resp.json['id']
+    creation_datetime = jsondatastore.get_data_object(uuid).creation_datetime
+
+    resp = app.post_json('/jsondatastore/foobar/data/%s/' % uuid, params={'foo': 'bar2'})
+    last_update_datetime = jsondatastore.get_data_object(uuid).last_update_datetime
+    assert resp.json['creation_datetime'] == encoder.default(creation_datetime)
+    assert resp.json['last_update_datetime'] == encoder.default(last_update_datetime)
+
+    resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar2'})
+    assert resp.json['creation_datetime'] == encoder.default(creation_datetime)
+    assert resp.json['last_update_datetime'] == encoder.default(last_update_datetime)
+
+    resp = app.get('/jsondatastore/foobar/data/')
+    assert resp.json['data'][0]['creation_datetime'] == encoder.default(creation_datetime)
+    assert resp.json['data'][0]['last_update_datetime'] == encoder.default(last_update_datetime)
-- 
2.20.1

