mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-27 03:54:01 +03:00
Merge pull request #703 from tomchristie/datetime-formats
Datetime formats
This commit is contained in:
commit
6135df56c6
|
@ -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]
|
||||||
|
|
||||||
|
@ -181,12 +181,6 @@ Corresponds to `django.forms.fields.RegexField`
|
||||||
|
|
||||||
**Signature:** `RegexField(regex, max_length=None, min_length=None)`
|
**Signature:** `RegexField(regex, max_length=None, min_length=None)`
|
||||||
|
|
||||||
## DateField
|
|
||||||
|
|
||||||
A date representation.
|
|
||||||
|
|
||||||
Corresponds to `django.db.models.fields.DateField`
|
|
||||||
|
|
||||||
## DateTimeField
|
## DateTimeField
|
||||||
|
|
||||||
A date and time representation.
|
A date and time representation.
|
||||||
|
@ -203,12 +197,41 @@ If you want to override this behavior, you'll need to declare the `DateTimeField
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Comment
|
model = Comment
|
||||||
|
|
||||||
|
**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 `'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 `['iso-8601']`.
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
A date representation.
|
||||||
|
|
||||||
|
Corresponds to `django.db.models.fields.DateField`
|
||||||
|
|
||||||
|
**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 `'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 `['iso-8601']`.
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
A time representation.
|
A time representation.
|
||||||
|
|
||||||
|
Optionally takes `format` as parameter to replace the matching pattern.
|
||||||
|
|
||||||
Corresponds to `django.db.models.fields.TimeField`
|
Corresponds to `django.db.models.fields.TimeField`
|
||||||
|
|
||||||
|
**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 `'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 `['iso-8601']`.
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
An integer representation.
|
An integer representation.
|
||||||
|
@ -252,3 +275,5 @@ Django's regular [FILE_UPLOAD_HANDLERS] are used for handling uploaded files.
|
||||||
|
|
||||||
[cite]: https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.cleaned_data
|
[cite]: https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.cleaned_data
|
||||||
[FILE_UPLOAD_HANDLERS]: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FILE_UPLOAD_HANDLERS
|
[FILE_UPLOAD_HANDLERS]: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FILE_UPLOAD_HANDLERS
|
||||||
|
[strftime]: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
|
||||||
|
[iso8601]: http://www.w3.org/TR/NOTE-datetime
|
||||||
|
|
|
@ -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,13 +184,59 @@ 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.
|
||||||
|
|
||||||
|
|
|
@ -133,6 +133,10 @@ Run the tests:
|
||||||
|
|
||||||
./rest_framework/runtests/runtests.py
|
./rest_framework/runtests/runtests.py
|
||||||
|
|
||||||
|
To run the tests against all supported configurations, first install [the tox testing tool][tox] globally, using `pip install tox`, then simply run `tox`:
|
||||||
|
|
||||||
|
tox
|
||||||
|
|
||||||
## Support
|
## Support
|
||||||
|
|
||||||
For support please see the [REST framework discussion group][group], try the `#restframework` channel on `irc.freenode.net`, or raise a question on [Stack Overflow][stack-overflow], making sure to include the ['django-rest-framework'][django-rest-framework-tag] tag.
|
For support please see the [REST framework discussion group][group], try the `#restframework` channel on `irc.freenode.net`, or raise a question on [Stack Overflow][stack-overflow], making sure to include the ['django-rest-framework'][django-rest-framework-tag] tag.
|
||||||
|
@ -218,6 +222,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
[release-notes]: topics/release-notes.md
|
[release-notes]: topics/release-notes.md
|
||||||
[credits]: topics/credits.md
|
[credits]: topics/credits.md
|
||||||
|
|
||||||
|
[tox]: http://testrun.org/tox/latest/
|
||||||
|
|
||||||
[group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework
|
[group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework
|
||||||
[stack-overflow]: http://stackoverflow.com/
|
[stack-overflow]: http://stackoverflow.com/
|
||||||
[django-rest-framework-tag]: http://stackoverflow.com/questions/tagged/django-rest-framework
|
[django-rest-framework-tag]: http://stackoverflow.com/questions/tagged/django-rest-framework
|
||||||
|
|
|
@ -42,7 +42,8 @@ You can determine your currently installed version using `pip freeze`:
|
||||||
|
|
||||||
### Master
|
### Master
|
||||||
|
|
||||||
* 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.
|
* 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.
|
||||||
* 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.
|
||||||
* Bugfix for authtoken migration while using a custom user model and south.
|
* Bugfix for authtoken migration while using a custom user model and south.
|
||||||
|
|
|
@ -4,3 +4,6 @@ VERSION = __version__ # synonym
|
||||||
|
|
||||||
# Header encoding (see RFC5987)
|
# Header encoding (see RFC5987)
|
||||||
HTTP_HEADER_ENCODING = 'iso-8859-1'
|
HTTP_HEADER_ENCODING = 'iso-8859-1'
|
||||||
|
|
||||||
|
# Default input and output format
|
||||||
|
ISO_8601 = 'iso-8601'
|
||||||
|
|
|
@ -13,12 +13,13 @@ from django import forms
|
||||||
from django.forms import widgets
|
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.compat import parse_date, parse_datetime
|
|
||||||
from rest_framework.compat import timezone
|
from rest_framework import ISO_8601
|
||||||
|
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.compat import parse_time
|
from rest_framework.settings import api_settings
|
||||||
|
|
||||||
|
|
||||||
def is_simple_callable(obj):
|
def is_simple_callable(obj):
|
||||||
|
@ -50,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
|
||||||
|
@ -447,12 +488,16 @@ class DateField(WritableField):
|
||||||
form_field_class = forms.DateField
|
form_field_class = forms.DateField
|
||||||
|
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid': _("'%s' value has an invalid date format. It must be "
|
'invalid': _("Date has wrong format. Use one of these formats instead: %s"),
|
||||||
"in YYYY-MM-DD format."),
|
|
||||||
'invalid_date': _("'%s' value has the correct format (YYYY-MM-DD) "
|
|
||||||
"but it is an invalid date."),
|
|
||||||
}
|
}
|
||||||
empty = None
|
empty = None
|
||||||
|
input_formats = api_settings.DATE_INPUT_FORMATS
|
||||||
|
format = api_settings.DATE_FORMAT
|
||||||
|
|
||||||
|
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.format = format if format is not None else self.format
|
||||||
|
super(DateField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def from_native(self, value):
|
def from_native(self, value):
|
||||||
if value in validators.EMPTY_VALUES:
|
if value in validators.EMPTY_VALUES:
|
||||||
|
@ -468,16 +513,32 @@ class DateField(WritableField):
|
||||||
if isinstance(value, datetime.date):
|
if isinstance(value, datetime.date):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
for format in self.input_formats:
|
||||||
|
if format.lower() == ISO_8601:
|
||||||
try:
|
try:
|
||||||
parsed = parse_date(value)
|
parsed = parse_date(value)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
if parsed is not None:
|
if parsed is not None:
|
||||||
return parsed
|
return parsed
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
parsed = datetime.datetime.strptime(value, format)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
msg = self.error_messages['invalid_date'] % value
|
pass
|
||||||
|
else:
|
||||||
|
return parsed.date()
|
||||||
|
|
||||||
|
msg = self.error_messages['invalid'] % readable_date_formats(self.input_formats)
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
msg = self.error_messages['invalid'] % value
|
def to_native(self, value):
|
||||||
raise ValidationError(msg)
|
if isinstance(value, datetime.datetime):
|
||||||
|
value = value.date()
|
||||||
|
if self.format.lower() == ISO_8601:
|
||||||
|
return value.isoformat()
|
||||||
|
return value.strftime(self.format)
|
||||||
|
|
||||||
|
|
||||||
class DateTimeField(WritableField):
|
class DateTimeField(WritableField):
|
||||||
|
@ -486,15 +547,16 @@ class DateTimeField(WritableField):
|
||||||
form_field_class = forms.DateTimeField
|
form_field_class = forms.DateTimeField
|
||||||
|
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid': _("'%s' value has an invalid format. It must be in "
|
'invalid': _("Datetime has wrong format. Use one of these formats instead: %s"),
|
||||||
"YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."),
|
|
||||||
'invalid_date': _("'%s' value has the correct format "
|
|
||||||
"(YYYY-MM-DD) but it is an invalid date."),
|
|
||||||
'invalid_datetime': _("'%s' value has the correct format "
|
|
||||||
"(YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) "
|
|
||||||
"but it is an invalid date/time."),
|
|
||||||
}
|
}
|
||||||
empty = None
|
empty = None
|
||||||
|
input_formats = api_settings.DATETIME_INPUT_FORMATS
|
||||||
|
format = api_settings.DATETIME_FORMAT
|
||||||
|
|
||||||
|
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.format = format if format is not None else self.format
|
||||||
|
super(DateTimeField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def from_native(self, value):
|
def from_native(self, value):
|
||||||
if value in validators.EMPTY_VALUES:
|
if value in validators.EMPTY_VALUES:
|
||||||
|
@ -516,24 +578,30 @@ class DateTimeField(WritableField):
|
||||||
value = timezone.make_aware(value, default_timezone)
|
value = timezone.make_aware(value, default_timezone)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
for format in self.input_formats:
|
||||||
|
if format.lower() == ISO_8601:
|
||||||
try:
|
try:
|
||||||
parsed = parse_datetime(value)
|
parsed = parse_datetime(value)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
if parsed is not None:
|
if parsed is not None:
|
||||||
return parsed
|
return parsed
|
||||||
except (ValueError, TypeError):
|
else:
|
||||||
msg = self.error_messages['invalid_datetime'] % value
|
|
||||||
raise ValidationError(msg)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
parsed = parse_date(value)
|
parsed = datetime.datetime.strptime(value, format)
|
||||||
if parsed is not None:
|
|
||||||
return datetime.datetime(parsed.year, parsed.month, parsed.day)
|
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
msg = self.error_messages['invalid_date'] % value
|
pass
|
||||||
|
else:
|
||||||
|
return parsed
|
||||||
|
|
||||||
|
msg = self.error_messages['invalid'] % readable_datetime_formats(self.input_formats)
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
msg = self.error_messages['invalid'] % value
|
def to_native(self, value):
|
||||||
raise ValidationError(msg)
|
if self.format.lower() == ISO_8601:
|
||||||
|
return value.isoformat()
|
||||||
|
return value.strftime(self.format)
|
||||||
|
|
||||||
|
|
||||||
class TimeField(WritableField):
|
class TimeField(WritableField):
|
||||||
|
@ -542,10 +610,16 @@ class TimeField(WritableField):
|
||||||
form_field_class = forms.TimeField
|
form_field_class = forms.TimeField
|
||||||
|
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid': _("'%s' value has an invalid format. It must be a valid "
|
'invalid': _("Time has wrong format. Use one of these formats instead: %s"),
|
||||||
"time in the HH:MM[:ss[.uuuuuu]] format."),
|
|
||||||
}
|
}
|
||||||
empty = None
|
empty = None
|
||||||
|
input_formats = api_settings.TIME_INPUT_FORMATS
|
||||||
|
format = api_settings.TIME_FORMAT
|
||||||
|
|
||||||
|
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.format = format if format is not None else self.format
|
||||||
|
super(TimeField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def from_native(self, value):
|
def from_native(self, value):
|
||||||
if value in validators.EMPTY_VALUES:
|
if value in validators.EMPTY_VALUES:
|
||||||
|
@ -554,14 +628,33 @@ class TimeField(WritableField):
|
||||||
if isinstance(value, datetime.time):
|
if isinstance(value, datetime.time):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
for format in self.input_formats:
|
||||||
|
if format.lower() == ISO_8601:
|
||||||
try:
|
try:
|
||||||
parsed = parse_time(value)
|
parsed = parse_time(value)
|
||||||
assert parsed is not None
|
|
||||||
return parsed
|
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
msg = self.error_messages['invalid'] % value
|
pass
|
||||||
|
else:
|
||||||
|
if parsed is not None:
|
||||||
|
return parsed
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
parsed = datetime.datetime.strptime(value, format)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
return parsed.time()
|
||||||
|
|
||||||
|
msg = self.error_messages['invalid'] % readable_time_formats(self.input_formats)
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
|
def to_native(self, value):
|
||||||
|
if isinstance(value, datetime.datetime):
|
||||||
|
value = value.time()
|
||||||
|
if self.format.lower() == ISO_8601:
|
||||||
|
return value.isoformat()
|
||||||
|
return value.strftime(self.format)
|
||||||
|
|
||||||
|
|
||||||
class IntegerField(WritableField):
|
class IntegerField(WritableField):
|
||||||
type_name = 'IntegerField'
|
type_name = 'IntegerField'
|
||||||
|
|
|
@ -18,8 +18,11 @@ REST framework settings, checking for user settings first, then falling
|
||||||
back to the defaults.
|
back to the defaults.
|
||||||
"""
|
"""
|
||||||
from __future__ import unicode_literals
|
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 ISO_8601
|
||||||
from rest_framework.compat import six
|
from rest_framework.compat import six
|
||||||
|
|
||||||
|
|
||||||
|
@ -76,6 +79,22 @@ DEFAULTS = {
|
||||||
'URL_FORMAT_OVERRIDE': 'format',
|
'URL_FORMAT_OVERRIDE': 'format',
|
||||||
|
|
||||||
'FORMAT_SUFFIX_KWARG': 'format',
|
'FORMAT_SUFFIX_KWARG': 'format',
|
||||||
|
|
||||||
|
# Input and output formats
|
||||||
|
'DATE_INPUT_FORMATS': (
|
||||||
|
ISO_8601,
|
||||||
|
),
|
||||||
|
'DATE_FORMAT': ISO_8601,
|
||||||
|
|
||||||
|
'DATETIME_INPUT_FORMATS': (
|
||||||
|
ISO_8601,
|
||||||
|
),
|
||||||
|
'DATETIME_FORMAT': ISO_8601,
|
||||||
|
|
||||||
|
'TIME_INPUT_FORMATS': (
|
||||||
|
ISO_8601,
|
||||||
|
),
|
||||||
|
'TIME_FORMAT': ISO_8601,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,11 @@ General serializer field tests.
|
||||||
"""
|
"""
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.core import validators
|
from django.core import validators
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,37 +61,370 @@ class BasicFieldTests(TestCase):
|
||||||
serializer = CharPrimaryKeyModelSerializer()
|
serializer = CharPrimaryKeyModelSerializer()
|
||||||
self.assertEqual(serializer.fields['id'].read_only, False)
|
self.assertEqual(serializer.fields['id'].read_only, False)
|
||||||
|
|
||||||
def test_TimeField_from_native(self):
|
|
||||||
|
class DateFieldTest(TestCase):
|
||||||
|
"""
|
||||||
|
Tests for the DateFieldTest from_native() and to_native() behavior
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_from_native_string(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() accepts default iso input formats.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField()
|
||||||
|
result_1 = f.from_native('1984-07-31')
|
||||||
|
|
||||||
|
self.assertEqual(datetime.date(1984, 7, 31), result_1)
|
||||||
|
|
||||||
|
def test_from_native_datetime_date(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() accepts a datetime.date instance.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField()
|
||||||
|
result_1 = f.from_native(datetime.date(1984, 7, 31))
|
||||||
|
|
||||||
|
self.assertEqual(result_1, datetime.date(1984, 7, 31))
|
||||||
|
|
||||||
|
def test_from_native_custom_format(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() accepts custom input formats.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField(input_formats=['%Y -- %d'])
|
||||||
|
result = f.from_native('1984 -- 31')
|
||||||
|
|
||||||
|
self.assertEqual(datetime.date(1984, 1, 31), result)
|
||||||
|
|
||||||
|
def test_from_native_invalid_default_on_custom_format(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() don't accept default formats if custom format is preset
|
||||||
|
"""
|
||||||
|
f = serializers.DateField(input_formats=['%Y -- %d'])
|
||||||
|
|
||||||
|
try:
|
||||||
|
f.from_native('1984-07-31')
|
||||||
|
except validators.ValidationError as e:
|
||||||
|
self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY -- DD"])
|
||||||
|
else:
|
||||||
|
self.fail("ValidationError was not properly raised")
|
||||||
|
|
||||||
|
def test_from_native_empty(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() returns None on empty param.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField()
|
||||||
|
result = f.from_native('')
|
||||||
|
|
||||||
|
self.assertEqual(result, None)
|
||||||
|
|
||||||
|
def test_from_native_none(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() returns None on None param.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField()
|
||||||
|
result = f.from_native(None)
|
||||||
|
|
||||||
|
self.assertEqual(result, None)
|
||||||
|
|
||||||
|
def test_from_native_invalid_date(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() raises a ValidationError on passing an invalid date.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField()
|
||||||
|
|
||||||
|
try:
|
||||||
|
f.from_native('1984-13-31')
|
||||||
|
except validators.ValidationError as e:
|
||||||
|
self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"])
|
||||||
|
else:
|
||||||
|
self.fail("ValidationError was not properly raised")
|
||||||
|
|
||||||
|
def test_from_native_invalid_format(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() raises a ValidationError on passing an invalid format.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField()
|
||||||
|
|
||||||
|
try:
|
||||||
|
f.from_native('1984 -- 31')
|
||||||
|
except validators.ValidationError as e:
|
||||||
|
self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"])
|
||||||
|
else:
|
||||||
|
self.fail("ValidationError was not properly raised")
|
||||||
|
|
||||||
|
def test_to_native(self):
|
||||||
|
"""
|
||||||
|
Make sure to_native() returns isoformat as default.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField()
|
||||||
|
|
||||||
|
result_1 = f.to_native(datetime.date(1984, 7, 31))
|
||||||
|
|
||||||
|
self.assertEqual('1984-07-31', result_1)
|
||||||
|
|
||||||
|
def test_to_native_custom_format(self):
|
||||||
|
"""
|
||||||
|
Make sure to_native() returns correct custom format.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField(format="%Y - %m.%d")
|
||||||
|
|
||||||
|
result_1 = f.to_native(datetime.date(1984, 7, 31))
|
||||||
|
|
||||||
|
self.assertEqual('1984 - 07.31', result_1)
|
||||||
|
|
||||||
|
|
||||||
|
class DateTimeFieldTest(TestCase):
|
||||||
|
"""
|
||||||
|
Tests for the DateTimeField from_native() and to_native() behavior
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_from_native_string(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() accepts default iso input formats.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField()
|
||||||
|
result_1 = f.from_native('1984-07-31 04:31')
|
||||||
|
result_2 = f.from_native('1984-07-31 04:31:59')
|
||||||
|
result_3 = f.from_native('1984-07-31 04:31:59.000200')
|
||||||
|
|
||||||
|
self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31), result_1)
|
||||||
|
self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59), result_2)
|
||||||
|
self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59, 200), result_3)
|
||||||
|
|
||||||
|
def test_from_native_datetime_datetime(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() accepts a datetime.datetime instance.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField()
|
||||||
|
result_1 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31))
|
||||||
|
result_2 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
||||||
|
result_3 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
||||||
|
|
||||||
|
self.assertEqual(result_1, datetime.datetime(1984, 7, 31, 4, 31))
|
||||||
|
self.assertEqual(result_2, datetime.datetime(1984, 7, 31, 4, 31, 59))
|
||||||
|
self.assertEqual(result_3, datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
||||||
|
|
||||||
|
def test_from_native_custom_format(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() accepts custom input formats.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField(input_formats=['%Y -- %H:%M'])
|
||||||
|
result = f.from_native('1984 -- 04:59')
|
||||||
|
|
||||||
|
self.assertEqual(datetime.datetime(1984, 1, 1, 4, 59), result)
|
||||||
|
|
||||||
|
def test_from_native_invalid_default_on_custom_format(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() don't accept default formats if custom format is preset
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField(input_formats=['%Y -- %H:%M'])
|
||||||
|
|
||||||
|
try:
|
||||||
|
f.from_native('1984-07-31 04:31:59')
|
||||||
|
except validators.ValidationError as e:
|
||||||
|
self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: YYYY -- hh:mm"])
|
||||||
|
else:
|
||||||
|
self.fail("ValidationError was not properly raised")
|
||||||
|
|
||||||
|
def test_from_native_empty(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() returns None on empty param.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField()
|
||||||
|
result = f.from_native('')
|
||||||
|
|
||||||
|
self.assertEqual(result, None)
|
||||||
|
|
||||||
|
def test_from_native_none(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() returns None on None param.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField()
|
||||||
|
result = f.from_native(None)
|
||||||
|
|
||||||
|
self.assertEqual(result, None)
|
||||||
|
|
||||||
|
def test_from_native_invalid_datetime(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() raises a ValidationError on passing an invalid datetime.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField()
|
||||||
|
|
||||||
|
try:
|
||||||
|
f.from_native('04:61:59')
|
||||||
|
except validators.ValidationError as e:
|
||||||
|
self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: "
|
||||||
|
"YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]"])
|
||||||
|
else:
|
||||||
|
self.fail("ValidationError was not properly raised")
|
||||||
|
|
||||||
|
def test_from_native_invalid_format(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() raises a ValidationError on passing an invalid format.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField()
|
||||||
|
|
||||||
|
try:
|
||||||
|
f.from_native('04 -- 31')
|
||||||
|
except validators.ValidationError as e:
|
||||||
|
self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: "
|
||||||
|
"YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]"])
|
||||||
|
else:
|
||||||
|
self.fail("ValidationError was not properly raised")
|
||||||
|
|
||||||
|
def test_to_native(self):
|
||||||
|
"""
|
||||||
|
Make sure to_native() returns isoformat as default.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField()
|
||||||
|
|
||||||
|
result_1 = f.to_native(datetime.datetime(1984, 7, 31))
|
||||||
|
result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31))
|
||||||
|
result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
||||||
|
result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
||||||
|
|
||||||
|
self.assertEqual('1984-07-31T00:00:00', result_1)
|
||||||
|
self.assertEqual('1984-07-31T04:31:00', result_2)
|
||||||
|
self.assertEqual('1984-07-31T04:31:59', result_3)
|
||||||
|
self.assertEqual('1984-07-31T04:31:59.000200', result_4)
|
||||||
|
|
||||||
|
def test_to_native_custom_format(self):
|
||||||
|
"""
|
||||||
|
Make sure to_native() returns correct custom format.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField(format="%Y - %H:%M")
|
||||||
|
|
||||||
|
result_1 = f.to_native(datetime.datetime(1984, 7, 31))
|
||||||
|
result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31))
|
||||||
|
result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
||||||
|
result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
||||||
|
|
||||||
|
self.assertEqual('1984 - 00:00', result_1)
|
||||||
|
self.assertEqual('1984 - 04:31', result_2)
|
||||||
|
self.assertEqual('1984 - 04:31', result_3)
|
||||||
|
self.assertEqual('1984 - 04:31', result_4)
|
||||||
|
|
||||||
|
|
||||||
|
class TimeFieldTest(TestCase):
|
||||||
|
"""
|
||||||
|
Tests for the TimeField from_native() and to_native() behavior
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_from_native_string(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() accepts default iso input formats.
|
||||||
|
"""
|
||||||
f = serializers.TimeField()
|
f = serializers.TimeField()
|
||||||
result = f.from_native('12:34:56.987654')
|
result_1 = f.from_native('04:31')
|
||||||
|
result_2 = f.from_native('04:31:59')
|
||||||
|
result_3 = f.from_native('04:31:59.000200')
|
||||||
|
|
||||||
self.assertEqual(datetime.time(12, 34, 56, 987654), result)
|
self.assertEqual(datetime.time(4, 31), result_1)
|
||||||
|
self.assertEqual(datetime.time(4, 31, 59), result_2)
|
||||||
|
self.assertEqual(datetime.time(4, 31, 59, 200), result_3)
|
||||||
|
|
||||||
def test_TimeField_from_native_datetime_time(self):
|
def test_from_native_datetime_time(self):
|
||||||
"""
|
"""
|
||||||
Make sure from_native() accepts a datetime.time instance.
|
Make sure from_native() accepts a datetime.time instance.
|
||||||
"""
|
"""
|
||||||
f = serializers.TimeField()
|
f = serializers.TimeField()
|
||||||
result = f.from_native(datetime.time(12, 34, 56))
|
result_1 = f.from_native(datetime.time(4, 31))
|
||||||
self.assertEqual(result, datetime.time(12, 34, 56))
|
result_2 = f.from_native(datetime.time(4, 31, 59))
|
||||||
|
result_3 = f.from_native(datetime.time(4, 31, 59, 200))
|
||||||
|
|
||||||
def test_TimeField_from_native_empty(self):
|
self.assertEqual(result_1, datetime.time(4, 31))
|
||||||
f = serializers.TimeField()
|
self.assertEqual(result_2, datetime.time(4, 31, 59))
|
||||||
result = f.from_native('')
|
self.assertEqual(result_3, datetime.time(4, 31, 59, 200))
|
||||||
self.assertEqual(result, None)
|
|
||||||
|
|
||||||
def test_TimeField_from_native_invalid_time(self):
|
def test_from_native_custom_format(self):
|
||||||
f = serializers.TimeField()
|
"""
|
||||||
|
Make sure from_native() accepts custom input formats.
|
||||||
|
"""
|
||||||
|
f = serializers.TimeField(input_formats=['%H -- %M'])
|
||||||
|
result = f.from_native('04 -- 31')
|
||||||
|
|
||||||
|
self.assertEqual(datetime.time(4, 31), result)
|
||||||
|
|
||||||
|
def test_from_native_invalid_default_on_custom_format(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() don't accept default formats if custom format is preset
|
||||||
|
"""
|
||||||
|
f = serializers.TimeField(input_formats=['%H -- %M'])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f.from_native('12:69:12')
|
f.from_native('04:31:59')
|
||||||
except validators.ValidationError as e:
|
except validators.ValidationError as e:
|
||||||
self.assertEqual(e.messages, ["'12:69:12' value has an invalid "
|
self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: hh -- mm"])
|
||||||
"format. It must be a valid time "
|
|
||||||
"in the HH:MM[:ss[.uuuuuu]] format."])
|
|
||||||
else:
|
else:
|
||||||
self.fail("ValidationError was not properly raised")
|
self.fail("ValidationError was not properly raised")
|
||||||
|
|
||||||
def test_TimeFieldModelSerializer(self):
|
def test_from_native_empty(self):
|
||||||
serializer = TimeFieldModelSerializer()
|
"""
|
||||||
self.assertTrue(isinstance(serializer.fields['clock'], serializers.TimeField))
|
Make sure from_native() returns None on empty param.
|
||||||
|
"""
|
||||||
|
f = serializers.TimeField()
|
||||||
|
result = f.from_native('')
|
||||||
|
|
||||||
|
self.assertEqual(result, None)
|
||||||
|
|
||||||
|
def test_from_native_none(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() returns None on None param.
|
||||||
|
"""
|
||||||
|
f = serializers.TimeField()
|
||||||
|
result = f.from_native(None)
|
||||||
|
|
||||||
|
self.assertEqual(result, None)
|
||||||
|
|
||||||
|
def test_from_native_invalid_time(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() raises a ValidationError on passing an invalid time.
|
||||||
|
"""
|
||||||
|
f = serializers.TimeField()
|
||||||
|
|
||||||
|
try:
|
||||||
|
f.from_native('04:61:59')
|
||||||
|
except validators.ValidationError as e:
|
||||||
|
self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: "
|
||||||
|
"hh:mm[:ss[.uuuuuu]]"])
|
||||||
|
else:
|
||||||
|
self.fail("ValidationError was not properly raised")
|
||||||
|
|
||||||
|
def test_from_native_invalid_format(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() raises a ValidationError on passing an invalid format.
|
||||||
|
"""
|
||||||
|
f = serializers.TimeField()
|
||||||
|
|
||||||
|
try:
|
||||||
|
f.from_native('04 -- 31')
|
||||||
|
except validators.ValidationError as e:
|
||||||
|
self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: "
|
||||||
|
"hh:mm[:ss[.uuuuuu]]"])
|
||||||
|
else:
|
||||||
|
self.fail("ValidationError was not properly raised")
|
||||||
|
|
||||||
|
def test_to_native(self):
|
||||||
|
"""
|
||||||
|
Make sure to_native() returns isoformat as default.
|
||||||
|
"""
|
||||||
|
f = serializers.TimeField()
|
||||||
|
result_1 = f.to_native(datetime.time(4, 31))
|
||||||
|
result_2 = f.to_native(datetime.time(4, 31, 59))
|
||||||
|
result_3 = f.to_native(datetime.time(4, 31, 59, 200))
|
||||||
|
|
||||||
|
self.assertEqual('04:31:00', result_1)
|
||||||
|
self.assertEqual('04:31:59', result_2)
|
||||||
|
self.assertEqual('04:31:59.000200', result_3)
|
||||||
|
|
||||||
|
def test_to_native_custom_format(self):
|
||||||
|
"""
|
||||||
|
Make sure to_native() returns correct custom format.
|
||||||
|
"""
|
||||||
|
f = serializers.TimeField(format="%H - %S [%f]")
|
||||||
|
result_1 = f.to_native(datetime.time(4, 31))
|
||||||
|
result_2 = f.to_native(datetime.time(4, 31, 59))
|
||||||
|
result_3 = f.to_native(datetime.time(4, 31, 59, 200))
|
||||||
|
|
||||||
|
self.assertEqual('04 - 00 [000000]', result_1)
|
||||||
|
self.assertEqual('04 - 59 [000000]', result_2)
|
||||||
|
self.assertEqual('04 - 59 [000200]', result_3)
|
||||||
|
|
|
@ -65,7 +65,7 @@ class IntegrationTestFiltering(TestCase):
|
||||||
|
|
||||||
self.objects = FilterableItem.objects
|
self.objects = FilterableItem.objects
|
||||||
self.data = [
|
self.data = [
|
||||||
{'id': obj.id, 'text': obj.text, 'decimal': obj.decimal, 'date': obj.date}
|
{'id': obj.id, 'text': obj.text, 'decimal': obj.decimal, 'date': obj.date.isoformat()}
|
||||||
for obj in self.objects.all()
|
for obj in self.objects.all()
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ class IntegrationTestFiltering(TestCase):
|
||||||
request = factory.get('/?date=%s' % search_date) # search_date str: '2012-09-22'
|
request = factory.get('/?date=%s' % search_date) # search_date str: '2012-09-22'
|
||||||
response = view(request).render()
|
response = view(request).render()
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
expected_data = [f for f in self.data if f['date'] == search_date]
|
expected_data = [f for f in self.data if datetime.datetime.strptime(f['date'], '%Y-%m-%d').date() == search_date]
|
||||||
self.assertEqual(response.data, expected_data)
|
self.assertEqual(response.data, expected_data)
|
||||||
|
|
||||||
@unittest.skipUnless(django_filters, 'django-filters not installed')
|
@unittest.skipUnless(django_filters, 'django-filters not installed')
|
||||||
|
@ -125,7 +125,7 @@ class IntegrationTestFiltering(TestCase):
|
||||||
request = factory.get('/?date=%s' % search_date) # search_date str: '2012-10-02'
|
request = factory.get('/?date=%s' % search_date) # search_date str: '2012-10-02'
|
||||||
response = view(request).render()
|
response = view(request).render()
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
expected_data = [f for f in self.data if f['date'] > search_date]
|
expected_data = [f for f in self.data if datetime.datetime.strptime(f['date'], '%Y-%m-%d').date() > search_date]
|
||||||
self.assertEqual(response.data, expected_data)
|
self.assertEqual(response.data, expected_data)
|
||||||
|
|
||||||
# Tests that the text filter set with 'icontains' in the filter class works.
|
# Tests that the text filter set with 'icontains' in the filter class works.
|
||||||
|
@ -142,7 +142,8 @@ class IntegrationTestFiltering(TestCase):
|
||||||
request = factory.get('/?decimal=%s&date=%s' % (search_decimal, search_date))
|
request = factory.get('/?decimal=%s&date=%s' % (search_decimal, search_date))
|
||||||
response = view(request).render()
|
response = view(request).render()
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
expected_data = [f for f in self.data if f['date'] > search_date and
|
expected_data = [f for f in self.data if
|
||||||
|
datetime.datetime.strptime(f['date'], '%Y-%m-%d').date() > search_date and
|
||||||
f['decimal'] < search_decimal]
|
f['decimal'] < search_decimal]
|
||||||
self.assertEqual(response.data, expected_data)
|
self.assertEqual(response.data, expected_data)
|
||||||
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ class IntegrationTestPaginationAndFiltering(TestCase):
|
||||||
|
|
||||||
self.objects = FilterableItem.objects
|
self.objects = FilterableItem.objects
|
||||||
self.data = [
|
self.data = [
|
||||||
{'id': obj.id, 'text': obj.text, 'decimal': obj.decimal, 'date': obj.date}
|
{'id': obj.id, 'text': obj.text, 'decimal': obj.decimal, 'date': obj.date.isoformat()}
|
||||||
for obj in self.objects.all()
|
for obj in self.objects.all()
|
||||||
]
|
]
|
||||||
self.view = FilterFieldsRootView.as_view()
|
self.view = FilterFieldsRootView.as_view()
|
||||||
|
|
|
@ -112,7 +112,7 @@ class BasicTests(TestCase):
|
||||||
self.expected = {
|
self.expected = {
|
||||||
'email': 'tom@example.com',
|
'email': 'tom@example.com',
|
||||||
'content': 'Happy new year!',
|
'content': 'Happy new year!',
|
||||||
'created': datetime.datetime(2012, 1, 1),
|
'created': '2012-01-01T00:00:00',
|
||||||
'sub_comment': 'And Merry Christmas!'
|
'sub_comment': 'And Merry Christmas!'
|
||||||
}
|
}
|
||||||
self.person_data = {'name': 'dwight', 'age': 35}
|
self.person_data = {'name': 'dwight', 'age': 35}
|
||||||
|
|
2
tox.ini
2
tox.ini
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user