From af57592496db9c65d88d806e57637c26e070e6ea Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Wed, 12 Feb 2025 12:58:04 -0500 Subject: [PATCH] Add search app configuration, views, and templates for advanced filtering functionality --- search/README.md | 106 ++++++++++++ search/apps.py | 1 - search/examples.py | 137 ++++++++++++++++ search/filters.py | 155 ++++++++++++++++++ search/mixins.py | 87 ++++++++++ .../search/components/filter_form.html | 81 +++++++++ search/templates/search/filters.html | 28 ++++ .../search/layouts/filtered_list.html | 61 +++++++ .../search/partials/generic_results.html | 134 +++++++++++++++ search/templates/search/results.html | 100 +++++++++++ search/templatetags/filter_utils.py | 100 +++++++++++ search/urls.py | 10 ++ search/views.py | 49 +++++- thrillwiki/settings.py | 1 + 14 files changed, 1047 insertions(+), 3 deletions(-) create mode 100644 search/README.md create mode 100644 search/examples.py create mode 100644 search/filters.py create mode 100644 search/mixins.py create mode 100644 search/templates/search/components/filter_form.html create mode 100644 search/templates/search/filters.html create mode 100644 search/templates/search/layouts/filtered_list.html create mode 100644 search/templates/search/partials/generic_results.html create mode 100644 search/templates/search/results.html create mode 100644 search/templatetags/filter_utils.py create mode 100644 search/urls.py diff --git a/search/README.md b/search/README.md new file mode 100644 index 00000000..2a92de60 --- /dev/null +++ b/search/README.md @@ -0,0 +1,106 @@ +# Search & Filter System + +A flexible, reusable search and filtering system that can be used across any Django model in the project. + +## Features + +- Modular filter system with composable mixins +- HTMX integration for dynamic updates +- Responsive, accessible filter UI components +- Automatic filter generation based on model fields +- Location-based filtering support +- Flexible template system + +## Usage + +### Basic Implementation + +Add filtering to any ListView by using the `HTMXFilterableMixin`: + +```python +from django.views.generic import ListView +from search.mixins import HTMXFilterableMixin + +class MyModelListView(HTMXFilterableMixin, ListView): + model = MyModel + template_name = "myapp/mymodel_list.html" + search_fields = ['name', 'description'] # Fields to include in text search +``` + +### Custom Filters + +Add custom filters for specific model needs: + +```python +additional_filters = { + 'category': ChoiceFilter(choices=MyModel.CATEGORY_CHOICES), + 'rating': RangeFilter(field_name='average_rating'), +} +``` + +### Template Integration + +Extend the base filtered list template: + +```html +{% extends "search/layouts/filtered_list.html" %} + +{% block list_actions %} + + Add New + +{% endblock %} +``` + +### Custom Result Display + +Create a custom results template in `templates/search/partials/mymodel_results.html`: + +```html +
+ {{ object.description|truncatewords:30 }} +
+ {% endif %} + + {% block object_metadata %} +No {{ view.model|model_name_plural }} found matching your criteria.
+ {% if applied_filters %} + + {% endif %} ++ Showing {{ page_obj.start_index }} + to {{ page_obj.end_index }} + of {{ page_obj.paginator.count }} + results +
+{{ park.formatted_location }}
+ {% endif %} ++ {{ park.description }} +
+ {% endif %} +