Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created date serielizer field for timestamp representation
- Loading branch information
1 parent
53165d7
commit fbb77a0
Showing
5 changed files
with
122 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import, unicode_literals, print_function | ||
import datetime | ||
import time | ||
|
||
from django.utils import six | ||
|
||
from rest_framework.fields import IntegerField | ||
|
||
|
||
def datetime_to_timestamp(value): | ||
return int(time.mktime(value.timetuple()) * 1000) | ||
|
||
|
||
class DateToTimeStampField(IntegerField): | ||
|
||
def to_internal_value(self, data): | ||
if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH: | ||
self.fail('max_string_length') | ||
|
||
try: | ||
data = int(self.re_decimal.sub('', str(data))) | ||
except (ValueError, TypeError): | ||
self.fail('invalid') | ||
|
||
date = datetime.datetime.fromtimestamp(data / 1e3).date() | ||
return date | ||
|
||
def to_representation(self, value): | ||
if not value: | ||
return None | ||
|
||
assert not isinstance(value, datetime.datetime), ( | ||
'Expected a `date`, but got a `datetime`. Refusing to coerce, ' | ||
'as this may mean losing timezone information. Use a custom ' | ||
'read-only field and deal with timezone issues explicitly.' | ||
) | ||
|
||
return datetime_to_timestamp(value) |
19 changes: 19 additions & 0 deletions
19
tests/django_rest_tools_app/migrations/0002_location_date.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('django_rest_tools_app', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='location', | ||
name='date', | ||
field=models.DateField(null=True, blank=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters