generated from bisco/codex-bootstrap
feat: add booking REST API
This commit is contained in:
65
backend/shows/views.py
Normal file
65
backend/shows/views.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils import timezone
|
||||
from django.utils.dateparse import parse_datetime
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
|
||||
from .models import Performance, Show
|
||||
from .serializers import (
|
||||
PublicPerformanceDetailSerializer,
|
||||
PublicPerformanceListSerializer,
|
||||
PublicShowDetailSerializer,
|
||||
PublicShowListSerializer,
|
||||
)
|
||||
|
||||
|
||||
def public_performance_queryset():
|
||||
return Performance.objects.select_related("show", "venue").filter(
|
||||
show__is_published=True,
|
||||
starts_at__gte=timezone.now(),
|
||||
)
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
def show_list(request):
|
||||
shows = Show.objects.filter(is_published=True).order_by("title")
|
||||
serializer = PublicShowListSerializer(shows, many=True)
|
||||
return Response({"results": serializer.data})
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
def show_detail(request, slug):
|
||||
show = get_object_or_404(Show, slug=slug, is_published=True)
|
||||
show.public_performances = public_performance_queryset().filter(show=show)
|
||||
serializer = PublicShowDetailSerializer(show)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
def performance_list(request):
|
||||
performances = public_performance_queryset()
|
||||
|
||||
show_slug = request.query_params.get("show")
|
||||
if show_slug:
|
||||
performances = performances.filter(show__slug=show_slug)
|
||||
|
||||
from_value = request.query_params.get("from")
|
||||
if from_value:
|
||||
starts_from = parse_datetime(from_value)
|
||||
if starts_from is None:
|
||||
return Response(
|
||||
{"from": ["Enter a valid ISO 8601 date/time."]},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
performances = performances.filter(starts_at__gte=starts_from)
|
||||
|
||||
serializer = PublicPerformanceListSerializer(performances.order_by("starts_at"), many=True)
|
||||
return Response({"results": serializer.data})
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
def performance_detail(request, pk):
|
||||
performance = get_object_or_404(public_performance_queryset(), pk=pk)
|
||||
serializer = PublicPerformanceDetailSerializer(performance)
|
||||
return Response(serializer.data)
|
||||
Reference in New Issue
Block a user