def change_goalie(request, match_id, team_id): data = json.loads(str(request.body.decode('utf-8'))) match = get_object_or_404(Match, pk=match_id) team_on_tournament = get_object_or_404(TeamOnTournament, pk=team_id) new_goalie = get_object_or_404(Player, pk=data.get('goalie')) time = datetime.datetime.strptime(data.get('time'), "%H:%M:%S") if new_goalie not in match.team_one.players.all() and new_goalie not in match.team_two.players.all(): return HttpResponseBadRequest("Nový brankář se nenachází ani v jednom z týmů.") for goalie in GoalieInMatch.objects.filter(match=match).all(): if goalie.goalie in team_on_tournament.players.all() and goalie.end is None: goalie.end = time if str(goalie.end).endswith(str(goalie.start)): goalie.delete() else: goalie.save() break new_goalie_in_match = GoalieInMatch(goalie=new_goalie, match=match, start=time + datetime.timedelta(seconds=1)) new_goalie_in_match.save() return HttpResponse("OK")
def add_match(request): data = json.loads(str(request.body.decode('utf-8'))) tournament = get_object_or_404(Tournament, pk=data.get('tournament')) team_one = get_object_or_404(TeamOnTournament, pk=data.get('team_one')) team_two = get_object_or_404(TeamOnTournament, pk=data.get('team_two')) referee_team = get_object_or_404(TeamOnTournament, pk=data.get('referee_team')) if data.get('referee'): referee = get_object_or_404(Player, pk=data.get('referee')) else: referee = None if team_one not in tournament.teams.all(): return HttpResponseBadRequest("První tým není zaregistrovaný na turnaji.") if team_two not in tournament.teams.all(): return HttpResponseBadRequest("Druhý tým není zaregistrovaný na turnaji.") if referee_team not in tournament.teams.all(): return HttpResponseBadRequest("Rozhodčí tým není zaregistrovaný na turnaji.") if team_one == team_two or team_one == referee_team or team_two == referee_team: return HttpResponseBadRequest("Některý z vybraných týmů se shoduje.") match = Match(tournament=tournament, team_one=team_one, team_two=team_two, referee=referee, referee_team=referee_team, start=data.get('start'), place=data.get('place'), end=data.get('end')) match.save() if data.get('goalie_one'): goalie_one = Player.objects.get(pk=data.get('goalie_one')) if goalie_one and goalie_one not in team_one.players.all(): return HttpResponseBadRequest("První brankář se nenachází v prvním týmu.") goalie_one_in_match = GoalieInMatch(goalie=goalie_one, match=match, start=datetime.time(0)) goalie_one_in_match.save() elif team_one.default_goalie: goalie_one_in_match = GoalieInMatch(goalie=team_one.default_goalie, match=match, start=datetime.time(0)) goalie_one_in_match.save() if data.get('goalie_two'): goalie_two = Player.objects.get(pk=data.get('goalie_two')) if goalie_two and goalie_two not in team_two.players.all(): return HttpResponseBadRequest("Druhý brankář se nenachází ve druhém týmu.") goalie_two_in_match = GoalieInMatch(goalie=goalie_two, match=match, start=datetime.time(0)) goalie_two_in_match.save() elif team_two.default_goalie: goalie_two_in_match = GoalieInMatch(goalie=team_two.default_goalie, match=match, start=datetime.time(0)) goalie_two_in_match.save() return HttpResponse(match.pk)