Merge master into develop

This commit is contained in:
Tom Christie 2012-02-21 20:57:36 +00:00
parent af9e4f69d7
commit 5fd4c639d7
14 changed files with 64 additions and 30 deletions

View File

@ -457,3 +457,11 @@ except ImportError: # python < 2.7
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)

View File

@ -3,6 +3,7 @@ from django.core.urlresolvers import get_urlconf, get_resolver, NoReverseMatch
from django.db import models
from djangorestframework.response import ImmediateResponse
from djangorestframework.reverse import reverse
from djangorestframework.serializer import Serializer, _SkipField
from djangorestframework.utils import as_tuple, reverse

View File

@ -0,0 +1,23 @@
"""
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)

View File

@ -2,28 +2,33 @@ from django.conf.urls.defaults import patterns, url
from django.test import TestCase
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.response import Response
class MockView(View):
"""Mock resource which simply returns a URL, so that we can ensure that reversed URLs are fully qualified"""
permissions = ()
class MyView(View):
"""
Mock resource which simply returns a URL, so that we can ensure
that reversed URLs are fully qualified.
"""
renderers = (JSONRenderer, )
def get(self, request):
return Response(reverse('another', request))
urlpatterns = patterns('',
url(r'^$', MockView.as_view()),
url(r'^another$', MockView.as_view(), name='another'),
url(r'^myview$', MyView.as_view(), name='myview'),
)
class ReverseTests(TestCase):
"""Tests for """
"""
Tests for fully qualifed URLs when using `reverse`.
"""
urls = 'djangorestframework.tests.reverse'
def test_reversed_urls_are_fully_qualified(self):
response = self.client.get('/')
self.assertEqual(json.loads(response.content), 'http://testserver/another')
response = self.client.get('/myview')
self.assertEqual(json.loads(response.content), 'http://testserver/myview')

View File

@ -1,12 +1,6 @@
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".
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
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
class MyView(View):
@ -39,9 +33,7 @@ The :py:func:`~utils.reverse` function has the same behavior as :py:func:`django
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
.. [2] https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy
.. _django.core.urlresolvers.reverse: https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse
.. _django.core.urlresolvers.reverse_lazy: https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy

5
docs/library/reverse.rst Normal file
View File

@ -0,0 +1,5 @@
:mod:`reverse`
================
.. automodule:: reverse
:members:

View File

@ -1,5 +1,5 @@
from djangorestframework.resources import ModelResource
from djangorestframework.utils import reverse
from djangorestframework.reverse import reverse
from blogpost.models import BlogPost, Comment

View File

@ -5,7 +5,7 @@ from django.test import TestCase
from django.utils import simplejson as json
from djangorestframework.compat import RequestFactory
from djangorestframework.utils import reverse
from djangorestframework.reverse import reverse
from djangorestframework.views import InstanceModelView, ListOrCreateModelView
from blogpost import models, urls

View File

@ -2,7 +2,7 @@ from djangorestframework.compat import View # Use Django 1.3's django.views.gen
from djangorestframework.mixins import ResponseMixin
from djangorestframework.renderers import DEFAULT_RENDERERS
from djangorestframework.response import Response
from djangorestframework.utils import reverse
from djangorestframework.reverse import reverse
from django.conf.urls.defaults import patterns, url

View File

@ -1,6 +1,6 @@
from django.conf import settings
from djangorestframework.utils import reverse
from djangorestframework.reverse import reverse
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework import status

View File

@ -1,7 +1,7 @@
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework.permissions import PerUserThrottling, IsAuthenticated
from djangorestframework.utils import reverse
from djangorestframework.reverse import reverse
class PermissionsExampleView(View):

View File

@ -4,7 +4,7 @@ from django.conf import settings
from djangorestframework.resources import FormResource
from djangorestframework.response import Response
from djangorestframework.renderers import BaseRenderer
from djangorestframework.utils import reverse
from djangorestframework.reverse import reverse
from djangorestframework.views import View
from djangorestframework import status

View File

@ -1,4 +1,4 @@
from djangorestframework.utils import reverse
from djangorestframework.reverse import reverse
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework import status

View File

@ -1,6 +1,6 @@
"""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.response import Response