Skip to content

Commit

Permalink
💄(front) pagination search results
Browse files Browse the repository at this point in the history
If results were over three pages, <li> tag was missing its closing
tag resulting in a problem of navigation. Some pages were not
accessible anymore.

Resolve #240
  • Loading branch information
carofun committed Jan 13, 2022
1 parent 0314273 commit 9ac61d3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).
- exclude archived forums from the list of forums of advanced search
- automatically assign a public_username to instructors and administrators
when none is defined for a user already existing
- fix pagination for search results

## [1.1.0] - 2021-10-28

Expand Down
31 changes: 31 additions & 0 deletions src/ashley/templates/forum_search/pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% load i18n %}

<ul class="m-0 pagination {{ pagination_size|default:"" }}">
<li class="page-item{% if not page.has_previous %} disabled{% endif %}">
<a href="{% if page.has_previous %}?q={{ query }}&amp;page={{ page.previous_page_number }}{% endif %}" class="page-link">&laquo;</a>
</li>
{% for number in paginator.page_range %}
{% if forloop.first %}
<li class="page-item{% if page.number == number %} active{% endif %}"><a href="?q={{ query }}&amp;page={{ number }}" class="page-link">{{ number }}</a></li>
{% if page.number > 4 %}
<li class="page-item disabled"><a href="#" class="page-link">...</a></li>
{% endif %}
{% elif forloop.last %}
{% if page.number < paginator.num_pages|add:"-3" %}
<li class="page-item disabled"><a href="#" class="page-link">...</a></li>
{% endif %}
<li class="page-item{% if page.number == number %} active{% endif %}"><a href="?q={{ query }}&amp;page={{ number }}" class="page-link">{{ number }}</a></li>
{% else %}
{% if page.number < 3 and number <= 5 %}
<li class="page-item{% if page.number == number %} active{% endif %}"><a href="?q={{ query }}&amp;page={{ number }}" class="page-link">{{ number }}</a></li>
{% elif page.number > paginator.num_pages|add:"-2" and number >= paginator.num_pages|add:"-4" %}
<li class="page-item{% if page.number == number %} active{% endif %}"><a href="?q={{ query }}&amp;page={{ number }}" class="page-link">{{ number }}</a></li>
{% elif number >= page.previous_page_number|add:"-1" and number <= page.next_page_number|add:"1" %}
<li class="page-item{% if page.number == number %} active{% endif %}"><a href="?q={{ query }}&amp;page={{ number }}" class="page-link">{{ number }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
<li class="page-item{% if not page.has_next %} disabled{% endif %}">
<a href="{% if page.has_next %}?q={{ query }}&amp;page={{ page.next_page_number }}{% endif %}" class="page-link">&raquo;</a>
</li>
</ul>
45 changes: 45 additions & 0 deletions tests/ashley/test_forum_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
)
from ashley.machina_extensions.forum_search.forms import SearchForm

# pylint: disable = too-many-public-methods

Forum = get_model("forum", "Forum")

# Change index name so we don't mess up the site index for development
Expand Down Expand Up @@ -685,3 +687,46 @@ def test_forum_search_archived_form_search(self):
f"Select a valid choice. {forum2.id} is not one of the available choices.",
html=True,
)

def test_forum_search_paginated_links(self):
"""
Check pagination has well formed links
"""
user = UserFactory()
post = PostFactory(text="a5g3g6k75")
PostFactory.create_batch(100, text="a5g3g6k75", topic=post.topic)

assign_perm("can_read_forum", user, post.topic.forum)

# Index 101 posts in Elasticsearch
call_command("rebuild_index", interactive=False)

self.client.force_login(user)
response = self.client.get("/forum/search/?q=a5g3")
self.assertContains(
response, "Your search has returned <b>101</b> results", html=True
)
self.assertContains(response, post, html=True)

# 20 results per page and 2 panels of pagination
# check all li tags are well closed
self.assertContains(response, '<li class="page-item">', 12)
self.assertContains(response, '<li class="page-item active">', 2)
self.assertContains(response, '<li class="page-item disabled">', 4)

# check all links are well formed
self.assertContains(
response,
'<li class="page-item active">'
'<a href="?q=a5g3&amp;page=1" class="page-link">1</a>'
"</li>",
)
for page in range(2, 6):
self.assertContains(response, f"?q=a5g3&amp;page={page}")
self.assertContains(
response,
f'<li class="page-item"><a href="?q=a5g3&amp;page={page}" '
f'class="page-link">{page}</a></li>',
)

self.assertNotContains(response, "?q=a5g3&amp;page=7")

0 comments on commit 9ac61d3

Please sign in to comment.