Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
e5f0a975
Commit
e5f0a975
authored
Sep 22, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More compat fixes
parent
5586b658
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
8 deletions
+33
-8
rest_framework/compat.py
+23
-0
rest_framework/fields.py
+4
-4
tests/test_fields.py
+6
-4
No files found.
rest_framework/compat.py
View file @
e5f0a975
...
...
@@ -139,6 +139,29 @@ else:
self
.
message
=
kwargs
.
pop
(
'message'
,
self
.
message
)
super
(
MaxValueValidator
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
# URLValidator only accept `message` in 1.6+
if
django
.
VERSION
>=
(
1
,
6
):
from
django.core.validators
import
URLValidator
else
:
from
django.core.validators
import
URLValidator
as
DjangoURLValidator
class
URLValidator
(
DjangoURLValidator
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
message
=
kwargs
.
pop
(
'message'
,
self
.
message
)
super
(
URLValidator
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
# EmailValidator requires explicit regex prior to 1.6+
if
django
.
VERSION
>=
(
1
,
6
):
from
django.core.validators
import
EmailValidator
else
:
from
django.core.validators
import
EmailValidator
as
DjangoEmailValidator
from
django.core.validators
import
email_re
class
EmailValidator
(
DjangoEmailValidator
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
EmailValidator
,
self
)
.
__init__
(
email_re
,
*
args
,
**
kwargs
)
# PATCH method is not implemented by Django
if
'patch'
not
in
View
.
http_method_names
:
...
...
rest_framework/fields.py
View file @
e5f0a975
...
...
@@ -6,7 +6,7 @@ from django.utils.dateparse import parse_date, parse_datetime, parse_time
from
django.utils.encoding
import
is_protected_type
from
django.utils.translation
import
ugettext_lazy
as
_
from
rest_framework
import
ISO_8601
from
rest_framework.compat
import
smart_text
,
MinValueValidator
,
MaxValue
Validator
from
rest_framework.compat
import
smart_text
,
EmailValidator
,
MinValueValidator
,
MaxValueValidator
,
URL
Validator
from
rest_framework.settings
import
api_settings
from
rest_framework.utils
import
html
,
representation
,
humanize_datetime
import
datetime
...
...
@@ -335,7 +335,7 @@ class EmailField(CharField):
def
__init__
(
self
,
**
kwargs
):
super
(
EmailField
,
self
)
.
__init__
(
**
kwargs
)
validator
=
validators
.
EmailValidator
(
message
=
self
.
error_messages
[
'invalid'
])
validator
=
EmailValidator
(
message
=
self
.
error_messages
[
'invalid'
])
self
.
validators
.
append
(
validator
)
def
to_internal_value
(
self
,
data
):
...
...
@@ -381,7 +381,7 @@ class URLField(CharField):
def
__init__
(
self
,
**
kwargs
):
super
(
URLField
,
self
)
.
__init__
(
**
kwargs
)
validator
=
validators
.
URLValidator
(
message
=
self
.
error_messages
[
'invalid'
])
validator
=
URLValidator
(
message
=
self
.
error_messages
[
'invalid'
])
self
.
validators
.
append
(
validator
)
...
...
@@ -525,7 +525,7 @@ class DecimalField(Field):
return
None
if
not
isinstance
(
value
,
decimal
.
Decimal
):
value
=
decimal
.
Decimal
(
value
)
value
=
decimal
.
Decimal
(
str
(
value
)
.
strip
()
)
context
=
decimal
.
getcontext
()
.
copy
()
context
.
prec
=
self
.
max_digits
...
...
tests/test_fields.py
View file @
e5f0a975
...
...
@@ -2,6 +2,7 @@ from decimal import Decimal
from
django.utils
import
timezone
from
rest_framework
import
fields
import
datetime
import
django
import
pytest
...
...
@@ -92,7 +93,7 @@ class TestEmailField(FieldValues):
' example@example.com '
:
'example@example.com'
,
}
invalid_inputs
=
{
'example
.
com'
:
[
'Enter a valid email address.'
]
'examplecom'
:
[
'Enter a valid email address.'
]
}
outputs
=
{}
field
=
fields
.
EmailField
()
...
...
@@ -267,8 +268,8 @@ class TestMinMaxDecimalField(FieldValues):
Valid and invalid values for `DecimalField` with min and max limits.
"""
valid_inputs
=
{
'10.0'
:
10.0
,
'20.0'
:
20.0
,
'10.0'
:
Decimal
(
'10.0'
)
,
'20.0'
:
Decimal
(
'20.0'
)
,
}
invalid_inputs
=
{
'9.9'
:
[
'Ensure this value is greater than or equal to 10.'
],
...
...
@@ -368,9 +369,10 @@ class TestDateTimeField(FieldValues):
'2001-01-01 13:00'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
'2001-01-01T13:00'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
'2001-01-01T13:00Z'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
'2001-01-01T14:00+0100'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
):
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()):
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
()),
# Note that 1.4 does not support timezone string parsing.
'2001-01-01T14:00+01:00'
if
(
django
.
VERSION
>
(
1
,
4
))
else
'2001-01-01T13:00Z'
:
datetime
.
datetime
(
2001
,
1
,
1
,
13
,
00
,
tzinfo
=
timezone
.
UTC
())
}
invalid_inputs
=
{
'abc'
:
[
'Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'
],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment