Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes bug 948704 - date changing links should keep query string intac…
…t, r=adrian
- Loading branch information
Showing
7 changed files
with
120 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import cgi | ||
import urllib | ||
|
||
import jinja2 | ||
from jingo import register | ||
|
||
|
||
@register.function | ||
@jinja2.contextfunction | ||
def change_query_string(context, **kwargs): | ||
""" | ||
Template function for modifying the current URL by parameters. | ||
You use it like this in a template: | ||
<a href={{ change_query_string(foo='bar') }}> | ||
And it takes the current request's URL (and query string) and modifies it | ||
just by the parameters you pass in. So if the current URL is | ||
`/page/?day=1` the output of this will become: | ||
<a href=/page?day=1&foo=bar> | ||
You can also pass lists like this: | ||
<a href={{ change_query_string(thing=['bar','foo']) }}> | ||
And you get this output: | ||
<a href=/page?day=1&thing=bar&thing=foo> | ||
And if you want to remove a parameter you can explicitely pass it `None`. | ||
Like this for example: | ||
<a href={{ change_query_string(day=None) }}> | ||
And you get this output: | ||
<a href=/page> | ||
""" | ||
base = context['request'].META['PATH_INFO'] | ||
qs = cgi.parse_qs(context['request'].META['QUERY_STRING']) | ||
for key, value in kwargs.items(): | ||
if value is None: | ||
# delete the parameter | ||
if key in qs: | ||
del qs[key] | ||
else: | ||
# change it | ||
qs[key] = value | ||
new_qs = urllib.urlencode(qs, True) | ||
if new_qs: | ||
return '%s?%s' % (base, new_qs) | ||
return base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from nose.tools import eq_ | ||
|
||
from django.test import TestCase | ||
from django.test.client import RequestFactory | ||
|
||
from crashstats.base.helpers import ( | ||
change_query_string | ||
) | ||
|
||
|
||
class TestChangeURL(TestCase): | ||
|
||
def test_root_url_no_query_string(self): | ||
context = {} | ||
context['request'] = RequestFactory().get('/') | ||
result = change_query_string(context) | ||
eq_(result, '/') | ||
|
||
def test_with_path_no_query_string(self): | ||
context = {} | ||
context['request'] = RequestFactory().get('/page/') | ||
result = change_query_string(context) | ||
eq_(result, '/page/') | ||
|
||
def test_with_query_string(self): | ||
context = {} | ||
context['request'] = RequestFactory().get('/page/?foo=bar&bar=baz') | ||
result = change_query_string(context) | ||
eq_(result, '/page/?foo=bar&bar=baz') | ||
|
||
def test_add_query_string(self): | ||
context = {} | ||
context['request'] = RequestFactory().get('/page/') | ||
result = change_query_string(context, foo='bar') | ||
eq_(result, '/page/?foo=bar') | ||
|
||
def test_change_query_string(self): | ||
context = {} | ||
context['request'] = RequestFactory().get('/page/?foo=bar') | ||
result = change_query_string(context, foo='else') | ||
eq_(result, '/page/?foo=else') | ||
|
||
def test_remove_query_string(self): | ||
context = {} | ||
context['request'] = RequestFactory().get('/page/?foo=bar') | ||
result = change_query_string(context, foo=None) | ||
eq_(result, '/page/') | ||
|
||
def test_remove_leave_some(self): | ||
context = {} | ||
context['request'] = RequestFactory().get('/page/?foo=bar&other=thing') | ||
result = change_query_string(context, foo=None) | ||
eq_(result, '/page/?other=thing') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters