mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 14:51:08 -05:00
- Created critical functionality audit report identifying 7 critical issues affecting production readiness. - Added design assessment report highlighting exceptional design quality and minor cosmetic fixes needed. - Documented non-authenticated features testing results confirming successful functionality and public access. - Implemented ride search form with autocomplete functionality and corresponding templates for search results. - Developed tests for ride autocomplete functionality, ensuring proper filtering and authentication checks.
121 lines
4.0 KiB
Python
121 lines
4.0 KiB
Python
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
|
|
|
|
import autocomplete
|
|
from rides.models import Ride
|
|
|
|
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'
|
|
"""
|
|
|
|
|
|
@autocomplete.register
|
|
class RideAutocomplete(autocomplete.ModelAutocomplete):
|
|
"""Autocomplete for searching rides.
|
|
|
|
Features:
|
|
- Name-based search with partial matching
|
|
- Includes park name in results for context
|
|
- Prefetches related park data for performance
|
|
- Formats results as "Ride Name - Park Name"
|
|
- Limited to 10 results for performance
|
|
"""
|
|
model = Ride
|
|
search_attrs = ['name'] # We'll match on ride names
|
|
max_results = 10 # Limit to 10 for performance
|
|
|
|
def get_search_results(self, search, context):
|
|
"""Return search results with related park data."""
|
|
return (Ride.objects
|
|
.filter(name__icontains=search)
|
|
.select_related('park')
|
|
.order_by('name')[:self.max_results])
|
|
|
|
def format_result(self, ride):
|
|
"""Format each ride result with park name for context."""
|
|
return {
|
|
'key': str(ride.pk),
|
|
'label': ride.name,
|
|
'extra': f"at {ride.park.name}"
|
|
} |