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

configurable items per page #764

Merged
merged 1 commit into from
Oct 27, 2024
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
20 changes: 12 additions & 8 deletions catalog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
from django.views.decorators.clickjacking import xframe_options_exempt
from django.views.decorators.http import require_http_methods

from common.utils import PageLinksGenerator, get_uuid_or_404, user_identity_required
from common.utils import (
CustomPaginator,
PageLinksGenerator,
get_uuid_or_404,
user_identity_required,
)
from journal.models import (
Collection,
Comment,
Expand All @@ -30,8 +35,7 @@
from .search.views import *
from .views_edit import *

NUM_REVIEWS_ON_ITEM_PAGE = 5
NUM_REVIEWS_ON_LIST_PAGE = 20
NUM_COMMENTS_ON_ITEM_PAGE = 10


def retrieve_by_uuid(request, item_uid):
Expand Down Expand Up @@ -151,7 +155,7 @@ def mark_list(request, item_path, item_uuid, following_only=False):
queryset = queryset.filter(q_piece_in_home_feed_of_user(request.user))
else:
queryset = queryset.filter(q_piece_visible_to_user(request.user))
paginator = Paginator(queryset, NUM_REVIEWS_ON_LIST_PAGE)
paginator = CustomPaginator(queryset, request)
page_number = request.GET.get("page", default=1)
marks = paginator.get_page(page_number)
pagination = PageLinksGenerator(page_number, paginator.num_pages, request.GET)
Expand All @@ -171,7 +175,7 @@ def review_list(request, item_path, item_uuid):
item = get_object_or_404(Item, uid=get_uuid_or_404(item_uuid))
queryset = Review.objects.filter(item=item).order_by("-created_time")
queryset = queryset.filter(q_piece_visible_to_user(request.user))
paginator = Paginator(queryset, NUM_REVIEWS_ON_LIST_PAGE)
paginator = CustomPaginator(queryset, request)
page_number = request.GET.get("page", default=1)
reviews = paginator.get_page(page_number)
pagination = PageLinksGenerator(page_number, paginator.num_pages, request.GET)
Expand Down Expand Up @@ -199,7 +203,7 @@ def comments(request, item_path, item_uuid):
"_item_comments.html",
{
"item": item,
"comments": queryset[:11],
"comments": queryset[: NUM_COMMENTS_ON_ITEM_PAGE + 1],
},
)

Expand All @@ -223,7 +227,7 @@ def comments_by_episode(request, item_path, item_uuid):
{
"item": item,
"episode_uuid": episode_uuid,
"comments": queryset[:11],
"comments": queryset[: NUM_COMMENTS_ON_ITEM_PAGE + 1],
},
)

Expand All @@ -241,7 +245,7 @@ def reviews(request, item_path, item_uuid):
"_item_reviews.html",
{
"item": item,
"reviews": queryset[:11],
"reviews": queryset[: NUM_COMMENTS_ON_ITEM_PAGE + 1],
},
)

Expand Down
1 change: 1 addition & 0 deletions common/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# how many items are showed in one search result page
ITEMS_PER_PAGE = 20
ITEMS_PER_PAGE_OPTIONS = [20, 40, 80]

# how many pages links in the pagination
PAGE_LINK_NUMBER = 7
Expand Down
19 changes: 18 additions & 1 deletion common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from django.conf import settings
from django.conf.locale import LANG_INFO
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.core.paginator import Paginator
from django.core.signing import b62_decode, b62_encode
from django.http import Http404, HttpRequest, HttpResponseRedirect, QueryDict
from django.utils import timezone
from django.utils.translation import gettext as _

from .config import PAGE_LINK_NUMBER
from .config import ITEMS_PER_PAGE, ITEMS_PER_PAGE_OPTIONS, PAGE_LINK_NUMBER

if TYPE_CHECKING:
from users.models import APIdentity, User
Expand Down Expand Up @@ -111,6 +112,22 @@ def wrapper(request, user_name, *args, **kwargs):
return wrapper


class CustomPaginator(Paginator):
def __init__(self, object_list, request=None) -> None:
per_page = ITEMS_PER_PAGE
if request:
try:
if request.GET.get("per_page"):
per_page = int(request.GET.get("per_page"))
elif request.COOKIES.get("per_page"):
per_page = int(request.COOKIES.get("per_page"))
except ValueError:
pass
if per_page not in ITEMS_PER_PAGE_OPTIONS:
per_page = ITEMS_PER_PAGE
super().__init__(object_list, per_page)


class PageLinksGenerator:
# TODO inherit django paginator
"""
Expand Down
3 changes: 2 additions & 1 deletion journal/views/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from catalog.models import *
from common.utils import (
AuthedHttpRequest,
CustomPaginator,
PageLinksGenerator,
get_uuid_or_404,
target_identity_required,
Expand Down Expand Up @@ -100,7 +101,7 @@ def render_list(
if year:
year = int(year)
queryset = queryset.filter(created_time__year=year)
paginator = Paginator(queryset, PAGE_SIZE) # type:ignore
paginator = CustomPaginator(queryset, request) # type:ignore
page_number = int(request.GET.get("page", default=1))
members = paginator.get_page(page_number)
pagination = PageLinksGenerator(page_number, paginator.num_pages, request.GET)
Expand Down
Loading