aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework
diff options
context:
space:
mode:
Diffstat (limited to 'djangorestframework')
-rw-r--r--djangorestframework/compat.py22
-rw-r--r--djangorestframework/resources.py14
-rw-r--r--djangorestframework/reverse.py19
-rw-r--r--djangorestframework/tests/reverse.py2
4 files changed, 25 insertions, 32 deletions
diff --git a/djangorestframework/compat.py b/djangorestframework/compat.py
index e81b428f..83d26f1f 100644
--- a/djangorestframework/compat.py
+++ b/djangorestframework/compat.py
@@ -214,18 +214,15 @@ else:
REASON_NO_CSRF_COOKIE = "CSRF cookie not set."
REASON_BAD_TOKEN = "CSRF token missing or incorrect."
-
def _get_failure_view():
"""
Returns the view to be used for CSRF rejections
"""
return get_callable(settings.CSRF_FAILURE_VIEW)
-
def _get_new_csrf_key():
return hashlib.md5("%s%s" % (randrange(0, _MAX_CSRF_KEY), settings.SECRET_KEY)).hexdigest()
-
def get_token(request):
"""
Returns the the CSRF token required for a POST form. The token is an
@@ -239,7 +236,6 @@ else:
request.META["CSRF_COOKIE_USED"] = True
return request.META.get("CSRF_COOKIE", None)
-
def _sanitize_token(token):
# Allow only alphanum, and ensure we return a 'str' for the sake of the post
# processing middleware.
@@ -432,12 +428,13 @@ try:
except ImportError:
yaml = None
+
import unittest
try:
import unittest.skip
-except ImportError: # python < 2.7
+except ImportError: # python < 2.7
from unittest import TestCase
- import functools
+ import functools
def skip(reason):
# Pasted from py27/lib/unittest/case.py
@@ -448,26 +445,19 @@ except ImportError: # python < 2.7
if not (isinstance(test_item, type) and issubclass(test_item, TestCase)):
@functools.wraps(test_item)
def skip_wrapper(*args, **kwargs):
- pass
+ pass
test_item = skip_wrapper
test_item.__unittest_skip__ = True
test_item.__unittest_skip_why__ = reason
return test_item
return decorator
-
+
unittest.skip = skip
-# reverse_lazy (Django 1.4 onwards)
-try:
- from django.core.urlresolvers import reverse_lazy
-except:
- from django.core.urlresolvers import reverse
- from django.utils.functional import lazy
- reverse_lazy = lazy(reverse, str)
# xml.etree.parse only throws ParseError for python >= 2.7
try:
from xml.etree import ParseError as ETParseError
-except ImportError: # python < 2.7
+except ImportError: # python < 2.7
ETParseError = None
diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py
index 2b84ee09..71c4d2b9 100644
--- a/djangorestframework/resources.py
+++ b/djangorestframework/resources.py
@@ -10,7 +10,8 @@ from djangorestframework.utils import as_tuple
class BaseResource(Serializer):
"""
- Base class for all Resource classes, which simply defines the interface they provide.
+ Base class for all Resource classes, which simply defines the interface
+ they provide.
"""
fields = None
include = None
@@ -19,11 +20,13 @@ class BaseResource(Serializer):
def __init__(self, view=None, depth=None, stack=[], **kwargs):
super(BaseResource, self).__init__(depth, stack, **kwargs)
self.view = view
+ self.request = view.request
def validate_request(self, data, files=None):
"""
Given the request content return the cleaned, validated content.
- Typically raises a :exc:`response.ErrorResponse` with status code 400 (Bad Request) on failure.
+ Typically raises a :exc:`response.ErrorResponse` with status code 400
+ (Bad Request) on failure.
"""
return data
@@ -37,7 +40,8 @@ class BaseResource(Serializer):
class Resource(BaseResource):
"""
A Resource determines how a python object maps to some serializable data.
- Objects that a resource can act on include plain Python object instances, Django Models, and Django QuerySets.
+ Objects that a resource can act on include plain Python object instances,
+ Django Models, and Django QuerySets.
"""
# The model attribute refers to the Django Model which this Resource maps to.
@@ -355,7 +359,9 @@ class ModelResource(FormResource):
instance_attrs[param] = attr
try:
- return reverse(self.view_callable[0], self.view.request, kwargs=instance_attrs)
+ return reverse(self.view_callable[0],
+ kwargs=instance_attrs,
+ request=self.view.request)
except NoReverseMatch:
pass
raise _SkipField
diff --git a/djangorestframework/reverse.py b/djangorestframework/reverse.py
index ad06f966..05643157 100644
--- a/djangorestframework/reverse.py
+++ b/djangorestframework/reverse.py
@@ -2,22 +2,19 @@
Provide reverse functions that return fully qualified URLs
"""
from django.core.urlresolvers import reverse as django_reverse
-from djangorestframework.compat import reverse_lazy as django_reverse_lazy
+from django.utils.functional import lazy
def reverse(viewname, request, *args, **kwargs):
"""
- Do the same as `django.core.urlresolvers.reverse` but using
- *request* to build a fully qualified URL.
+ Same as `django.core.urlresolvers.reverse`, but optionally takes a request
+ and returns a fully qualified URL, using the request to get the base URL.
"""
+ request = kwargs.pop('request', None)
url = django_reverse(viewname, *args, **kwargs)
- return request.build_absolute_uri(url)
+ if request:
+ return request.build_absolute_uri(url)
+ return url
-def reverse_lazy(viewname, request, *args, **kwargs):
- """
- Do the same as `django.core.urlresolvers.reverse_lazy` but using
- *request* to build a fully qualified URL.
- """
- url = django_reverse_lazy(viewname, *args, **kwargs)
- return request.build_absolute_uri(url)
+reverse_lazy = lazy(reverse, str)
diff --git a/djangorestframework/tests/reverse.py b/djangorestframework/tests/reverse.py
index 9c0aee79..3dd13ef8 100644
--- a/djangorestframework/tests/reverse.py
+++ b/djangorestframework/tests/reverse.py
@@ -15,7 +15,7 @@ class MyView(View):
renderers = (JSONRenderer, )
def get(self, request):
- return reverse('myview', request)
+ return reverse('myview', request=request)
urlpatterns = patterns('',
url(r'^myview$', MyView.as_view(), name='myview'),