From 265fe1a84be3c90d55cc1ff9df2acfa31e8d8617 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             | 25 +++++++++++++++++++++++++
 2 files changed, 37 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..131729a 100644
--- a/tests/test_jsondatastore.py
+++ b/tests/test_jsondatastore.py
@@ -1,3 +1,5 @@
+import datetime
+import mock
 import pytest
 
 from django.contrib.contenttypes.models import ContentType
@@ -148,3 +150,26 @@ 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):
+    creation_time = datetime.datetime(2019, 1, 1, 0, 0, 0)
+
+    with mock.patch('django.utils.timezone.now') as mocked:
+        mocked.return_value = creation_time
+        resp = app.post_json('/jsondatastore/foobar/data/create', params={'foo': 'bar'})
+        uuid = resp.json['id']
+        update_time = creation_time + datetime.timedelta(1)
+        mocked.return_value = update_time
+
+        resp = app.post_json('/jsondatastore/foobar/data/%s/' % uuid, params={'foo': 'bar2'})
+        assert resp.json['creation_datetime'].strip('Z') == creation_time.isoformat()
+        assert resp.json['last_update_datetime'].strip('Z') == update_time.isoformat()
+
+        resp = app.get('/jsondatastore/foobar/data/by/foo/', params={'value': 'bar2'})
+        assert resp.json['creation_datetime'].strip('Z') == creation_time.isoformat()
+        assert resp.json['last_update_datetime'].strip('Z') == update_time.isoformat()
+
+        resp = app.get('/jsondatastore/foobar/data/')
+        assert resp.json['data'][0]['creation_datetime'].strip('Z') == creation_time.isoformat()
+        assert resp.json['data'][0]['last_update_datetime'].strip('Z') == update_time.isoformat()
-- 
2.20.1

