Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
86d2774c
Commit
86d2774c
authored
Jan 16, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix compat issues
parent
8b0f25aa
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
37 deletions
+34
-37
rest_framework/pagination.py
+4
-4
rest_framework/templatetags/rest_framework.py
+3
-31
rest_framework/utils/urls.py
+25
-0
tests/test_pagination.py
+2
-2
No files found.
rest_framework/pagination.py
View file @
86d2774c
...
@@ -12,7 +12,7 @@ from rest_framework.compat import OrderedDict
...
@@ -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):
...
@@ -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):
...
@@ -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.
...
...
rest_framework/templatetags/rest_framework.py
View file @
86d2774c
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):
...
@@ -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
{}
...
...
rest_framework/utils/urls.py
0 → 100644
View file @
86d2774c
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
))
tests/test_pagination.py
View file @
86d2774c
...
@@ -117,7 +117,7 @@ class TestPaginationDisabledIntegration:
...
@@ -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:
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment