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
7d9125bc
Commit
7d9125bc
authored
Sep 07, 2012
by
Mjumbe Wawatu Poe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix Django 1.3 compatibility
parent
72bdd0fc
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
6 deletions
+62
-6
djangorestframework/compat.py
+53
-0
djangorestframework/fields.py
+3
-3
djangorestframework/settings.py
+4
-1
djangorestframework/utils/encoders.py
+2
-2
No files found.
djangorestframework/compat.py
View file @
7d9125bc
...
@@ -366,6 +366,59 @@ else:
...
@@ -366,6 +366,59 @@ else:
return
self
.
_accept
(
request
)
return
self
.
_accept
(
request
)
# timezone support is new in Django 1.4
try
:
from
django.utils
import
timezone
except
ImportError
:
timezone
=
None
# dateparse is ALSO new in Django 1.4
try
:
from
django.utils.dateparse
import
parse_date
,
parse_datetime
except
ImportError
:
import
datetime
import
re
date_re
=
re
.
compile
(
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$'
)
datetime_re
=
re
.
compile
(
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})'
r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
r'(?P<tzinfo>Z|[+-]\d{1,2}:\d{1,2})?$'
)
time_re
=
re
.
compile
(
r'(?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
)
def
parse_date
(
value
):
match
=
date_re
.
match
(
value
)
if
match
:
kw
=
dict
((
k
,
int
(
v
))
for
k
,
v
in
match
.
groupdict
()
.
iteritems
())
return
datetime
.
date
(
**
kw
)
def
parse_time
(
value
):
match
=
time_re
.
match
(
value
)
if
match
:
kw
=
match
.
groupdict
()
if
kw
[
'microsecond'
]:
kw
[
'microsecond'
]
=
kw
[
'microsecond'
]
.
ljust
(
6
,
'0'
)
kw
=
dict
((
k
,
int
(
v
))
for
k
,
v
in
kw
.
iteritems
()
if
v
is
not
None
)
return
datetime
.
time
(
**
kw
)
def
parse_datetime
(
value
):
"""Parse datetime, but w/o the timezone awareness in 1.4"""
match
=
datetime_re
.
match
(
value
)
if
match
:
kw
=
match
.
groupdict
()
if
kw
[
'microsecond'
]:
kw
[
'microsecond'
]
=
kw
[
'microsecond'
]
.
ljust
(
6
,
'0'
)
kw
=
dict
((
k
,
int
(
v
))
for
k
,
v
in
kw
.
iteritems
()
if
v
is
not
None
)
return
datetime
.
datetime
(
**
kw
)
# Markdown is optional
# Markdown is optional
try
:
try
:
...
...
djangorestframework/fields.py
View file @
7d9125bc
...
@@ -8,10 +8,10 @@ from django.core.exceptions import ValidationError
...
@@ -8,10 +8,10 @@ from django.core.exceptions import ValidationError
from
django.conf
import
settings
from
django.conf
import
settings
from
django.db
import
DEFAULT_DB_ALIAS
from
django.db
import
DEFAULT_DB_ALIAS
from
django.db.models.related
import
RelatedObject
from
django.db.models.related
import
RelatedObject
from
django.utils
import
timezone
from
django.utils.dateparse
import
parse_date
,
parse_datetime
from
django.utils.encoding
import
is_protected_type
,
smart_unicode
from
django.utils.encoding
import
is_protected_type
,
smart_unicode
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.translation
import
ugettext_lazy
as
_
from
djangorestframework.compat
import
parse_date
,
parse_datetime
from
djangorestframework.compat
import
timezone
def
is_simple_callable
(
obj
):
def
is_simple_callable
(
obj
):
...
@@ -317,7 +317,7 @@ class DateField(Field):
...
@@ -317,7 +317,7 @@ class DateField(Field):
if
value
is
None
:
if
value
is
None
:
return
value
return
value
if
isinstance
(
value
,
datetime
.
datetime
):
if
isinstance
(
value
,
datetime
.
datetime
):
if
settings
.
USE_TZ
and
timezone
.
is_aware
(
value
):
if
timezone
and
settings
.
USE_TZ
and
timezone
.
is_aware
(
value
):
# Convert aware datetimes to the default time zone
# Convert aware datetimes to the default time zone
# before casting them to dates (#17742).
# before casting them to dates (#17742).
default_timezone
=
timezone
.
get_default_timezone
()
default_timezone
=
timezone
.
get_default_timezone
()
...
...
djangorestframework/settings.py
View file @
7d9125bc
...
@@ -88,7 +88,10 @@ def import_from_string(val, setting):
...
@@ -88,7 +88,10 @@ def import_from_string(val, setting):
module_path
,
class_name
=
'.'
.
join
(
parts
[:
-
1
]),
parts
[
-
1
]
module_path
,
class_name
=
'.'
.
join
(
parts
[:
-
1
]),
parts
[
-
1
]
module
=
importlib
.
import_module
(
module_path
)
module
=
importlib
.
import_module
(
module_path
)
return
getattr
(
module
,
class_name
)
return
getattr
(
module
,
class_name
)
except
:
except
Exception
,
e
:
import
traceback
tb
=
traceback
.
format_exc
()
import
pdb
;
pdb
.
set_trace
()
msg
=
"Could not import '
%
s' for API setting '
%
s'"
%
(
val
,
setting
)
msg
=
"Could not import '
%
s' for API setting '
%
s'"
%
(
val
,
setting
)
raise
ImportError
(
msg
)
raise
ImportError
(
msg
)
...
...
djangorestframework/utils/encoders.py
View file @
7d9125bc
...
@@ -3,8 +3,8 @@ Helper classes for parsers.
...
@@ -3,8 +3,8 @@ Helper classes for parsers.
"""
"""
import
datetime
import
datetime
import
decimal
import
decimal
from
django.utils
import
timezone
from
django.utils
import
simplejson
as
json
from
django.utils
import
simplejson
as
json
from
djangorestframework.compat
import
timezone
class
JSONEncoder
(
json
.
JSONEncoder
):
class
JSONEncoder
(
json
.
JSONEncoder
):
...
@@ -25,7 +25,7 @@ class JSONEncoder(json.JSONEncoder):
...
@@ -25,7 +25,7 @@ class JSONEncoder(json.JSONEncoder):
elif
isinstance
(
o
,
datetime
.
date
):
elif
isinstance
(
o
,
datetime
.
date
):
return
o
.
isoformat
()
return
o
.
isoformat
()
elif
isinstance
(
o
,
datetime
.
time
):
elif
isinstance
(
o
,
datetime
.
time
):
if
timezone
.
is_aware
(
o
):
if
timezone
and
timezone
.
is_aware
(
o
):
raise
ValueError
(
"JSON can't represent timezone-aware times."
)
raise
ValueError
(
"JSON can't represent timezone-aware times."
)
r
=
o
.
isoformat
()
r
=
o
.
isoformat
()
if
o
.
microsecond
:
if
o
.
microsecond
:
...
...
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