Skip to content

Commit

Permalink
Fix pagination of search results
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Mar 20, 2024
1 parent e4f549e commit 2f65467
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions radis/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import ValidationError
from django.core.paginator import Paginator
from django.http import HttpRequest
from django.http import Http404, HttpRequest
from django.shortcuts import render
from django.views import View

Expand Down Expand Up @@ -32,14 +32,19 @@ def get(self, request: AuthenticatedHttpRequest, *args, **kwargs):
age_from = form.cleaned_data["age_from"]
age_till = form.cleaned_data["age_till"]

search_provider = search_providers[provider]
context["info_template"] = search_provider.info_template

page_number = self.get_page_number(request)
page_size: int = self.get_page_size(request)

if page_size * page_number > search_provider.max_results:
# https://github.com/django/django/blob/6f7c0a4d66f36c59ae9eafa168b455e462d81901/django/views/generic/list.py#L76
raise Http404(f"Invalid page {page_number}.")

offset = (page_number - 1) * page_size
context["offset"] = offset

search_provider = search_providers[provider]
context["info_template"] = search_provider.info_template

if query:
search = Search(
query=query,
Expand Down Expand Up @@ -75,17 +80,15 @@ def get(self, request: AuthenticatedHttpRequest, *args, **kwargs):
def get_page_number(self, request: HttpRequest) -> int:
page = request.GET.get("page") or 1
try:
page_number = int(page)
page_number = min(page_number, 1)
page_number = max(int(page), 1)
except ValueError:
page_number = 1
return page_number

def get_page_size(self, request: HttpRequest) -> int:
page_size = request.GET.get("per_page") or 25
try:
page_size = int(page_size)
page_size = min(page_size, 100)
page_size = min(int(page_size), 100)
except ValueError:
page_size = 10
return page_size
Expand Down

0 comments on commit 2f65467

Please sign in to comment.