diff --git a/project/main/templates/main/filter_list.html b/project/main/templates/main/filter_list.html index 5eccfae..52c57a8 100644 --- a/project/main/templates/main/filter_list.html +++ b/project/main/templates/main/filter_list.html @@ -3,5 +3,66 @@ {% block content %} - {{ js_alert_work_in_progress() }} +
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+ {% include "main/filter_list_content.html" %} +
{% endblock %} diff --git a/project/main/templates/main/filter_list_content.html b/project/main/templates/main/filter_list_content.html new file mode 100644 index 0000000..558657f --- /dev/null +++ b/project/main/templates/main/filter_list_content.html @@ -0,0 +1,53 @@ + + + + + + + + + + + {% for area in areas %} + + + + + + + {% endfor %} + +
Zip CodePost OfficeAreaCounty
{{ area.post_office.zip }}{{ area.post_office.name }}{{ area.name }}{{ area.county.name }}
+ + +{% if pagination.page_count > 1 %} + +{% endif %} \ No newline at end of file diff --git a/project/main/views/__init__.py b/project/main/views/__init__.py index ba8d607..a75755b 100644 --- a/project/main/views/__init__.py +++ b/project/main/views/__init__.py @@ -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 diff --git a/project/main/views/filter_list.py b/project/main/views/filter_list.py index 831c811..9bffa95 100644 --- a/project/main/views/filter_list.py +++ b/project/main/views/filter_list.py @@ -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 diff --git a/project/urls.py b/project/urls.py index 1c76271..a3a646e 100644 --- a/project/urls.py +++ b/project/urls.py @@ -10,6 +10,9 @@ urlpatterns = [ path("", views.HomeView.as_view(), name="home"), path("swap", views.SwapView.as_view(), name="swap"), path("filter-list", views.FilterListView.as_view(), name="filter-list"), + path( + "filter-list/filter", views.FilterListFilterView.as_view(), name="filter-list-filter" + ), path("form-validation", views.FormValidationView.as_view(), name="form-validation"), path("complex-form", views.ComplexFormView.as_view(), name="complex-form"), path(