fix: video pagination
This commit is contained in:
+69
-5
@@ -1,8 +1,10 @@
|
||||
import random
|
||||
|
||||
from django.db.models import Prefetch
|
||||
from django.http import Http404
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse
|
||||
from django.views import View
|
||||
from django.views.generic import DetailView, ListView, TemplateView
|
||||
|
||||
@@ -21,6 +23,18 @@ from .models import (
|
||||
PageVideo,
|
||||
)
|
||||
|
||||
VIDEO_PREVIEW_LIMIT = 3
|
||||
VIDEO_ARCHIVE_PAGE_SIZE = 6
|
||||
|
||||
|
||||
def _video_preview_context(queryset, archive_url):
|
||||
total = queryset.count()
|
||||
return {
|
||||
"videos": queryset[:VIDEO_PREVIEW_LIMIT],
|
||||
"videos_total_count": total,
|
||||
"videos_archive_url": archive_url if total > VIDEO_PREVIEW_LIMIT else "",
|
||||
}
|
||||
|
||||
|
||||
def _homepage_products_catalog_queryset():
|
||||
return (
|
||||
@@ -79,10 +93,14 @@ class FAQView(ListView):
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
ctx["faq_videos"] = PageVideo.objects.filter(
|
||||
videos = PageVideo.objects.filter(
|
||||
page=PageVideo.PAGE_FAQ,
|
||||
is_active=True,
|
||||
).order_by("order")
|
||||
preview = _video_preview_context(videos, reverse("pages:video_archive", kwargs={"library": PageVideo.PAGE_FAQ}))
|
||||
ctx["faq_videos"] = preview["videos"]
|
||||
ctx["videos_total_count"] = preview["videos_total_count"]
|
||||
ctx["videos_archive_url"] = preview["videos_archive_url"]
|
||||
return ctx
|
||||
|
||||
|
||||
@@ -95,16 +113,23 @@ class ContactView(View):
|
||||
return f"{a} + {b}"
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
videos = PageVideo.objects.filter(
|
||||
page=PageVideo.PAGE_CONTACT,
|
||||
is_active=True,
|
||||
).order_by("order")
|
||||
preview = _video_preview_context(
|
||||
videos,
|
||||
reverse("pages:video_archive", kwargs={"library": PageVideo.PAGE_CONTACT}),
|
||||
)
|
||||
return render(
|
||||
request,
|
||||
self.template_name,
|
||||
{
|
||||
"form": ContactForm(),
|
||||
"captcha_question": self._new_captcha(request),
|
||||
"contact_videos": PageVideo.objects.filter(
|
||||
page=PageVideo.PAGE_CONTACT,
|
||||
is_active=True,
|
||||
).order_by("order"),
|
||||
"contact_videos": preview["videos"],
|
||||
"videos_total_count": preview["videos_total_count"],
|
||||
"videos_archive_url": preview["videos_archive_url"],
|
||||
},
|
||||
)
|
||||
|
||||
@@ -168,3 +193,42 @@ class CustomPageView(DetailView):
|
||||
)
|
||||
ctx["homepage_products_catalog"] = _homepage_products_catalog_queryset()
|
||||
return ctx
|
||||
|
||||
|
||||
class PageVideoArchiveView(ListView):
|
||||
model = PageVideo
|
||||
template_name = "videos/archive.html"
|
||||
context_object_name = "videos"
|
||||
paginate_by = VIDEO_ARCHIVE_PAGE_SIZE
|
||||
|
||||
PAGE_CONFIG = {
|
||||
PageVideo.PAGE_CONTACT: ("Contact videos", "Guides and updates from the Tecvico team.", "pages:contact"),
|
||||
PageVideo.PAGE_FAQ: ("FAQ videos", "Video answers to common questions.", "pages:faq"),
|
||||
}
|
||||
|
||||
def get_page_config(self):
|
||||
try:
|
||||
return self.PAGE_CONFIG[self.kwargs["library"]]
|
||||
except KeyError as exc:
|
||||
raise Http404("Video library not found.") from exc
|
||||
|
||||
def get_queryset(self):
|
||||
self.get_page_config()
|
||||
return PageVideo.objects.filter(
|
||||
page=self.kwargs["library"],
|
||||
is_active=True,
|
||||
).order_by("order", "pk")
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
title, description, source_url_name = self.get_page_config()
|
||||
context.update(
|
||||
{
|
||||
"library_title": title,
|
||||
"library_description": description,
|
||||
"library_back_url": reverse(source_url_name),
|
||||
"library_back_label": "Back to page",
|
||||
"pagination_range": context["paginator"].get_elided_page_range(context["page_obj"].number),
|
||||
}
|
||||
)
|
||||
return context
|
||||
|
||||
Reference in New Issue
Block a user