Browse Source

Fixed #28720 -- Added HttpRequest.get_full_path_info().

Jonas Haag 7 years ago
parent
commit
a2851f204c
4 changed files with 24 additions and 5 deletions
  1. 8 2
      django/http/request.py
  2. 9 0
      docs/ref/request-response.txt
  3. 1 1
      docs/releases/2.1.txt
  4. 6 2
      tests/requests/tests.py

+ 8 - 2
django/http/request.py

@@ -113,11 +113,17 @@ class HttpRequest:
         return str(port)
 
     def get_full_path(self, force_append_slash=False):
+        return self._get_full_path(self.path, force_append_slash)
+
+    def get_full_path_info(self, force_append_slash=False):
+        return self._get_full_path(self.path_info, force_append_slash)
+
+    def _get_full_path(self, path, force_append_slash):
         # RFC 3986 requires query string arguments to be in the ASCII range.
         # Rather than crash if this doesn't happen, we encode defensively.
         return '%s%s%s' % (
-            escape_uri_path(self.path),
-            '/' if force_append_slash and not self.path.endswith('/') else '',
+            escape_uri_path(path),
+            '/' if force_append_slash and not path.endswith('/') else '',
             ('?' + iri_to_uri(self.META.get('QUERY_STRING', ''))) if self.META.get('QUERY_STRING', '') else ''
         )
 

+ 9 - 0
docs/ref/request-response.txt

@@ -284,6 +284,15 @@ Methods
 
     Example: ``"/music/bands/the_beatles/?print=true"``
 
+.. method:: HttpRequest.get_full_path_info()
+
+    .. versionadded:: 2.1
+
+    Like :meth:`get_full_path`, but uses :attr:`path_info` instead of
+    :attr:`path`.
+
+    Example: ``"/minfo/music/bands/the_beatles/?print=true"``
+
 .. method:: HttpRequest.build_absolute_uri(location)
 
     Returns the absolute URI form of ``location``. If no location is provided,

+ 1 - 1
docs/releases/2.1.txt

@@ -159,7 +159,7 @@ Models
 Requests and Responses
 ~~~~~~~~~~~~~~~~~~~~~~
 
-* ...
+* Added :meth:`.HttpRequest.get_full_path_info`.
 
 Serialization
 ~~~~~~~~~~~~~

+ 6 - 2
tests/requests/tests.py

@@ -38,16 +38,20 @@ class RequestsTests(SimpleTestCase):
 
     def test_httprequest_full_path(self):
         request = HttpRequest()
-        request.path = request.path_info = '/;some/?awful/=path/foo:bar/'
+        request.path = '/;some/?awful/=path/foo:bar/'
+        request.path_info = '/prefix' + request.path
         request.META['QUERY_STRING'] = ';some=query&+query=string'
         expected = '/%3Bsome/%3Fawful/%3Dpath/foo:bar/?;some=query&+query=string'
         self.assertEqual(request.get_full_path(), expected)
+        self.assertEqual(request.get_full_path_info(), '/prefix' + expected)
 
     def test_httprequest_full_path_with_query_string_and_fragment(self):
         request = HttpRequest()
-        request.path = request.path_info = '/foo#bar'
+        request.path = '/foo#bar'
+        request.path_info = '/prefix' + request.path
         request.META['QUERY_STRING'] = 'baz#quux'
         self.assertEqual(request.get_full_path(), '/foo%23bar?baz#quux')
+        self.assertEqual(request.get_full_path_info(), '/prefix/foo%23bar?baz#quux')
 
     def test_httprequest_repr(self):
         request = HttpRequest()