Clean ups to datetime formatting

This commit is contained in:
Tom Christie 2013-03-06 12:19:39 +00:00
parent 4f7b028a0a
commit 1106596c80
9 changed files with 182 additions and 128 deletions

View File

@ -2,7 +2,7 @@
# Serializer fields # Serializer fields
> Each field in a Form class is responsible not only for validating data, but also for "cleaning" it -- normalizing it to a consistent format. > Each field in a Form class is responsible not only for validating data, but also for "cleaning" it — normalizing it to a consistent format.
> >
> — [Django documentation][cite] > — [Django documentation][cite]
@ -199,10 +199,10 @@ If you want to override this behavior, you'll need to declare the `DateTimeField
**Signature:** `DateTimeField(format=None, input_formats=None)` **Signature:** `DateTimeField(format=None, input_formats=None)`
* `format` - A string representing the output format. If not specified, the `DATETIME_FORMAT` setting will be used, which defaults to `'iso8601'`. * `format` - A string representing the output format. If not specified, the `DATETIME_FORMAT` setting will be used, which defaults to `'iso-8601'`.
* `input_formats` - A list of strings representing the input formats which may be used to parse the date. If not specified, the `DATETIME_INPUT_FORMATS` setting will be used, which defaults to `['iso8601']`. * `input_formats` - A list of strings representing the input formats which may be used to parse the date. If not specified, the `DATETIME_INPUT_FORMATS` setting will be used, which defaults to `['iso-8601']`.
DateTime format strings may either be [python strftime formats][strftime] which explicitly specifiy the format, or the special string `'is8601'`, which indicates that [ISO 8601][iso8601] style datetimes should be used. (eg `'2013-01-29T12:34:56.000000'`) DateTime format strings may either be [python strftime formats][strftime] which explicitly specifiy the format, or the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style datetimes should be used. (eg `'2013-01-29T12:34:56.000000'`)
## DateField ## DateField
@ -212,10 +212,10 @@ Corresponds to `django.db.models.fields.DateField`
**Signature:** `DateField(format=None, input_formats=None)` **Signature:** `DateField(format=None, input_formats=None)`
* `format` - A string representing the output format. If not specified, the `DATE_FORMAT` setting will be used, which defaults to `'iso8601'`. * `format` - A string representing the output format. If not specified, the `DATE_FORMAT` setting will be used, which defaults to `'iso-8601'`.
* `input_formats` - A list of strings representing the input formats which may be used to parse the date. If not specified, the `DATE_INPUT_FORMATS` setting will be used, which defaults to `['iso8601']`. * `input_formats` - A list of strings representing the input formats which may be used to parse the date. If not specified, the `DATE_INPUT_FORMATS` setting will be used, which defaults to `['iso-8601']`.
Date format strings may either be [python strftime formats][strftime] which explicitly specifiy the format, or the special string `'is8601'`, which indicates that [ISO 8601][iso8601] style dates should be used. (eg `'2013-01-29'`) Date format strings may either be [python strftime formats][strftime] which explicitly specifiy the format, or the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style dates should be used. (eg `'2013-01-29'`)
## TimeField ## TimeField
@ -227,10 +227,10 @@ Corresponds to `django.db.models.fields.TimeField`
**Signature:** `TimeField(format=None, input_formats=None)` **Signature:** `TimeField(format=None, input_formats=None)`
* `format` - A string representing the output format. If not specified, the `TIME_FORMAT` setting will be used, which defaults to `'iso8601'`. * `format` - A string representing the output format. If not specified, the `TIME_FORMAT` setting will be used, which defaults to `'iso-8601'`.
* `input_formats` - A list of strings representing the input formats which may be used to parse the date. If not specified, the `TIME_INPUT_FORMATS` setting will be used, which defaults to `['iso8601']`. * `input_formats` - A list of strings representing the input formats which may be used to parse the date. If not specified, the `TIME_INPUT_FORMATS` setting will be used, which defaults to `['iso-8601']`.
Time format strings may either be [python strftime formats][strftime] which explicitly specifiy the format, or the special string `'is8601'`, which indicates that [ISO 8601][iso8601] style times should be used. (eg `'12:34:56.000000'`) Time format strings may either be [python strftime formats][strftime] which explicitly specifiy the format, or the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style times should be used. (eg `'12:34:56.000000'`)
## IntegerField ## IntegerField

View File

@ -34,7 +34,11 @@ The `api_settings` object will check for any user-defined settings, and otherwis
# API Reference # API Reference
## DEFAULT_RENDERER_CLASSES ## API policy settings
*The following settings control the basic API policies, and are applied to every `APIView` class based view, or `@api_view` function based view.*
#### DEFAULT_RENDERER_CLASSES
A list or tuple of renderer classes, that determines the default set of renderers that may be used when returning a `Response` object. A list or tuple of renderer classes, that determines the default set of renderers that may be used when returning a `Response` object.
@ -45,7 +49,7 @@ Default:
'rest_framework.renderers.BrowsableAPIRenderer', 'rest_framework.renderers.BrowsableAPIRenderer',
) )
## DEFAULT_PARSER_CLASSES #### DEFAULT_PARSER_CLASSES
A list or tuple of parser classes, that determines the default set of parsers used when accessing the `request.DATA` property. A list or tuple of parser classes, that determines the default set of parsers used when accessing the `request.DATA` property.
@ -57,7 +61,7 @@ Default:
'rest_framework.parsers.MultiPartParser' 'rest_framework.parsers.MultiPartParser'
) )
## DEFAULT_AUTHENTICATION_CLASSES #### DEFAULT_AUTHENTICATION_CLASSES
A list or tuple of authentication classes, that determines the default set of authenticators used when accessing the `request.user` or `request.auth` properties. A list or tuple of authentication classes, that determines the default set of authenticators used when accessing the `request.user` or `request.auth` properties.
@ -68,7 +72,7 @@ Default:
'rest_framework.authentication.BasicAuthentication' 'rest_framework.authentication.BasicAuthentication'
) )
## DEFAULT_PERMISSION_CLASSES #### DEFAULT_PERMISSION_CLASSES
A list or tuple of permission classes, that determines the default set of permissions checked at the start of a view. A list or tuple of permission classes, that determines the default set of permissions checked at the start of a view.
@ -78,59 +82,77 @@ Default:
'rest_framework.permissions.AllowAny', 'rest_framework.permissions.AllowAny',
) )
## DEFAULT_THROTTLE_CLASSES #### DEFAULT_THROTTLE_CLASSES
A list or tuple of throttle classes, that determines the default set of throttles checked at the start of a view. A list or tuple of throttle classes, that determines the default set of throttles checked at the start of a view.
Default: `()` Default: `()`
## DEFAULT_CONTENT_NEGOTIATION_CLASS #### DEFAULT_CONTENT_NEGOTIATION_CLASS
A content negotiation class, that determines how a renderer is selected for the response, given an incoming request. A content negotiation class, that determines how a renderer is selected for the response, given an incoming request.
Default: `'rest_framework.negotiation.DefaultContentNegotiation'` Default: `'rest_framework.negotiation.DefaultContentNegotiation'`
## DEFAULT_MODEL_SERIALIZER_CLASS ---
## Generic view settings
*The following settings control the behavior of the generic class based views.*
#### DEFAULT_MODEL_SERIALIZER_CLASS
A class that determines the default type of model serializer that should be used by a generic view if `model` is specified, but `serializer_class` is not provided. A class that determines the default type of model serializer that should be used by a generic view if `model` is specified, but `serializer_class` is not provided.
Default: `'rest_framework.serializers.ModelSerializer'` Default: `'rest_framework.serializers.ModelSerializer'`
## DEFAULT_PAGINATION_SERIALIZER_CLASS #### DEFAULT_PAGINATION_SERIALIZER_CLASS
A class the determines the default serialization style for paginated responses. A class the determines the default serialization style for paginated responses.
Default: `rest_framework.pagination.PaginationSerializer` Default: `rest_framework.pagination.PaginationSerializer`
## FILTER_BACKEND #### FILTER_BACKEND
The filter backend class that should be used for generic filtering. If set to `None` then generic filtering is disabled. The filter backend class that should be used for generic filtering. If set to `None` then generic filtering is disabled.
## PAGINATE_BY #### PAGINATE_BY
The default page size to use for pagination. If set to `None`, pagination is disabled by default. The default page size to use for pagination. If set to `None`, pagination is disabled by default.
Default: `None` Default: `None`
## PAGINATE_BY_PARAM #### PAGINATE_BY_PARAM
The name of a query parameter, which can be used by the client to overide the default page size to use for pagination. If set to `None`, clients may not override the default page size. The name of a query parameter, which can be used by the client to overide the default page size to use for pagination. If set to `None`, clients may not override the default page size.
Default: `None` Default: `None`
## UNAUTHENTICATED_USER ---
## Authentication settings
*The following settings control the behavior of unauthenticated requests.*
#### UNAUTHENTICATED_USER
The class that should be used to initialize `request.user` for unauthenticated requests. The class that should be used to initialize `request.user` for unauthenticated requests.
Default: `django.contrib.auth.models.AnonymousUser` Default: `django.contrib.auth.models.AnonymousUser`
## UNAUTHENTICATED_TOKEN #### UNAUTHENTICATED_TOKEN
The class that should be used to initialize `request.auth` for unauthenticated requests. The class that should be used to initialize `request.auth` for unauthenticated requests.
Default: `None` Default: `None`
## FORM_METHOD_OVERRIDE ---
## Browser overrides
*The following settings provide URL or form-based overrides of the default browser behavior.*
#### FORM_METHOD_OVERRIDE
The name of a form field that may be used to override the HTTP method of the form. The name of a form field that may be used to override the HTTP method of the form.
@ -138,7 +160,7 @@ If the value of this setting is `None` then form method overloading will be disa
Default: `'_method'` Default: `'_method'`
## FORM_CONTENT_OVERRIDE #### FORM_CONTENT_OVERRIDE
The name of a form field that may be used to override the content of the form payload. Must be used together with `FORM_CONTENTTYPE_OVERRIDE`. The name of a form field that may be used to override the content of the form payload. Must be used together with `FORM_CONTENTTYPE_OVERRIDE`.
@ -146,7 +168,7 @@ If either setting is `None` then form content overloading will be disabled.
Default: `'_content'` Default: `'_content'`
## FORM_CONTENTTYPE_OVERRIDE #### FORM_CONTENTTYPE_OVERRIDE
The name of a form field that may be used to override the content type of the form payload. Must be used together with `FORM_CONTENT_OVERRIDE`. The name of a form field that may be used to override the content type of the form payload. Must be used together with `FORM_CONTENT_OVERRIDE`.
@ -154,7 +176,7 @@ If either setting is `None` then form content overloading will be disabled.
Default: `'_content_type'` Default: `'_content_type'`
## URL_ACCEPT_OVERRIDE #### URL_ACCEPT_OVERRIDE
The name of a URL parameter that may be used to override the HTTP `Accept` header. The name of a URL parameter that may be used to override the HTTP `Accept` header.
@ -162,52 +184,62 @@ If the value of this setting is `None` then URL accept overloading will be disab
Default: `'accept'` Default: `'accept'`
## URL_FORMAT_OVERRIDE #### URL_FORMAT_OVERRIDE
The name of a URL parameter that may be used to override the default `Accept` header based content negotiation. The name of a URL parameter that may be used to override the default `Accept` header based content negotiation.
Default: `'format'` Default: `'format'`
## FORMAT_SUFFIX_KWARG ---
## Date/Time formatting
*The following settings are used to control how date and time representations may be parsed and rendered.*
#### DATETIME_FORMAT
A format string that should be used by default for rendering the output of `DateTimeField` serializer fields.
Default: `'iso-8601'`
#### DATETIME_INPUT_FORMATS
A list of format strings that should be used by default for parsing inputs to `DateTimeField` serializer fields.
Default: `['iso-8601']`
#### DATE_FORMAT
A format string that should be used by default for rendering the output of `DateField` serializer fields.
Default: `'iso-8601'`
#### DATE_INPUT_FORMATS
A list of format strings that should be used by default for parsing inputs to `DateField` serializer fields.
Default: `['iso-8601']`
#### TIME_FORMAT
A format string that should be used by default for rendering the output of `TimeField` serializer fields.
Default: `'iso-8601'`
#### TIME_INPUT_FORMATS
A list of format strings that should be used by default for parsing inputs to `TimeField` serializer fields.
Default: `['iso-8601']`
---
## Miscellaneous settings
#### FORMAT_SUFFIX_KWARG
The name of a parameter in the URL conf that may be used to provide a format suffix. The name of a parameter in the URL conf that may be used to provide a format suffix.
Default: `'format'` Default: `'format'`
## DATETIME_FORMAT
A format string that should be used by default for `DateTimeField` serializer fields.
Default: `'iso8601'`
## DATETIME_INPUT_FORMATS
A list of format strings that should be used by default for parsing inputs to `DateTimeField` serializer fields.
Default: `['iso8601']`
## DATE_FORMAT
A format string that should be used by default for `DateField` serializer fields.
Default: `'iso8601'`
## DATE_INPUT_FORMATS
A list of format strings that should be used by default for parsing inputs to `DateField` serializer fields.
Default: `['iso8601']`
## TIME_FORMAT
A format string that should be used by default for `TimeField` serializer fields.
Default: `'iso8601'`
## TIME_INPUT_FORMATS
A list of format strings that should be used by default for parsing inputs to `TimeField` serializer fields.
Default: `['iso8601']`
[cite]: http://www.python.org/dev/peps/pep-0020/ [cite]: http://www.python.org/dev/peps/pep-0020/

View File

@ -42,7 +42,7 @@ You can determine your currently installed version using `pip freeze`:
### Master ### Master
* Support for custom input and output formats for `DateField`, `DateTimeField` and `TimeField` * Support for custom input and output formats for `DateField`, `DateTimeField` and `TimeField`.
* Cleanup: Request authentication is no longer lazily evaluated, instead authentication is always run, which results in more consistent, obvious behavior. Eg. Supplying bad auth credentials will now always return an error response, even if no permissions are set on the view. * Cleanup: Request authentication is no longer lazily evaluated, instead authentication is always run, which results in more consistent, obvious behavior. Eg. Supplying bad auth credentials will now always return an error response, even if no permissions are set on the view.
* Bugfix for serializer data being uncacheable with pickle protocol 0. * Bugfix for serializer data being uncacheable with pickle protocol 0.
* Bugfixes for model field validation edge-cases. * Bugfixes for model field validation edge-cases.

View File

@ -6,4 +6,4 @@ VERSION = __version__ # synonym
HTTP_HEADER_ENCODING = 'iso-8859-1' HTTP_HEADER_ENCODING = 'iso-8859-1'
# Default input and output format # Default input and output format
ISO8601 = 'iso-8601' ISO_8601 = 'iso-8601'

View File

@ -14,13 +14,12 @@ from django.forms import widgets
from django.utils.encoding import is_protected_type from django.utils.encoding import is_protected_type
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from rest_framework import ISO8601 from rest_framework import ISO_8601
from rest_framework.compat import timezone, parse_date, parse_datetime, parse_time from rest_framework.compat import timezone, parse_date, parse_datetime, parse_time
from rest_framework.compat import BytesIO from rest_framework.compat import BytesIO
from rest_framework.compat import six from rest_framework.compat import six
from rest_framework.compat import smart_text from rest_framework.compat import smart_text
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework.utils.dates import get_readable_date_format
def is_simple_callable(obj): def is_simple_callable(obj):
@ -52,6 +51,46 @@ def get_component(obj, attr_name):
return val return val
def readable_datetime_formats(formats):
format = ', '.join(formats).replace(ISO_8601, 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]')
return humanize_strptime(format)
def readable_date_formats(formats):
format = ', '.join(formats).replace(ISO_8601, 'YYYY[-MM[-DD]]')
return humanize_strptime(format)
def readable_time_formats(formats):
format = ', '.join(formats).replace(ISO_8601, 'hh:mm[:ss[.uuuuuu]]')
return humanize_strptime(format)
def humanize_strptime(format_string):
# Note that we're missing some of the locale specific mappings that
# don't really make sense.
mapping = {
"%Y": "YYYY",
"%y": "YY",
"%m": "MM",
"%b": "[Jan-Dec]",
"%B": "[January-December]",
"%d": "DD",
"%H": "hh",
"%I": "hh", # Requires '%p' to differentiate from '%H'.
"%M": "mm",
"%S": "ss",
"%f": "uuuuuu",
"%a": "[Mon-Sun]",
"%A": "[Monday-Sunday]",
"%p": "[AM|PM]",
"%z": "[+HHMM|-HHMM]"
}
for key, val in mapping.items():
format_string = format_string.replace(key, val)
return format_string
class Field(object): class Field(object):
read_only = True read_only = True
creation_counter = 0 creation_counter = 0
@ -453,11 +492,11 @@ class DateField(WritableField):
} }
empty = None empty = None
input_formats = api_settings.DATE_INPUT_FORMATS input_formats = api_settings.DATE_INPUT_FORMATS
output_format = api_settings.DATE_OUTPUT_FORMAT format = api_settings.DATE_FORMAT
def __init__(self, input_formats=None, output_format=None, *args, **kwargs): def __init__(self, input_formats=None, format=None, *args, **kwargs):
self.input_formats = input_formats if input_formats is not None else self.input_formats self.input_formats = input_formats if input_formats is not None else self.input_formats
self.output_format = output_format if output_format is not None else self.output_format self.format = format if format is not None else self.format
super(DateField, self).__init__(*args, **kwargs) super(DateField, self).__init__(*args, **kwargs)
def from_native(self, value): def from_native(self, value):
@ -475,7 +514,7 @@ class DateField(WritableField):
return value return value
for format in self.input_formats: for format in self.input_formats:
if format.lower() == ISO8601: if format.lower() == ISO_8601:
try: try:
parsed = parse_date(value) parsed = parse_date(value)
except (ValueError, TypeError): except (ValueError, TypeError):
@ -491,16 +530,15 @@ class DateField(WritableField):
else: else:
return parsed.date() return parsed.date()
date_input_formats = '; '.join(self.input_formats).replace(ISO8601, 'YYYY-MM-DD') msg = self.error_messages['invalid'] % readable_date_formats(self.input_formats)
msg = self.error_messages['invalid'] % get_readable_date_format(date_input_formats)
raise ValidationError(msg) raise ValidationError(msg)
def to_native(self, value): def to_native(self, value):
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
value = value.date() value = value.date()
if self.output_format.lower() == ISO8601: if self.format.lower() == ISO_8601:
return value.isoformat() return value.isoformat()
return value.strftime(self.output_format) return value.strftime(self.format)
class DateTimeField(WritableField): class DateTimeField(WritableField):
@ -513,11 +551,11 @@ class DateTimeField(WritableField):
} }
empty = None empty = None
input_formats = api_settings.DATETIME_INPUT_FORMATS input_formats = api_settings.DATETIME_INPUT_FORMATS
output_format = api_settings.DATETIME_OUTPUT_FORMAT format = api_settings.DATETIME_FORMAT
def __init__(self, input_formats=None, output_format=None, *args, **kwargs): def __init__(self, input_formats=None, format=None, *args, **kwargs):
self.input_formats = input_formats if input_formats is not None else self.input_formats self.input_formats = input_formats if input_formats is not None else self.input_formats
self.output_format = output_format if output_format is not None else self.output_format self.format = format if format is not None else self.format
super(DateTimeField, self).__init__(*args, **kwargs) super(DateTimeField, self).__init__(*args, **kwargs)
def from_native(self, value): def from_native(self, value):
@ -541,7 +579,7 @@ class DateTimeField(WritableField):
return value return value
for format in self.input_formats: for format in self.input_formats:
if format.lower() == ISO8601: if format.lower() == ISO_8601:
try: try:
parsed = parse_datetime(value) parsed = parse_datetime(value)
except (ValueError, TypeError): except (ValueError, TypeError):
@ -557,14 +595,13 @@ class DateTimeField(WritableField):
else: else:
return parsed return parsed
datetime_input_formats = '; '.join(self.input_formats).replace(ISO8601, 'YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]') msg = self.error_messages['invalid'] % readable_datetime_formats(self.input_formats)
msg = self.error_messages['invalid'] % get_readable_date_format(datetime_input_formats)
raise ValidationError(msg) raise ValidationError(msg)
def to_native(self, value): def to_native(self, value):
if self.output_format.lower() == ISO8601: if self.format.lower() == ISO_8601:
return value.isoformat() return value.isoformat()
return value.strftime(self.output_format) return value.strftime(self.format)
class TimeField(WritableField): class TimeField(WritableField):
@ -577,11 +614,11 @@ class TimeField(WritableField):
} }
empty = None empty = None
input_formats = api_settings.TIME_INPUT_FORMATS input_formats = api_settings.TIME_INPUT_FORMATS
output_format = api_settings.TIME_OUTPUT_FORMAT format = api_settings.TIME_FORMAT
def __init__(self, input_formats=None, output_format=None, *args, **kwargs): def __init__(self, input_formats=None, format=None, *args, **kwargs):
self.input_formats = input_formats if input_formats is not None else self.input_formats self.input_formats = input_formats if input_formats is not None else self.input_formats
self.output_format = output_format if output_format is not None else self.output_format self.format = format if format is not None else self.format
super(TimeField, self).__init__(*args, **kwargs) super(TimeField, self).__init__(*args, **kwargs)
def from_native(self, value): def from_native(self, value):
@ -592,7 +629,7 @@ class TimeField(WritableField):
return value return value
for format in self.input_formats: for format in self.input_formats:
if format.lower() == ISO8601: if format.lower() == ISO_8601:
try: try:
parsed = parse_time(value) parsed = parse_time(value)
except (ValueError, TypeError): except (ValueError, TypeError):
@ -608,16 +645,15 @@ class TimeField(WritableField):
else: else:
return parsed.time() return parsed.time()
time_input_formats = '; '.join(self.input_formats).replace(ISO8601, 'HH:MM[:ss[.uuuuuu]]') msg = self.error_messages['invalid'] % readable_time_formats(self.input_formats)
msg = self.error_messages['invalid'] % get_readable_date_format(time_input_formats)
raise ValidationError(msg) raise ValidationError(msg)
def to_native(self, value): def to_native(self, value):
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
value = value.time() value = value.time()
if self.output_format.lower() == ISO8601: if self.format.lower() == ISO_8601:
return value.isoformat() return value.isoformat()
return value.strftime(self.output_format) return value.strftime(self.format)
class IntegerField(WritableField): class IntegerField(WritableField):

View File

@ -22,7 +22,7 @@ from __future__ import unicode_literals
from django.conf import settings from django.conf import settings
from django.utils import importlib from django.utils import importlib
from rest_framework import ISO8601 from rest_framework import ISO_8601
from rest_framework.compat import six from rest_framework.compat import six
@ -82,19 +82,19 @@ DEFAULTS = {
# Input and output formats # Input and output formats
'DATE_INPUT_FORMATS': ( 'DATE_INPUT_FORMATS': (
ISO8601, ISO_8601,
), ),
'DATE_OUTPUT_FORMAT': ISO8601, 'DATE_FORMAT': ISO_8601,
'DATETIME_INPUT_FORMATS': ( 'DATETIME_INPUT_FORMATS': (
ISO8601, ISO_8601,
), ),
'DATETIME_OUTPUT_FORMAT': ISO8601, 'DATETIME_FORMAT': ISO_8601,
'TIME_INPUT_FORMATS': ( 'TIME_INPUT_FORMATS': (
ISO8601, ISO_8601,
), ),
'TIME_OUTPUT_FORMAT': ISO8601, 'TIME_FORMAT': ISO_8601,
} }

View File

@ -59,7 +59,7 @@ class BasicFieldTests(TestCase):
PK fields other than AutoField fields should not be read_only by default. PK fields other than AutoField fields should not be read_only by default.
""" """
serializer = CharPrimaryKeyModelSerializer() serializer = CharPrimaryKeyModelSerializer()
self.assertEquals(serializer.fields['id'].read_only, False) self.assertEqual(serializer.fields['id'].read_only, False)
class DateFieldTest(TestCase): class DateFieldTest(TestCase):
@ -134,7 +134,7 @@ class DateFieldTest(TestCase):
try: try:
f.from_native('1984-13-31') f.from_native('1984-13-31')
except validators.ValidationError as e: except validators.ValidationError as e:
self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY-MM-DD"]) self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"])
else: else:
self.fail("ValidationError was not properly raised") self.fail("ValidationError was not properly raised")
@ -147,7 +147,7 @@ class DateFieldTest(TestCase):
try: try:
f.from_native('1984 -- 31') f.from_native('1984 -- 31')
except validators.ValidationError as e: except validators.ValidationError as e:
self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY-MM-DD"]) self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"])
else: else:
self.fail("ValidationError was not properly raised") self.fail("ValidationError was not properly raised")
@ -165,7 +165,7 @@ class DateFieldTest(TestCase):
""" """
Make sure to_native() returns correct custom format. Make sure to_native() returns correct custom format.
""" """
f = serializers.DateField(output_format="%Y - %m.%d") f = serializers.DateField(format="%Y - %m.%d")
result_1 = f.to_native(datetime.date(1984, 7, 31)) result_1 = f.to_native(datetime.date(1984, 7, 31))
@ -221,7 +221,7 @@ class DateTimeFieldTest(TestCase):
try: try:
f.from_native('1984-07-31 04:31:59') f.from_native('1984-07-31 04:31:59')
except validators.ValidationError as e: except validators.ValidationError as e:
self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: YYYY -- HH:MM"]) self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: YYYY -- hh:mm"])
else: else:
self.fail("ValidationError was not properly raised") self.fail("ValidationError was not properly raised")
@ -253,7 +253,7 @@ class DateTimeFieldTest(TestCase):
f.from_native('04:61:59') f.from_native('04:61:59')
except validators.ValidationError as e: except validators.ValidationError as e:
self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: " self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: "
"YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]"]) "YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]"])
else: else:
self.fail("ValidationError was not properly raised") self.fail("ValidationError was not properly raised")
@ -267,7 +267,7 @@ class DateTimeFieldTest(TestCase):
f.from_native('04 -- 31') f.from_native('04 -- 31')
except validators.ValidationError as e: except validators.ValidationError as e:
self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: " self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: "
"YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]"]) "YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]"])
else: else:
self.fail("ValidationError was not properly raised") self.fail("ValidationError was not properly raised")
@ -291,7 +291,7 @@ class DateTimeFieldTest(TestCase):
""" """
Make sure to_native() returns correct custom format. Make sure to_native() returns correct custom format.
""" """
f = serializers.DateTimeField(output_format="%Y - %H:%M") f = serializers.DateTimeField(format="%Y - %H:%M")
result_1 = f.to_native(datetime.datetime(1984, 7, 31)) result_1 = f.to_native(datetime.datetime(1984, 7, 31))
result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31)) result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31))
@ -353,7 +353,7 @@ class TimeFieldTest(TestCase):
try: try:
f.from_native('04:31:59') f.from_native('04:31:59')
except validators.ValidationError as e: except validators.ValidationError as e:
self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: HH -- MM"]) self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: hh -- mm"])
else: else:
self.fail("ValidationError was not properly raised") self.fail("ValidationError was not properly raised")
@ -385,7 +385,7 @@ class TimeFieldTest(TestCase):
f.from_native('04:61:59') f.from_native('04:61:59')
except validators.ValidationError as e: except validators.ValidationError as e:
self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: " self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: "
"HH:MM[:ss[.uuuuuu]]"]) "hh:mm[:ss[.uuuuuu]]"])
else: else:
self.fail("ValidationError was not properly raised") self.fail("ValidationError was not properly raised")
@ -399,7 +399,7 @@ class TimeFieldTest(TestCase):
f.from_native('04 -- 31') f.from_native('04 -- 31')
except validators.ValidationError as e: except validators.ValidationError as e:
self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: " self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: "
"HH:MM[:ss[.uuuuuu]]"]) "hh:mm[:ss[.uuuuuu]]"])
else: else:
self.fail("ValidationError was not properly raised") self.fail("ValidationError was not properly raised")
@ -420,11 +420,11 @@ class TimeFieldTest(TestCase):
""" """
Make sure to_native() returns correct custom format. Make sure to_native() returns correct custom format.
""" """
f = serializers.TimeField(output_format="%H - %S [%f]") f = serializers.TimeField(format="%H - %S [%f]")
result_1 = f.to_native(datetime.time(4, 31)) result_1 = f.to_native(datetime.time(4, 31))
result_2 = f.to_native(datetime.time(4, 31, 59)) result_2 = f.to_native(datetime.time(4, 31, 59))
result_3 = f.to_native(datetime.time(4, 31, 59, 200)) result_3 = f.to_native(datetime.time(4, 31, 59, 200))
self.assertEqual('04 - 00 [000000]', result_1) self.assertEqual('04 - 00 [000000]', result_1)
self.assertEqual('04 - 59 [000000]', result_2) self.assertEqual('04 - 59 [000000]', result_2)
self.assertEqual('04 - 59 [000200]', result_3) self.assertEqual('04 - 59 [000200]', result_3)

View File

@ -1,14 +0,0 @@
def get_readable_date_format(date_format):
mapping = [("%Y", "YYYY"),
("%y", "YY"),
("%m", "MM"),
("%b", "[Jan through Dec]"),
("%B", "[January through December]"),
("%d", "DD"),
("%H", "HH"),
("%M", "MM"),
("%S", "SS"),
("%f", "uuuuuu")]
for k, v in mapping:
date_format = date_format.replace(k, v)
return date_format

View File

@ -1,6 +1,6 @@
[tox] [tox]
downloadcache = {toxworkdir}/cache/ downloadcache = {toxworkdir}/cache/
envlist = py3.3-django1.5,py3.2-django1.5,py2.7-django1.5,py2.7-django1.4,py2.7-django1.3,py2.6-django1.5,py2.6-django1.4,py2.6-django1.3 envlist = py3.3-django1.5,py3.2-django1.5,py2.7-django1.5,py2.6-django1.5,py2.7-django1.4,py2.6-django1.4,py2.7-django1.3,py2.6-django1.3
[testenv] [testenv]
commands = {envpython} rest_framework/runtests/runtests.py commands = {envpython} rest_framework/runtests/runtests.py