mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 05:11:09 -05:00
pycountry integration?
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
from django.views.generic import DetailView, ListView, CreateView
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
from django.urls import reverse
|
||||
from django.db.models import Q
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.http import JsonResponse, HttpResponseRedirect
|
||||
from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
|
||||
from .models import Park, ParkArea
|
||||
from rides.models import Ride
|
||||
from core.views import SlugRedirectMixin
|
||||
from moderation.mixins import EditSubmissionMixin, PhotoSubmissionMixin, InlineEditMixin, HistoryMixin
|
||||
from moderation.models import EditSubmission
|
||||
import pycountry
|
||||
|
||||
class ParkCreateView(LoginRequiredMixin, CreateView):
|
||||
model = Park
|
||||
@@ -42,6 +44,37 @@ class ParkCreateView(LoginRequiredMixin, CreateView):
|
||||
def get_success_url(self):
|
||||
return reverse('park_detail', kwargs={'slug': self.object.slug})
|
||||
|
||||
def search_countries(request):
|
||||
query = request.GET.get('q', '').strip()
|
||||
countries = []
|
||||
|
||||
if query:
|
||||
# Use pycountry's search functionality for fuzzy matching
|
||||
try:
|
||||
# Try exact search first
|
||||
country = pycountry.countries.get(name=query)
|
||||
if country:
|
||||
countries = [country]
|
||||
else:
|
||||
# If no exact match, try fuzzy search
|
||||
countries = pycountry.countries.search_fuzzy(query)
|
||||
except LookupError:
|
||||
# If search fails, fallback to manual filtering
|
||||
countries = [
|
||||
country for country in pycountry.countries
|
||||
if query.lower() in country.name.lower()
|
||||
]
|
||||
|
||||
return render(request, 'parks/partials/country_search_results.html', {
|
||||
'countries': countries[:10] # Limit to top 10 results
|
||||
})
|
||||
|
||||
def select_country(request):
|
||||
if request.method == 'POST':
|
||||
country = request.POST.get('country', '')
|
||||
return HttpResponse(country)
|
||||
return HttpResponse('Invalid request', status=400)
|
||||
|
||||
class ParkDetailView(SlugRedirectMixin, EditSubmissionMixin, PhotoSubmissionMixin, InlineEditMixin, HistoryMixin, DetailView):
|
||||
model = Park
|
||||
template_name = 'parks/park_detail.html'
|
||||
@@ -106,13 +139,15 @@ class ParkListView(ListView):
|
||||
def get_queryset(self):
|
||||
queryset = Park.objects.select_related('owner').prefetch_related('photos', 'rides')
|
||||
|
||||
# Apply filters
|
||||
search = self.request.GET.get('search', '').strip()
|
||||
location = self.request.GET.get('location', '').strip()
|
||||
status = self.request.GET.get('status', '').strip()
|
||||
search = self.request.GET.get('search', '').strip() or None
|
||||
location = self.request.GET.get('location', '').strip() or None
|
||||
status = self.request.GET.get('status', '').strip() or None
|
||||
|
||||
if search:
|
||||
queryset = queryset.filter(name__icontains=search) | queryset.filter(location__icontains=search)
|
||||
queryset = queryset.filter(
|
||||
Q(name__icontains=search) |
|
||||
Q(location__icontains=search)
|
||||
)
|
||||
if location:
|
||||
queryset = queryset.filter(location=location)
|
||||
if status:
|
||||
|
||||
Reference in New Issue
Block a user