Fix compat issues

This commit is contained in:
Tom Christie 2015-01-16 20:30:46 +00:00
parent 8b0f25aa0a
commit 86d2774cf3
4 changed files with 34 additions and 37 deletions

View File

@ -12,7 +12,7 @@ from rest_framework.compat import OrderedDict
from rest_framework.exceptions import NotFound from rest_framework.exceptions import NotFound
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework.templatetags.rest_framework import ( from rest_framework.utils.urls import (
replace_query_param, remove_query_param replace_query_param, remove_query_param
) )
@ -34,8 +34,8 @@ def _divide_with_ceil(a, b):
Returns 'a' divded by 'b', with any remainder rounded up. Returns 'a' divded by 'b', with any remainder rounded up.
""" """
if a % b: if a % b:
return (a / b) + 1 return (a // b) + 1
return a / b return a // b
def _get_count(queryset): def _get_count(queryset):
@ -70,7 +70,7 @@ def _get_displayed_page_numbers(current, final):
assert final >= current assert final >= current
if final <= 5: if final <= 5:
return range(1, final + 1) return list(range(1, final + 1))
# We always include the first two pages, last two pages, and # We always include the first two pages, last two pages, and
# two pages either side of the current page. # two pages either side of the current page.

View File

@ -1,41 +1,19 @@
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
from django import template from django import template
from django.core.urlresolvers import reverse, NoReverseMatch from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import QueryDict
from django.utils import six from django.utils import six
from django.utils.six.moves.urllib import parse as urlparse
from django.utils.encoding import iri_to_uri, force_text from django.utils.encoding import iri_to_uri, force_text
from django.utils.html import escape from django.utils.html import escape
from django.utils.safestring import SafeData, mark_safe from django.utils.safestring import SafeData, mark_safe
from django.utils.html import smart_urlquote from django.utils.html import smart_urlquote
from rest_framework.renderers import HTMLFormRenderer from rest_framework.renderers import HTMLFormRenderer
from rest_framework.utils.urls import replace_query_param
import re import re
register = template.Library() register = template.Library()
# Regex for adding classes to html snippets
def replace_query_param(url, key, val): class_re = re.compile(r'(?<=class=["\'])(.*)(?=["\'])')
"""
Given a URL and a key/val pair, set or replace an item in the query
parameters of the URL, and return the new URL.
"""
(scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
query_dict = QueryDict(query).copy()
query_dict[key] = val
query = query_dict.urlencode()
return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
def remove_query_param(url, key):
"""
Given a URL and a key/val pair, set or replace an item in the query
parameters of the URL, and return the new URL.
"""
(scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
query_dict = QueryDict(query).copy()
query_dict.pop(key, None)
query = query_dict.urlencode()
return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
@register.simple_tag @register.simple_tag
@ -43,12 +21,6 @@ def get_pagination_html(pager):
return pager.to_html() return pager.to_html()
# Regex for adding classes to html snippets
class_re = re.compile(r'(?<=class=["\'])(.*)(?=["\'])')
# And the template tags themselves...
@register.simple_tag @register.simple_tag
def render_field(field, style=None): def render_field(field, style=None):
style = style or {} style = style or {}

View File

@ -0,0 +1,25 @@
from django.utils.six.moves.urllib import parse as urlparse
def replace_query_param(url, key, val):
"""
Given a URL and a key/val pair, set or replace an item in the query
parameters of the URL, and return the new URL.
"""
(scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
query_dict = urlparse.parse_qs(query)
query_dict[key] = [val]
query = urlparse.urlencode(sorted(list(query_dict.items())), doseq=True)
return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
def remove_query_param(url, key):
"""
Given a URL and a key/val pair, remove an item in the query
parameters of the URL, and return the new URL.
"""
(scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
query_dict = urlparse.parse_qs(query)
query_dict.pop(key, None)
query = urlparse.urlencode(sorted(list(query_dict.items())), doseq=True)
return urlparse.urlunsplit((scheme, netloc, path, query, fragment))

View File

@ -117,7 +117,7 @@ class TestPaginationDisabledIntegration:
request = factory.get('/', {'page': 2}) request = factory.get('/', {'page': 2})
response = self.view(request) response = self.view(request)
assert response.status_code == status.HTTP_200_OK assert response.status_code == status.HTTP_200_OK
assert response.data == range(1, 101) assert response.data == list(range(1, 101))
class TestDeprecatedStylePagination: class TestDeprecatedStylePagination:
@ -268,7 +268,7 @@ class TestLimitOffset:
self.queryset = range(1, 101) self.queryset = range(1, 101)
def paginate_queryset(self, request): def paginate_queryset(self, request):
return self.pagination.paginate_queryset(self.queryset, request) return list(self.pagination.paginate_queryset(self.queryset, request))
def get_paginated_content(self, queryset): def get_paginated_content(self, queryset):
response = self.pagination.get_paginated_response(queryset) response = self.pagination.get_paginated_response(queryset)