Add search app configuration, views, and templates for advanced filtering functionality

This commit is contained in:
pacnpal
2025-02-12 12:58:04 -05:00
parent 62723d0e33
commit af57592496
14 changed files with 1047 additions and 3 deletions

View File

@@ -0,0 +1,81 @@
{% load static %}
<div class="filter-container bg-white rounded-lg shadow p-4" x-data="{ open: false }">
{# Mobile Filter Toggle #}
<div class="lg:hidden">
<button @click="open = !open" type="button" class="w-full flex items-center justify-between p-2 text-gray-400 hover:text-gray-500">
<span class="font-medium text-gray-900">Filters</span>
<span class="ml-6 flex items-center">
<svg class="w-5 h-5" x-show="!open" fill="currentColor" viewBox="0 0 20 20">
<path d="M10 6L16 12H4L10 6Z"/>
</svg>
<svg class="w-5 h-5" x-show="open" fill="currentColor" viewBox="0 0 20 20">
<path d="M10 14L4 8H16L10 14Z"/>
</svg>
</span>
</button>
</div>
{# Filter Form #}
<form hx-get="{{ request.path }}"
hx-trigger="change delay:500ms, submit"
hx-target="#results-container"
hx-push-url="true"
class="mt-4 lg:mt-0"
x-show="open || $screen('lg')"
x-transition>
{# Active Filters Summary #}
{% if applied_filters %}
<div class="bg-blue-50 p-4 rounded-lg mb-4">
<div class="flex justify-between items-center">
<h3 class="text-sm font-medium text-blue-800">Active Filters</h3>
<a href="{{ request.path }}"
class="text-sm text-blue-600 hover:text-blue-500"
hx-get="{{ request.path }}"
hx-target="#results-container"
hx-push-url="true">
Clear All
</a>
</div>
</div>
{% endif %}
{# Filter Groups #}
<div class="space-y-4">
{% for fieldset in filter.form|groupby_filters %}
<div class="border-b border-gray-200 pb-4">
<h3 class="text-sm font-medium text-gray-900 mb-3">{{ fieldset.name }}</h3>
<div class="space-y-3">
{% for field in fieldset.fields %}
<div>
<label for="{{ field.id_for_label }}" class="text-sm text-gray-600">
{{ field.label }}
</label>
<div class="mt-1">
{{ field }}
</div>
{% if field.help_text %}
<p class="mt-1 text-xs text-gray-500">{{ field.help_text }}</p>
{% endif %}
</div>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
{# Submit Button - Only visible on mobile #}
<div class="mt-4 lg:hidden">
<button type="submit"
class="w-full bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500">
Apply Filters
</button>
</div>
</form>
</div>
{% block extra_scripts %}
{# Add Alpine.js for mobile menu toggle if not already included #}
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
{% endblock %}

View File

@@ -0,0 +1,28 @@
<form hx-get="{% url 'search:search' %}" hx-target="#search-results" hx-swap="outerHTML" class="space-y-4">
{% for field in filters.form %}
<div class="flex flex-col">
<label for="{{ field.id_for_label }}" class="text-sm font-medium text-gray-700">
{{ field.label }}
</label>
<div class="mt-1">
{{ field }}
</div>
{% if field.help_text %}
<p class="text-sm text-gray-500">{{ field.help_text }}</p>
{% endif %}
</div>
{% endfor %}
<div class="flex justify-between">
<button type="submit"
class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
Apply Filters
</button>
{% if applied_filters %}
<a href="{% url 'search:search' %}"
class="inline-flex justify-center py-2 px-4 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
Clear Filters
</a>
{% endif %}
</div>
</form>

View File

@@ -0,0 +1,61 @@
{% extends "base.html" %}
{% load static %}
{% block title %}{{ view.model|model_name_plural|title }} - ThrillWiki{% endblock %}
{% block content %}
<div class="container mx-auto px-4 py-8">
<div class="flex flex-col lg:flex-row gap-8">
{# Filters Sidebar #}
<div class="lg:w-1/4">
{% include "search/components/filter_form.html" %}
</div>
{# Results Section #}
<div class="lg:w-3/4">
<div id="results-container">
{# Result count and sorting #}
<div class="bg-white rounded-lg shadow mb-4">
<div class="p-4 border-b">
<div class="flex justify-between items-center">
<h2 class="text-xl font-bold">
{{ view.model|model_name_plural|title }}
<span class="text-sm font-normal text-gray-500">({{ page_obj.paginator.count }} found)</span>
</h2>
{% block list_actions %}
{# Custom actions can be added here by extending views #}
{% endblock %}
</div>
</div>
</div>
{# Results list #}
{% block results_list %}
<div class="bg-white rounded-lg shadow">
{% include results_template|default:"search/partials/generic_results.html" %}
</div>
{% endblock %}
{# Pagination #}
{% if is_paginated %}
<div class="mt-4">
{% include "search/components/pagination.html" %}
</div>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock %}
{% block extra_scripts %}
<script>
// Handle browser back/forward with HTMX
document.body.addEventListener('htmx:beforeOnLoad', function(evt) {
if (evt.detail.requestConfig.verb === "get") {
history.replaceState(null, '', evt.detail.requestConfig.path);
}
});
</script>
{% endblock %}

View File

@@ -0,0 +1,134 @@
<div class="divide-y">
{% for object in object_list %}
<div class="p-4">
<h3 class="text-lg font-semibold">
<a href="{{ object.get_absolute_url }}" class="hover:text-blue-600">
{{ object }}
</a>
</h3>
{% if object.description %}
<p class="mt-2 text-sm text-gray-600">
{{ object.description|truncatewords:30 }}
</p>
{% endif %}
{% block object_metadata %}
<div class="mt-2 flex flex-wrap gap-2">
{% if object.created_at %}
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
Added {{ object.created_at|date }}
</span>
{% endif %}
{% if object.average_rating %}
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
{{ object.average_rating }} ★
</span>
{% endif %}
{% if object.location.exists %}
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
{{ object.location.first.get_formatted_address }}
</span>
{% endif %}
</div>
{% endblock %}
</div>
{% empty %}
<div class="p-8 text-center text-gray-500">
<p>No {{ view.model|model_name_plural }} found matching your criteria.</p>
{% if applied_filters %}
<p class="mt-2">
<a href="{{ request.path }}"
class="text-blue-600 hover:text-blue-500"
hx-get="{{ request.path }}"
hx-target="#results-container"
hx-push-url="true">
Clear all filters
</a>
</p>
{% endif %}
</div>
{% endfor %}
</div>
{% if is_paginated %}
<div class="px-4 py-3 flex items-center justify-between border-t border-gray-200 sm:px-6">
<div class="flex-1 flex justify-between sm:hidden">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}"
class="relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50"
hx-get="?page={{ page_obj.previous_page_number }}"
hx-target="#results-container"
hx-push-url="true">
Previous
</a>
{% endif %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}"
class="ml-3 relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50"
hx-get="?page={{ page_obj.next_page_number }}"
hx-target="#results-container"
hx-push-url="true">
Next
</a>
{% endif %}
</div>
<div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">
<div>
<p class="text-sm text-gray-700">
Showing <span class="font-medium">{{ page_obj.start_index }}</span>
to <span class="font-medium">{{ page_obj.end_index }}</span>
of <span class="font-medium">{{ page_obj.paginator.count }}</span>
results
</p>
</div>
<div>
<nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px" aria-label="Pagination">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}"
class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50"
hx-get="?page={{ page_obj.previous_page_number }}"
hx-target="#results-container"
hx-push-url="true">
<span class="sr-only">Previous</span>
<svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" />
</svg>
</a>
{% endif %}
{% for i in page_obj.paginator.page_range %}
{% if i == page_obj.number %}
<span class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-blue-50 text-sm font-medium text-blue-600">
{{ i }}
</span>
{% elif i > page_obj.number|add:"-3" and i < page_obj.number|add:"3" %}
<a href="?page={{ i }}"
class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50"
hx-get="?page={{ i }}"
hx-target="#results-container"
hx-push-url="true">
{{ i }}
</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}"
class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50"
hx-get="?page={{ page_obj.next_page_number }}"
hx-target="#results-container"
hx-push-url="true">
<span class="sr-only">Next</span>
<svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" />
</svg>
</a>
{% endif %}
</nav>
</div>
</div>
</div>
{% endif %}

View File

@@ -0,0 +1,100 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Search Parks - ThrillWiki{% endblock %}
{% block content %}
<div class="container mx-auto px-4 py-8">
<div class="flex flex-col lg:flex-row gap-8">
<!-- Filters Sidebar -->
<div class="lg:w-1/4">
<div class="bg-white p-6 rounded-lg shadow">
<h2 class="text-xl font-bold mb-4">Filter Parks</h2>
{% include "search/filters.html" %}
</div>
</div>
<!-- Results Section -->
<div class="lg:w-3/4" id="search-results">
<div class="bg-white rounded-lg shadow">
<div class="p-6 border-b">
<div class="flex justify-between items-center">
<h2 class="text-xl font-bold">
Search Results
<span class="text-sm font-normal text-gray-500">({{ results.count }} found)</span>
</h2>
</div>
</div>
<div class="divide-y">
{% for park in results %}
<div class="p-6 flex flex-col md:flex-row gap-4">
<!-- Park Image -->
<div class="md:w-48 h-32 bg-gray-200 rounded-lg overflow-hidden">
{% if park.photos.exists %}
<img src="{{ park.photos.first.image.url }}"
alt="{{ park.name }}"
class="w-full h-full object-cover">
{% else %}
<div class="w-full h-full flex items-center justify-center text-gray-400">
No Image
</div>
{% endif %}
</div>
<!-- Park Details -->
<div class="flex-1">
<h3 class="text-lg font-semibold">
<a href="{{ park.get_absolute_url }}" class="hover:text-blue-600">
{{ park.name }}
</a>
</h3>
<div class="mt-2 text-sm text-gray-600">
{% if park.formatted_location %}
<p>{{ park.formatted_location }}</p>
{% endif %}
</div>
<div class="mt-2 flex flex-wrap gap-2">
{% if park.average_rating %}
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
{{ park.average_rating }} ★
</span>
{% endif %}
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
{{ park.get_status_display }}
</span>
{% if park.ride_count %}
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-purple-100 text-purple-800">
{{ park.ride_count }} Rides
</span>
{% endif %}
{% if park.coaster_count %}
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800">
{{ park.coaster_count }} Coasters
</span>
{% endif %}
</div>
{% if park.description %}
<p class="mt-2 text-sm text-gray-600 line-clamp-2">
{{ park.description }}
</p>
{% endif %}
</div>
</div>
{% empty %}
<div class="p-6 text-center text-gray-500">
No parks found matching your criteria.
</div>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}