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

Show more records, records items per page #306

Merged
merged 3 commits into from
Nov 22, 2023
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
48 changes: 47 additions & 1 deletion qgis-app/plugins/templates/plugins/plugin_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,54 @@ <h2>{% if title %}{{title}}{% else %}{% trans "All plugins" %}{% endif %}</h2>
{% endfor %}
</tbody>
</table>
<div class="pagination">
<div class="pagination" style="display:flex;justify-content: space-between;align-items: center;">
{% include 'sortable_listview/pagination.html' %}

<div style="margin-top:20px;">
{% if show_more_items_number <= page_obj.paginator.count %}
<button class="btn" onclick="set_per_page({{show_more_items_number}})">
{% trans "Show more" %}
</button>
{% else %}
<button class="btn" disabled>
{% trans "Show more" %}
</button>
{% endif %}
</div>
<div style="margin-top:20px;">
<span>
{% trans "Elements per page:" %}&nbsp;
</span>
<select class="form-select" id="items_per_page" style="margin:0;">
{% if paginator.per_page not in per_page_list%}
<option>{{paginator.per_page}}</option>
{% endif %}
{% for p in per_page_list %}
<option value="{{ p }}" {% if p == paginator.per_page%} selected {% endif %}>{{ p }}</option>
{% endfor %}
</select>

</div>
<script type="text/javascript">
function set_per_page(n){
if(window.location.href.search('per_page') >= 0){
new_page = window.location.href.replace(/per_page=\d+/, 'per_page=' + n);
} else {
new_page = -1 == window.location.href.search('\\?') ? window.location.href + '?' + 'per_page=' + n : window.location.href + '&' + 'per_page=' + n;
}
// Reset page
new_page = new_page.replace(/&page=\d+/, '&page=1');
new_page = new_page.replace(/\?page=\d+/, '?page=1');
window.location.href = new_page;
}

let itemsPerPageSelect = document.getElementById("items_per_page");
itemsPerPageSelect.addEventListener("change", function () {
if (itemsPerPageSelect.value) {
set_per_page(itemsPerPageSelect.value)
}
});
</script>
</div>
<div class="alert">
<button type="button" class="close" data-dismiss="alert">&times;</button>
Expand Down
57 changes: 57 additions & 0 deletions qgis-app/plugins/tests/test_plugin_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django.test import TestCase
from django.urls import reverse
from ..models import Plugin

class PluginsListViewTestCase(TestCase):
fixtures = [
"fixtures/styles.json",
"fixtures/auth.json",
"fixtures/simplemenu.json",
"fixtures/plugins.json",
]

def setUp(self):
pass

def test_plugins_list_view(self):
# Test the main plugins list view without any parameters
response = self.client.get(reverse('approved_plugins'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'plugins/plugin_list.html')
self.assertTrue('current_sort_query' in response.context)
self.assertTrue('current_querystring' in response.context)
self.assertTrue('per_page_list' in response.context)
self.assertTrue('show_more_items_number' in response.context)

def test_plugins_list_pagination(self):
# Test the plugins list view with pagination
response = self.client.get(reverse('approved_plugins'), {'per_page': 20})
self.assertEqual(response.status_code, 200)
self.assertTrue('current_sort_query' in response.context)
self.assertTrue('current_querystring' in response.context)
self.assertTrue('per_page_list' in response.context)
self.assertTrue('show_more_items_number' in response.context)

show_more_items_number = response.context['show_more_items_number']
self.assertEqual(show_more_items_number, 50)

response = self.client.get(reverse('approved_plugins'), {'per_page': 110})
self.assertEqual(response.status_code, 200)
self.assertTrue('current_sort_query' in response.context)
self.assertTrue('current_querystring' in response.context)
self.assertTrue('per_page_list' in response.context)
self.assertTrue('show_more_items_number' in response.context)

show_more_items_number = response.context['show_more_items_number']
records_count = Plugin.approved_objects.count()
self.assertEqual(show_more_items_number, records_count + 1)

def test_plugins_list_sorting(self):
# Test the plugins list view with sorting
response = self.client.get(reverse('approved_plugins'), {'sort': 'name'})
self.assertEqual(response.status_code, 200)
self.assertTrue('current_sort_query' in response.context)
self.assertTrue('current_querystring' in response.context)
self.assertTrue('per_page_list' in response.context)
self.assertTrue('show_more_items_number' in response.context)

14 changes: 14 additions & 0 deletions qgis-app/plugins/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,20 @@ def get_context_data(self, **kwargs):
context.update(self.additional_context)
context["current_sort_query"] = self.get_sortstring()
context["current_querystring"] = self.get_querystring()
context["per_page_list"] = [20, 50, 75, 100]

try:
# Get the next value of per page from per_page_list
next_per_page_id = context["per_page_list"].index(context["paginator"].per_page) + 1
next_per_page = context["per_page_list"][next_per_page_id]
except (ValueError, IndexError):
# If the 'per_page' value in the request parameter
# is not found in the 'per_page_list' or if the
# next index is out of range, set the 'next_per_page'
# value to a number greater than the total count
# of records. This action effectively disables the button."
next_per_page = context["paginator"].count + 1
context["show_more_items_number"] = next_per_page
return context

def get_sortstring(self):
Expand Down
Loading