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

Have provided pagination for the case table #242

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
58 changes: 54 additions & 4 deletions legal_db/templates/legal_db/case/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load i18n static %}

{% block body_content %}

<section class="hero case">
<div class="container">
{% include "legal_db/partials/_breadcrumb.html" %}
Expand Down Expand Up @@ -53,17 +54,29 @@ <h2 class="card-title">{% trans "Contribute" %}</h2>
<table class="table is-bordered is-striped is-fullwidth">
<thead>
<tr>
<th>{% trans "Country" %}</th>
<th>{% trans "Case name" %}</th>
<th>
<a href="?sort=country&direction={% if direction == 'desc' %}asc{% else %}desc{% endif %}{% if request.GET.keywords %}&keywords={{ request.GET.keywords }}{% endif %}">
{% trans "Country" %}
</a>
</th>
<th>
<a href="?sort=name&direction={% if direction == 'desc' %}asc{% else %}desc{% endif %}{% if request.GET.keywords %}&keywords={{ request.GET.keywords }}{% endif %}">
{% trans "Case name" %}
</a>
</th>
<th>{% trans "License" %}</th>
<th>{% trans "Year of resolution" %}</th>
<th>
<a href="?sort=decision_year&direction={% if direction == 'desc' %}asc{% else %}desc{% endif %}{% if request.GET.keywords %}&keywords={{ request.GET.keywords }}{% endif %}">
{% trans "Year of resolution" %}
</a>
</th>
</tr>
</thead>
<tbody>
{% for case in cases %}
<tr>
<td>{{ case.country.name }}</td>
<td><a href="{% url 'case_detail' case.id %}" class="has-text-dark-slate-blue">{{ case.name }}</a> </td>
<td><a href="{% url 'case_detail' case.id %}" class="has-text-dark-slate-blue">{{ case.name }}</a></td>
<td class="is-narrow">
{% if not case.license %} <span class="has-text-grey">{% trans "Unknown" %}</span>
{% else %} {{ case.license }} {% endif %}
Expand All @@ -76,6 +89,43 @@ <h2 class="card-title">{% trans "Contribute" %}</h2>
</tbody>
</table>
</div>

<nav class="pagination is-centered" role="navigation" aria-label="pagination">
<ul class="pagination-list">
{% if page_obj.has_previous %}
<li>
<a class="pagination-link button tag case margin-right-small margin-vertical-smaller" href="?page=1&sort={{ current_sort }}&direction={{ direction }}">&laquo; First</a>
</li>
<li>
<a class="pagination-link button tag case margin-right-small margin-vertical-smaller" href="?page={{ page_obj.previous_page_number }}&sort={{ current_sort }}&direction={{ direction }}">Previous</a>
</li>
{% endif %}

{% for page in custom_page_range %}
{% if page == "..." %}
<li><span class="pagination-ellipsis">&hellip;</span></li>
{% elif page == page_obj.number %}
<li>
<a class="pagination-link is-current button tag margin-right-small margin-vertical-smaller">{{ page }}</a>
</li>
{% else %}
<li>
<a class="pagination-link button tag case margin-right-small margin-vertical-smaller" href="?page={{ page }}&sort={{ current_sort }}&direction={{ direction }}">{{ page }}</a>
</li>
{% endif %}
{% endfor %}

{% if page_obj.has_next %}
<li>
<a class="pagination-link button tag case margin-right-small margin-vertical-smaller" href="?page={{ page_obj.next_page_number }}&sort={{ current_sort }}&direction={{ direction }}">Next</a>
</li>
<li>
<a class="pagination-link button tag case margin-right-small margin-vertical-smaller" href="?page={{ page_obj.paginator.num_pages }}&sort={{ current_sort }}&direction={{ direction }}">Last &raquo;</a>
</li>
{% endif %}
</ul>
</nav>

</div>
</section>
{% endblock %}
Expand Down
44 changes: 39 additions & 5 deletions legal_db/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.shortcuts import redirect, render
from django.views.generic import DetailView, ListView, TemplateView
from taggit.models import Tag
from django.core.paginator import Paginator
from django.db.models import Q

from .forms import CaseForm, LinkForm, LinkFormset, ScholarshipForm, SearchForm
from .models import FAQ, Case, Link, Scholarship
Expand All @@ -24,11 +26,11 @@ def get_context_data(self, **kwargs):
class CaseListView(ListView):
template_name = "legal_db/case/index.html"
context_object_name = "cases"
paginate_by = 8

def get_queryset(self):
"""
Get only rows with PUBLISHED status and filtered by user input and
tags.
Get only rows with PUBLISHED status and filtered by user input and tags.
"""
qs = Case.objects.filter(status=Case.Status.PUBLISHED).only(
"country", "name", "license", "decision_year"
Expand All @@ -53,20 +55,52 @@ def get_queryset(self):
if tags:
qs = qs.filter(tags__name__in=tags)

return qs.order_by("country", "name")
sort_by = self.request.GET.get('sort', 'name')
direction = self.request.GET.get('direction', 'asc')
if direction == 'desc':
sort_by = '-' + sort_by

return qs.order_by(sort_by)

def get_context_data(self, **kwargs):
"""
Append search form and tags marking the selected ones.
Append search form, tags marking the selected ones, and customized pagination range.
"""
context = super().get_context_data(**kwargs)
selected_tags = self.request.GET.getlist("tags[]")
tags = []
for tag in Tag.objects.exclude(case=None).order_by("name"):
checked = True if tag.name in selected_tags else False
checked = tag.name in selected_tags
tags.append({"name": tag, "checked": checked})
context["tags"] = tags
context["form"] = SearchForm(self.request.GET)

context["current_sort"] = self.request.GET.get('sort', 'name')
context["direction"] = self.request.GET.get('direction', 'asc')

page_obj = context["page_obj"]
paginator = page_obj.paginator
current_page = page_obj.number
total_pages = paginator.num_pages

start_page = max(current_page - 2, 1)
end_page = min(current_page + 2, total_pages)

page_range = []
if start_page > 1:
page_range.append(1)
if start_page > 2:
page_range.append("...")

page_range.extend(range(start_page, end_page + 1))

if end_page < total_pages:
if end_page < total_pages - 1:
page_range.append("...")
page_range.append(total_pages)

context["custom_page_range"] = page_range

return context


Expand Down