from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse from django.template import loader from django.contrib.auth.models import User from .models import Match, Club, Tournament from .serializers import UserSerializer, MatchSerializer, ClubSerializer, TournamentSerializer from rest_framework import viewsets from rest_framework import permissions def index(request): club = Club.objects.first() live_matches = Match.objects.filter(enddate__isnull=True).order_by('court') ended_matches = Match.objects.filter(enddate__isnull=False).order_by('court') template = loader.get_template('scores/index.html') context = { 'club': club, 'live_matches': live_matches, 'ended_matches': ended_matches, } return HttpResponse(template.render(context, request)) def club(request, club_id): club = get_object_or_404(Club, pk=club_id) template = loader.get_template('scores/club.html') context = { 'club': club, } return HttpResponse(template.render(context, request)) def club_name(request, club_name): club = get_object_or_404(Club, name__iexact=club_name.lower()) tournaments = Tournament.objects.filter(club=club.id) template = loader.get_template('scores/club.html') context = { 'club': club, 'tournaments': tournaments, } return HttpResponse(template.render(context, request)) def tournament_club(request, club_name, tournament_id): return tournament(request, tournament_id) def tournament(request, tournament_id): tournament = get_object_or_404(Tournament, pk=tournament_id) live_matches = Match.objects.filter(tournament=tournament.id, enddate__isnull=True).order_by('court') ended_matches = Match.objects.filter(tournament=tournament.id, enddate__isnull=False).order_by('court') template = loader.get_template('scores/tournament.html') context = { 'tournament': tournament, 'live_matches': live_matches, 'ended_matches': ended_matches, } return HttpResponse(template.render(context, request)) def match(request, match_id): match = get_object_or_404(Match, pk=match_id) template = loader.get_template('scores/match.html') context = { 'match': match, } return HttpResponse(template.render(context, request)) class UserViewSet(viewsets.ModelViewSet): """ API endpoint that allows users to be viewed or edited. """ queryset = User.objects.all().order_by('-date_joined') serializer_class = UserSerializer permission_classes = [permissions.IsAuthenticated] class ClubViewSet(viewsets.ModelViewSet): """ API endpoint that allows clubs to be viewed or edited. """ queryset = Club.objects.all().order_by('id') serializer_class = ClubSerializer permission_classes = [permissions.IsAuthenticated] class TournamentViewSet(viewsets.ModelViewSet): """ API endpoint that allows clubs to be viewed or edited. """ queryset = Tournament.objects.all().order_by('id') serializer_class = TournamentSerializer permission_classes = [permissions.IsAuthenticated] class MatchViewSet(viewsets.ModelViewSet): """ API endpoint that allows matches to be viewed or edited. """ queryset = Match.objects.all().order_by('court') serializer_class = MatchSerializer permission_classes = [permissions.IsAuthenticated]