Continue with list
This commit is contained in:
@ -4,7 +4,7 @@ from .complex_form.views import (
|
||||
RouteModuleHandleView,
|
||||
WarehouseManagementHandleView,
|
||||
)
|
||||
from .filter_list import FilterListView
|
||||
from .filter_list import FilterListFilterView, FilterListView
|
||||
from .form_validation import FormValidationView
|
||||
from .home import HomeView
|
||||
from .swap import SwapView
|
||||
|
||||
@ -1,7 +1,96 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Tuple
|
||||
|
||||
from django.db.models import Model, Q, QuerySet
|
||||
|
||||
from project.main.models import Area, County
|
||||
from project.main.views.demo_view_base import DemoViewBase
|
||||
|
||||
PAGE_SIZE = 20
|
||||
|
||||
class FilterListView(DemoViewBase):
|
||||
|
||||
def get_all_counties() -> QuerySet[County]:
|
||||
return County.objects.all()
|
||||
|
||||
|
||||
@dataclass
|
||||
class Pagination:
|
||||
page: int
|
||||
page_size: int
|
||||
page_count: int
|
||||
prev_page: int
|
||||
next_page: int
|
||||
|
||||
|
||||
class FilterListViewBase(DemoViewBase):
|
||||
def paginate(self, qs: QuerySet[Area]) -> Tuple[QuerySet[Area], Pagination]:
|
||||
page = int(self.request.GET.get("page", 1))
|
||||
page_size = int(self.request.GET.get("page_size", PAGE_SIZE))
|
||||
|
||||
start_offs = (page - 1) * page_size
|
||||
end_offs = start_offs + page_size
|
||||
|
||||
page_count = int(qs.count() / page_size)
|
||||
|
||||
pagination = Pagination(
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
page_count=page_count,
|
||||
prev_page=page - 1 if page > 1 else None,
|
||||
next_page=page + 1 if page < page_count else None,
|
||||
)
|
||||
|
||||
return qs[start_offs:end_offs], pagination
|
||||
|
||||
|
||||
class FilterListView(FilterListViewBase):
|
||||
template_name = "main/filter_list.html"
|
||||
active_section = "filter-list"
|
||||
title = "Filter List"
|
||||
|
||||
def get_context_data(self, **kwargs) -> dict[str, Any]:
|
||||
context_data = super().get_context_data(**kwargs)
|
||||
|
||||
qs, pagination = self.paginate(
|
||||
qs=Area.objects.select_related("county", "post_office").all(),
|
||||
)
|
||||
context_data.update(
|
||||
{
|
||||
"areas": qs,
|
||||
"counties": get_all_counties(),
|
||||
"pagination": pagination,
|
||||
}
|
||||
)
|
||||
return context_data
|
||||
|
||||
|
||||
class FilterListFilterView(FilterListViewBase):
|
||||
template_name = "main/filter_list_content.html"
|
||||
|
||||
def get_context_data(self, **kwargs) -> dict[str, Any]:
|
||||
context_data = super().get_context_data(**kwargs)
|
||||
|
||||
q = Q()
|
||||
zip = self.request.GET.get("zip", None)
|
||||
area = self.request.GET.get("area", None)
|
||||
county = self.request.GET.get("county", None)
|
||||
|
||||
if zip:
|
||||
q &= Q(post_office__zip=zip)
|
||||
if area:
|
||||
q &= Q(name__icontains=area)
|
||||
if county:
|
||||
q &= Q(county_id=county)
|
||||
|
||||
qs, pagination = self.paginate(
|
||||
qs=Area.objects.select_related("county", "post_office").filter(q),
|
||||
)
|
||||
|
||||
context_data.update(
|
||||
{
|
||||
"areas": qs,
|
||||
"counties": get_all_counties(),
|
||||
"pagination": pagination,
|
||||
}
|
||||
)
|
||||
return context_data
|
||||
|
||||
Reference in New Issue
Block a user