mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 05:11:09 -05:00
fixed the damn discord button
This commit is contained in:
122
parks/views.py
122
parks/views.py
@@ -7,17 +7,66 @@ from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
|
||||
from .models import Park, ParkArea
|
||||
from .forms import ParkForm
|
||||
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
|
||||
from cities_light.models import Country, Region, City
|
||||
|
||||
def get_countries(request):
|
||||
query = request.GET.get('q', '')
|
||||
countries = Country.objects.filter(name__icontains=query).values_list('name', flat=True)[:10]
|
||||
return JsonResponse(list(countries), safe=False)
|
||||
|
||||
def get_regions(request):
|
||||
query = request.GET.get('q', '')
|
||||
country = request.GET.get('country', '')
|
||||
if not country:
|
||||
return JsonResponse([], safe=False)
|
||||
|
||||
regions = Region.objects.filter(
|
||||
Q(name__icontains=query) | Q(alternate_names__icontains=query),
|
||||
country__name__iexact=country
|
||||
).values_list('name', flat=True)[:10]
|
||||
return JsonResponse(list(regions), safe=False)
|
||||
|
||||
def get_cities(request):
|
||||
query = request.GET.get('q', '')
|
||||
region = request.GET.get('region', '')
|
||||
country = request.GET.get('country', '')
|
||||
if not region or not country:
|
||||
return JsonResponse([], safe=False)
|
||||
|
||||
cities = City.objects.filter(
|
||||
Q(name__icontains=query) | Q(alternate_names__icontains=query),
|
||||
region__name__iexact=region,
|
||||
region__country__name__iexact=country
|
||||
).values_list('name', flat=True)[:10]
|
||||
return JsonResponse(list(cities), safe=False)
|
||||
|
||||
def get_locations(request):
|
||||
query = request.GET.get('q', '')
|
||||
locations = set()
|
||||
|
||||
# Search countries
|
||||
countries = Country.objects.filter(name__icontains=query).values_list('name', flat=True)[:5]
|
||||
locations.update(countries)
|
||||
|
||||
# Search regions
|
||||
regions = Region.objects.filter(name__icontains=query).values_list('name', flat=True)[:5]
|
||||
locations.update(regions)
|
||||
|
||||
# Search cities
|
||||
cities = City.objects.filter(name__icontains=query).values_list('name', flat=True)[:5]
|
||||
locations.update(cities)
|
||||
|
||||
return JsonResponse(list(locations), safe=False)
|
||||
|
||||
class ParkCreateView(LoginRequiredMixin, CreateView):
|
||||
model = Park
|
||||
form_class = ParkForm
|
||||
template_name = 'parks/park_form.html'
|
||||
fields = ['name', 'location', 'country', 'description', 'owner', 'status',
|
||||
'opening_date', 'closing_date', 'operating_season', 'size_acres', 'website']
|
||||
|
||||
def form_valid(self, form):
|
||||
# If user is moderator or above, save directly
|
||||
@@ -30,6 +79,12 @@ class ParkCreateView(LoginRequiredMixin, CreateView):
|
||||
# Convert model instances to IDs for JSON serialization
|
||||
if cleaned_data.get('owner'):
|
||||
cleaned_data['owner'] = cleaned_data['owner'].id
|
||||
if cleaned_data.get('country'):
|
||||
cleaned_data['country'] = cleaned_data['country'].id
|
||||
if cleaned_data.get('region'):
|
||||
cleaned_data['region'] = cleaned_data['region'].id
|
||||
if cleaned_data.get('city'):
|
||||
cleaned_data['city'] = cleaned_data['city'].id
|
||||
|
||||
submission = EditSubmission.objects.create(
|
||||
user=self.request.user,
|
||||
@@ -39,41 +94,10 @@ class ParkCreateView(LoginRequiredMixin, CreateView):
|
||||
reason=self.request.POST.get('reason', ''),
|
||||
source=self.request.POST.get('source', '')
|
||||
)
|
||||
return HttpResponseRedirect(reverse('park_list'))
|
||||
return HttpResponseRedirect(reverse('parks:park_list'))
|
||||
|
||||
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)
|
||||
return reverse('parks:park_detail', kwargs={'slug': self.object.slug})
|
||||
|
||||
class ParkDetailView(SlugRedirectMixin, EditSubmissionMixin, PhotoSubmissionMixin, InlineEditMixin, HistoryMixin, DetailView):
|
||||
model = Park
|
||||
@@ -96,7 +120,7 @@ class ParkDetailView(SlugRedirectMixin, EditSubmissionMixin, PhotoSubmissionMixi
|
||||
return context
|
||||
|
||||
def get_redirect_url_pattern(self):
|
||||
return 'park_detail'
|
||||
return 'parks:park_detail'
|
||||
|
||||
class ParkAreaDetailView(SlugRedirectMixin, EditSubmissionMixin, PhotoSubmissionMixin, InlineEditMixin, HistoryMixin, DetailView):
|
||||
model = ParkArea
|
||||
@@ -123,7 +147,7 @@ class ParkAreaDetailView(SlugRedirectMixin, EditSubmissionMixin, PhotoSubmission
|
||||
return context
|
||||
|
||||
def get_redirect_url_pattern(self):
|
||||
return 'park_detail'
|
||||
return 'parks:park_detail'
|
||||
|
||||
def get_redirect_url_kwargs(self):
|
||||
return {
|
||||
@@ -137,7 +161,7 @@ class ParkListView(ListView):
|
||||
context_object_name = 'parks'
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = Park.objects.select_related('owner').prefetch_related('photos', 'rides')
|
||||
queryset = Park.objects.select_related('owner', 'country', 'region', 'city').prefetch_related('photos', 'rides')
|
||||
|
||||
search = self.request.GET.get('search', '').strip() or None
|
||||
location = self.request.GET.get('location', '').strip() or None
|
||||
@@ -146,10 +170,19 @@ class ParkListView(ListView):
|
||||
if search:
|
||||
queryset = queryset.filter(
|
||||
Q(name__icontains=search) |
|
||||
Q(location__icontains=search)
|
||||
Q(location__icontains=search) |
|
||||
Q(country__name__icontains=search) |
|
||||
Q(region__name__icontains=search) |
|
||||
Q(city__name__icontains=search)
|
||||
)
|
||||
if location:
|
||||
queryset = queryset.filter(location=location)
|
||||
# Try to match against the formatted location or any location field
|
||||
queryset = queryset.filter(
|
||||
Q(location__icontains=location) |
|
||||
Q(country__name__icontains=location) |
|
||||
Q(region__name__icontains=location) |
|
||||
Q(city__name__icontains=location)
|
||||
)
|
||||
if status:
|
||||
queryset = queryset.filter(status=status)
|
||||
|
||||
@@ -157,18 +190,11 @@ class ParkListView(ListView):
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
# Get unique locations for filter dropdown
|
||||
context['locations'] = list(Park.objects.values_list('location', flat=True)
|
||||
.distinct().order_by('location'))
|
||||
|
||||
# Add current filter values to context
|
||||
context['current_filters'] = {
|
||||
'search': self.request.GET.get('search', ''),
|
||||
'location': self.request.GET.get('location', ''),
|
||||
'status': self.request.GET.get('status', '')
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
|
||||
Reference in New Issue
Block a user