Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INTERNAL] Helper to rewrite query string for paginated lists #244

Merged
merged 1 commit into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions promgen/templates/promgen/pagination.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{% load promgen %}
<nav aria-label="Page navigation">
{% if page_obj.has_other_pages %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
<li><a href="?{% qs_replace 'page' page_obj.previous_page_number %}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}
{% for i in page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
<li><a href="?{% qs_replace 'page' i %}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
<li><a href="?{% qs_replace 'page' page_obj.next_page_number %}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions promgen/templates/promgen/pagination_short.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% if page_obj.has_other_pages %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
<li><a href="?{% qs_replace 'page' page_obj.previous_page_number %}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}
Expand All @@ -13,7 +13,7 @@
</li>

{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
<li><a href="?{% qs_replace 'page' page_obj.next_page_number %}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
Expand Down
21 changes: 21 additions & 0 deletions promgen/templatetags/promgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,24 @@ def to_tag():
yield "</ol>"

return mark_safe("".join(to_tag()))


@register.simple_tag(takes_context=True)
def qs_replace(context, k, v):
"""
Query string handler for paginators

Assuming we have a query string like ?page=1&search=foo, there are several cases
in which we want to replace only the page key, while leaving the rest alone.
This tag allows us to replace individual values (like the current page) while
carrying over other values (like a search string)

Example:
{% qs_replace 'page' page_obj.next_page_number %}
"""
dict_ = context["request"].GET.copy()
if v:
dict_[k] = v
else:
dict_.pop(k, None)
return dict_.urlencode()