Commit 5fd4c639 by Tom Christie

Merge master into develop

parent af9e4f69
...@@ -457,3 +457,11 @@ except ImportError: # python < 2.7 ...@@ -457,3 +457,11 @@ except ImportError: # python < 2.7
return decorator return decorator
unittest.skip = skip 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)
...@@ -3,6 +3,7 @@ from django.core.urlresolvers import get_urlconf, get_resolver, NoReverseMatch ...@@ -3,6 +3,7 @@ from django.core.urlresolvers import get_urlconf, get_resolver, NoReverseMatch
from django.db import models from django.db import models
from djangorestframework.response import ImmediateResponse from djangorestframework.response import ImmediateResponse
from djangorestframework.reverse import reverse
from djangorestframework.serializer import Serializer, _SkipField from djangorestframework.serializer import Serializer, _SkipField
from djangorestframework.utils import as_tuple, reverse from djangorestframework.utils import as_tuple, reverse
......
"""
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
def reverse(viewname, request, *args, **kwargs):
"""
Do the same as `django.core.urlresolvers.reverse` but using
*request* to build a fully qualified URL.
"""
url = django_reverse(viewname, *args, **kwargs)
return request.build_absolute_uri(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)
...@@ -2,28 +2,33 @@ from django.conf.urls.defaults import patterns, url ...@@ -2,28 +2,33 @@ from django.conf.urls.defaults import patterns, url
from django.test import TestCase from django.test import TestCase
from django.utils import simplejson as json from django.utils import simplejson as json
from djangorestframework.utils import reverse from djangorestframework.renderers import JSONRenderer
from djangorestframework.reverse import reverse
from djangorestframework.views import View from djangorestframework.views import View
from djangorestframework.response import Response from djangorestframework.response import Response
class MockView(View): class MyView(View):
"""Mock resource which simply returns a URL, so that we can ensure that reversed URLs are fully qualified""" """
permissions = () Mock resource which simply returns a URL, so that we can ensure
that reversed URLs are fully qualified.
"""
renderers = (JSONRenderer, )
def get(self, request): def get(self, request):
return Response(reverse('another', request)) return Response(reverse('another', request))
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^$', MockView.as_view()), url(r'^myview$', MyView.as_view(), name='myview'),
url(r'^another$', MockView.as_view(), name='another'),
) )
class ReverseTests(TestCase): class ReverseTests(TestCase):
"""Tests for """ """
Tests for fully qualifed URLs when using `reverse`.
"""
urls = 'djangorestframework.tests.reverse' urls = 'djangorestframework.tests.reverse'
def test_reversed_urls_are_fully_qualified(self): def test_reversed_urls_are_fully_qualified(self):
response = self.client.get('/') response = self.client.get('/myview')
self.assertEqual(json.loads(response.content), 'http://testserver/another') self.assertEqual(json.loads(response.content), 'http://testserver/myview')
Returning URIs from your Web APIs Returning URIs from your Web APIs
================================= =================================
"The central feature that distinguishes the REST architectural style from
other network-based styles is its emphasis on a uniform interface between
components."
-- Roy Fielding, Architectural Styles and the Design of Network-based Software Architectures
As a rule, it's probably better practice to return absolute URIs from you web APIs, e.g. "http://example.com/foobar", rather than returning relative URIs, e.g. "/foobar". As a rule, it's probably better practice to return absolute URIs from you web APIs, e.g. "http://example.com/foobar", rather than returning relative URIs, e.g. "/foobar".
The advantages of doing so are: The advantages of doing so are:
...@@ -23,9 +17,9 @@ There's no requirement for you to use them, but if you do then the self-describi ...@@ -23,9 +17,9 @@ There's no requirement for you to use them, but if you do then the self-describi
reverse(viewname, request, ...) reverse(viewname, request, ...)
------------------------------- -------------------------------
The :py:func:`~utils.reverse` function has the same behavior as :py:func:`django.core.urlresolvers.reverse` [1]_, except that it takes a request object and returns a fully qualified URL, using the request to determine the host and port:: The :py:func:`~reverse.reverse` function has the same behavior as `django.core.urlresolvers.reverse`_, except that it takes a request object and returns a fully qualified URL, using the request to determine the host and port::
from djangorestframework.utils import reverse from djangorestframework.reverse import reverse
from djangorestframework.views import View from djangorestframework.views import View
class MyView(View): class MyView(View):
...@@ -39,9 +33,7 @@ The :py:func:`~utils.reverse` function has the same behavior as :py:func:`django ...@@ -39,9 +33,7 @@ The :py:func:`~utils.reverse` function has the same behavior as :py:func:`django
reverse_lazy(viewname, request, ...) reverse_lazy(viewname, request, ...)
------------------------------------ ------------------------------------
The :py:func:`~utils.reverse_lazy` function has the same behavior as :py:func:`django.core.urlresolvers.reverse_lazy` [2]_, except that it takes a request object and returns a fully qualified URL, using the request to determine the host and port. The :py:func:`~reverse.reverse_lazy` function has the same behavior as `django.core.urlresolvers.reverse_lazy`_, except that it takes a request object and returns a fully qualified URL, using the request to determine the host and port.
.. rubric:: Footnotes
.. [1] https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse .. _django.core.urlresolvers.reverse: https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse
.. [2] https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy .. _django.core.urlresolvers.reverse_lazy: https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy
:mod:`reverse`
================
.. automodule:: reverse
:members:
from djangorestframework.resources import ModelResource from djangorestframework.resources import ModelResource
from djangorestframework.utils import reverse from djangorestframework.reverse import reverse
from blogpost.models import BlogPost, Comment from blogpost.models import BlogPost, Comment
......
...@@ -5,7 +5,7 @@ from django.test import TestCase ...@@ -5,7 +5,7 @@ from django.test import TestCase
from django.utils import simplejson as json from django.utils import simplejson as json
from djangorestframework.compat import RequestFactory from djangorestframework.compat import RequestFactory
from djangorestframework.utils import reverse from djangorestframework.reverse import reverse
from djangorestframework.views import InstanceModelView, ListOrCreateModelView from djangorestframework.views import InstanceModelView, ListOrCreateModelView
from blogpost import models, urls from blogpost import models, urls
......
...@@ -2,7 +2,7 @@ from djangorestframework.compat import View # Use Django 1.3's django.views.gen ...@@ -2,7 +2,7 @@ from djangorestframework.compat import View # Use Django 1.3's django.views.gen
from djangorestframework.mixins import ResponseMixin from djangorestframework.mixins import ResponseMixin
from djangorestframework.renderers import DEFAULT_RENDERERS from djangorestframework.renderers import DEFAULT_RENDERERS
from djangorestframework.response import Response from djangorestframework.response import Response
from djangorestframework.utils import reverse from djangorestframework.reverse import reverse
from django.conf.urls.defaults import patterns, url from django.conf.urls.defaults import patterns, url
......
from django.conf import settings from django.conf import settings
from djangorestframework.utils import reverse from djangorestframework.reverse import reverse
from djangorestframework.views import View from djangorestframework.views import View
from djangorestframework.response import Response from djangorestframework.response import Response
from djangorestframework import status from djangorestframework import status
......
from djangorestframework.views import View from djangorestframework.views import View
from djangorestframework.response import Response from djangorestframework.response import Response
from djangorestframework.permissions import PerUserThrottling, IsAuthenticated from djangorestframework.permissions import PerUserThrottling, IsAuthenticated
from djangorestframework.utils import reverse from djangorestframework.reverse import reverse
class PermissionsExampleView(View): class PermissionsExampleView(View):
......
...@@ -4,7 +4,7 @@ from django.conf import settings ...@@ -4,7 +4,7 @@ from django.conf import settings
from djangorestframework.resources import FormResource from djangorestframework.resources import FormResource
from djangorestframework.response import Response from djangorestframework.response import Response
from djangorestframework.renderers import BaseRenderer from djangorestframework.renderers import BaseRenderer
from djangorestframework.utils import reverse from djangorestframework.reverse import reverse
from djangorestframework.views import View from djangorestframework.views import View
from djangorestframework import status from djangorestframework import status
......
from djangorestframework.utils import reverse from djangorestframework.reverse import reverse
from djangorestframework.views import View from djangorestframework.views import View
from djangorestframework.response import Response from djangorestframework.response import Response
from djangorestframework import status from djangorestframework import status
......
"""The root view for the examples provided with Django REST framework""" """The root view for the examples provided with Django REST framework"""
from djangorestframework.utils import reverse from djangorestframework.reverse import reverse
from djangorestframework.views import View from djangorestframework.views import View
from djangorestframework.response import Response from djangorestframework.response import Response
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment