Skip to content

Commit

Permalink
Return datetime string with UTC and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
berggren committed Nov 12, 2014
1 parent 8fd8e92 commit 9d869fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion timesketch/apps/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def _test_get_resources(self, request_data=None, expected_keys=None):
response_dict = self.deserialize(response)
self.assertValidJSONResponse(response)
self.assertKeys(response_dict['objects'][0], expected_keys)
return response_dict

def _test_post_resources(self, request_data=None, expected_keys=None):
"""Send a request to the API to create a resource."""
Expand Down Expand Up @@ -227,9 +228,10 @@ class CommentResourceTest(BaseResourceTest):
u'resource_uri'])

def test_get_resources(self):
self._test_get_resources(
response = self._test_get_resources(
request_data=self.request_get_data,
expected_keys=self.expected_get_keys)
self.assertTrue("Z" in response['objects'][0]['created'])

def test_post_resources(self):
self._test_post_resources(
Expand Down
24 changes: 19 additions & 5 deletions timesketch/apps/api/v1_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import json

from django.contrib.auth.models import User
from tastypie.resources import Resource
from tastypie.resources import ModelResource
from tastypie import fields
from tastypie import utils
from tastypie.authorization import Authorization
from tastypie.authentication import SessionAuthentication
from tastypie import fields, utils
from tastypie.resources import Resource
from tastypie.resources import ModelResource
from tastypie.serializers import Serializer
from pyelasticsearch.exceptions import ElasticHttpNotFoundError

from timesketch.lib.datastores import elasticsearch_datastore
Expand Down Expand Up @@ -72,6 +74,17 @@ def __setattr__(self, name, value):
self.__dict__['_data'][name] = value


class DateTimeSerializer(Serializer):
"""
Serializer to format the datetime output.
All django datetime fields in the database are UTC. Make the output format
reflect this.
"""
def format_datetime(self, data):
return data.strftime("%Y-%m-%dT%H:%M:%SZ")


class UserProfileResource(ModelResource):
"""Model resource for UserProfile."""
class Meta:
Expand Down Expand Up @@ -213,14 +226,15 @@ class CommentResource(ModelResource):
body = fields.CharField(attribute='body', null=True)
datastore_index = fields.CharField(attribute='datastore_index', null=True)
datastore_id = fields.CharField(attribute='datastore_id', null=True)
created = fields.CharField(attribute='created', default=utils.now)
updated = fields.CharField(attribute='updated')
created = fields.DateTimeField(attribute='created', default=utils.now)
updated = fields.DateTimeField(attribute='updated')

class Meta:
resource_name = 'comment'
always_return_data = True
authorization = Authorization()
authentication = SessionAuthentication()
serializer = DateTimeSerializer()

def obj_get_list(self, bundle, **kwargs):
datastore_index = bundle.request.GET['index']
Expand Down
2 changes: 1 addition & 1 deletion timesketch/apps/ui/templates/event.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<img width="40px" class="img-rounded" src="{{ comment.user.profile.avatar }}">
</div>
<div class="comment-name">
{{ comment.user.first_name }} {{ comment.user.last_name }}
{{ comment.user.username }}
<span class="comment-timestamp">{{ comment.created | date:'mediumDate' }}</span>
</div>
<div class="comment-body">
Expand Down

0 comments on commit 9d869fd

Please sign in to comment.