mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 06:51:08 -05:00
first commit
This commit is contained in:
91
parks/views.py
Normal file
91
parks/views.py
Normal file
@@ -0,0 +1,91 @@
|
||||
from django.views.generic import DetailView, ListView
|
||||
from django.shortcuts import get_object_or_404
|
||||
from .models import Park, ParkArea
|
||||
from rides.models import Ride
|
||||
from core.views import SlugRedirectMixin
|
||||
|
||||
class ParkDetailView(SlugRedirectMixin, DetailView):
|
||||
model = Park
|
||||
template_name = 'parks/park_detail.html'
|
||||
context_object_name = 'park'
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
if queryset is None:
|
||||
queryset = self.get_queryset()
|
||||
slug = self.kwargs.get(self.slug_url_kwarg)
|
||||
# Try to get by current or historical slug
|
||||
return self.model.get_by_slug(slug)[0]
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['rides'] = Ride.objects.filter(
|
||||
park=self.object
|
||||
).select_related('coaster_stats')
|
||||
context['areas'] = ParkArea.objects.filter(park=self.object)
|
||||
return context
|
||||
|
||||
def get_redirect_url_pattern(self):
|
||||
return 'park_detail'
|
||||
|
||||
class ParkAreaDetailView(SlugRedirectMixin, DetailView):
|
||||
model = ParkArea
|
||||
template_name = 'parks/area_detail.html'
|
||||
context_object_name = 'area'
|
||||
slug_url_kwarg = 'area_slug'
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
if queryset is None:
|
||||
queryset = self.get_queryset()
|
||||
park_slug = self.kwargs.get('park_slug')
|
||||
area_slug = self.kwargs.get('area_slug')
|
||||
# Try to get by current or historical slug
|
||||
obj, is_old_slug = self.model.get_by_slug(area_slug)
|
||||
if obj.park.slug != park_slug:
|
||||
raise self.model.DoesNotExist("Park slug doesn't match")
|
||||
return obj
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['rides'] = Ride.objects.filter(
|
||||
area=self.object
|
||||
).select_related('coaster_stats')
|
||||
return context
|
||||
|
||||
def get_redirect_url_pattern(self):
|
||||
return 'area_detail'
|
||||
|
||||
def get_redirect_url_kwargs(self):
|
||||
return {
|
||||
'park_slug': self.object.park.slug,
|
||||
'area_slug': self.object.slug
|
||||
}
|
||||
|
||||
class ParkListView(ListView):
|
||||
model = Park
|
||||
template_name = 'parks/park_list.html'
|
||||
context_object_name = 'parks'
|
||||
paginate_by = 12
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = Park.objects.select_related('owner')
|
||||
|
||||
# Filter by location if specified
|
||||
location = self.request.GET.get('location')
|
||||
if location:
|
||||
queryset = queryset.filter(location__icontains=location)
|
||||
|
||||
# Filter by status if specified
|
||||
status = self.request.GET.get('status')
|
||||
if status:
|
||||
queryset = queryset.filter(status=status)
|
||||
|
||||
return queryset.order_by('name')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
# Add list of locations for the filter dropdown
|
||||
context['locations'] = Park.objects.values_list(
|
||||
'location', flat=True
|
||||
).distinct().order_by('location')
|
||||
context['selected_location'] = self.request.GET.get('location', '')
|
||||
return context
|
||||
Reference in New Issue
Block a user