mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-21 06:51:08 -05:00
Add search app configuration, views, and templates for advanced filtering functionality
This commit is contained in:
87
search/mixins.py
Normal file
87
search/mixins.py
Normal file
@@ -0,0 +1,87 @@
|
||||
from typing import Any, Dict, Optional, Type
|
||||
from django.db.models import QuerySet
|
||||
from django.views.generic.list import ListView
|
||||
from django_filters import FilterSet
|
||||
from .filters import create_model_filter, LocationFilterMixin, RatingFilterMixin, DateRangeFilterMixin
|
||||
|
||||
class FilterableViewMixin:
|
||||
"""
|
||||
Mixin to add filtering capabilities to any ListView
|
||||
"""
|
||||
filter_class: Optional[Type[FilterSet]] = None
|
||||
search_fields: list[str] = None
|
||||
exclude_fields: list[str] = None
|
||||
additional_filters: Dict[str, Any] = None
|
||||
filter_mixins: list[Type] = None
|
||||
template_name_suffix = '_list'
|
||||
|
||||
def get_filter_class(self) -> Type[FilterSet]:
|
||||
"""
|
||||
Get or create the filter class for the view
|
||||
"""
|
||||
if self.filter_class is not None:
|
||||
return self.filter_class
|
||||
|
||||
if not self.model:
|
||||
raise ValueError("Model must be defined to use FilterableViewMixin")
|
||||
|
||||
return create_model_filter(
|
||||
model=self.model,
|
||||
search_fields=self.search_fields,
|
||||
exclude_fields=self.exclude_fields,
|
||||
additional_filters=self.additional_filters,
|
||||
mixins=self.filter_mixins or []
|
||||
)
|
||||
|
||||
def get_filter_mixins(self) -> list:
|
||||
"""
|
||||
Get the filter mixins to use. Override to customize.
|
||||
"""
|
||||
return [LocationFilterMixin, RatingFilterMixin, DateRangeFilterMixin]
|
||||
|
||||
def get_queryset(self) -> QuerySet:
|
||||
"""
|
||||
Apply filters to the queryset
|
||||
"""
|
||||
queryset = super().get_queryset()
|
||||
self.filterset = self.get_filter_class()(self.request.GET, queryset=queryset)
|
||||
return self.filterset.qs
|
||||
|
||||
def get_context_data(self, **kwargs) -> Dict[str, Any]:
|
||||
"""
|
||||
Add filter-related context
|
||||
"""
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['filter'] = self.filterset
|
||||
context['applied_filters'] = bool(self.request.GET)
|
||||
return context
|
||||
|
||||
class HTMXFilterableMixin(FilterableViewMixin):
|
||||
"""
|
||||
Extension of FilterableViewMixin that adds HTMX support
|
||||
"""
|
||||
def get_template_names(self) -> list[str]:
|
||||
"""
|
||||
Return different templates based on HTMX request
|
||||
"""
|
||||
if self.request.htmx:
|
||||
# If it's an HTMX request, return just the results partial
|
||||
return [f"search/partials/{self.model._meta.model_name}_results.html"]
|
||||
return super().get_template_names()
|
||||
|
||||
# Example Usage:
|
||||
"""
|
||||
class ParkListView(HTMXFilterableMixin, ListView):
|
||||
model = Park
|
||||
template_name = 'parks/park_list.html'
|
||||
search_fields = ['name', 'description']
|
||||
additional_filters = {
|
||||
'min_rides': django_filters.NumberFilter(field_name='ride_count', lookup_expr='gte')
|
||||
}
|
||||
|
||||
# Or with a custom filter class:
|
||||
class RideListView(HTMXFilterableMixin, ListView):
|
||||
model = Ride
|
||||
filter_class = CustomRideFilter
|
||||
template_name = 'rides/ride_list.html'
|
||||
"""
|
||||
Reference in New Issue
Block a user