from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse from .serializers import ClubSerializer, TournamentSerializer, UserSerializer, ChangePasswordSerializer, EventSerializer, RoundSerializer, GroupStageSerializer, MatchSerializer, TeamScoreSerializer, TeamRegistrationSerializer, PlayerRegistrationSerializer from .models import Club, Tournament, CustomUser, Event, Round, GroupStage, Match, TeamScore, TeamRegistration, PlayerRegistration from .models import TeamCall from rest_framework import viewsets, permissions from rest_framework.authtoken.models import Token from rest_framework.response import Response from rest_framework.decorators import api_view from rest_framework import status from rest_framework.generics import UpdateAPIView from django.template import loader from datetime import date from django.http import JsonResponse def index(request): today = date.today() future_tournaments = Tournament.objects.filter(end_date__isnull=True, start_date__gt=today).order_by('start_date') live_tournaments = Tournament.objects.filter(end_date__isnull=True, start_date__lte=today).order_by('start_date') ended_tournaments = Tournament.objects.filter(end_date__isnull=False).order_by('start_date') # template = loader.get_template('tournaments/tournaments.html') # context = { # 'future': future_tournaments, # 'live': live_tournaments, # 'ended': ended_tournaments, # } # return HttpResponse(template.render(context, request)) return render( request, "tournaments/tournaments.html", { 'future': future_tournaments, 'live': live_tournaments, 'ended': ended_tournaments, }, content_type="text/html", ) def tournament(request, tournament_id): tournament = get_object_or_404(Tournament, pk=tournament_id) today = date.today() future_matches = Match.objects.filter(end_date__isnull=True, start_date__gt=today).order_by('start_date') live_matches = Match.objects.filter(end_date__isnull=True, start_date__lte=today).order_by('start_date') ended_matches = Match.objects.filter(end_date__isnull=False).order_by('start_date') template = loader.get_template('tournaments/tournament.html') context = { 'future': future_matches, 'live': live_matches, 'ended': ended_matches, } return HttpResponse(template.render(context, request)) def tournament_planning(request, tournament_id): tournament = get_object_or_404(Tournament, pk=tournament_id) team_calls = tournament.team_calls() template = loader.get_template('tournaments/planning.html') context = { 'team_calls': team_calls, } return HttpResponse(template.render(context, request)) def tournament_json(request, tournament_id): tournament = get_object_or_404(Tournament, pk=tournament_id) live_matches = tournament.live_matches() return JsonResponse(live_matches.__dict__) # 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)) @api_view(['GET']) def user_by_token(request): # return Response({"message": "Hello for today! See you tomorrow!"}) # key = request.data['token'] # token = Token.objects.get(key=key) # user = CustomUser.objects.get(username=token.user) serializer = UserSerializer(request.user) return Response(serializer.data, status=status.HTTP_200_OK) class UserViewSet(viewsets.ModelViewSet): queryset = CustomUser.objects.all() serializer_class = UserSerializer class ClubViewSet(viewsets.ModelViewSet): queryset = Club.objects.all() serializer_class = ClubSerializer class TournamentViewSet(viewsets.ModelViewSet): queryset = Tournament.objects.all() serializer_class = TournamentSerializer # class ExpandedTournamentViewSet(viewsets.ModelViewSet): # queryset = Tournament.objects.all() # serializer_class = ExpandedTournamentSerializer class ChangePasswordView(UpdateAPIView): serializer_class = ChangePasswordSerializer def update(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.save() # if using drf authtoken, create a new token if hasattr(user, 'auth_token'): user.auth_token.delete() token, created = Token.objects.get_or_create(user=user) # return new token return Response({'token': token.key}, status=status.HTTP_200_OK) class EventViewSet(viewsets.ModelViewSet): queryset = Event.objects.all() serializer_class = EventSerializer class RoundViewSet(viewsets.ModelViewSet): queryset = Round.objects.all() serializer_class = RoundSerializer class GroupStageViewSet(viewsets.ModelViewSet): queryset = GroupStage.objects.all() serializer_class = GroupStageSerializer class MatchViewSet(viewsets.ModelViewSet): queryset = Match.objects.all() serializer_class = MatchSerializer class TeamScoreViewSet(viewsets.ModelViewSet): queryset = TeamScore.objects.all() serializer_class = TeamScoreSerializer class TeamRegistrationViewSet(viewsets.ModelViewSet): queryset = TeamRegistration.objects.all() serializer_class = TeamRegistrationSerializer class PlayerRegistrationViewSet(viewsets.ModelViewSet): queryset = PlayerRegistration.objects.all() serializer_class = PlayerRegistrationSerializer