def add_new_match(request): # method to add new matches context={} if request.method == 'POST': form = AddMatchForm(request.POST) form.is_valid() league_id = form.cleaned_data.get('league') league1 = League.objects.get(id=league_id) match = Match(league=league1,date=datetime.date.today()) match.save() teams = form.cleaned_data.get('teams') for team_id in list(teams): team = Team.objects.get(id=int(team_id)) match.teams.add(team) context={'msg':"new Match Added added, want to add more matches??"} return render(request, 'football/add_match.html',context)
def update_match(): """ Update match data in the database """ all_matches = football_matches() league_ids = [ 2, 46, 47, 49, 36, 48, ] for league_id in league_ids: league_match_list = all_matches[league_id] for match in league_match_list: try: # check if given match already in database match_instance = Match.objects.get(match_id=match['dbid']) # if match result is not in db but is updated in api call then update it in database if match_instance.outcome is None : if match['outcome'] is not None : match_instance.outcome = match['outcome']['winner'] match_instance.home_team_goals = match['homeGoals'] match_instance.away_team_goals = match['awayGoals'] match_instance.save() # Checking bets and assigning points for the match bets = Bets.objects.filter(match_id__match_id=match['dbid']) for bet in bets: points_earned = 0 #for winner prediction if match['homeGoals'] > match['awayGoals']: if bet.winner_prediction == 0: points_earned += 5 else: points_earned -= 5 elif match['homeGoals'] == match['awayGoals']: if bet.winner_prediction == 2: points_earned += 5 else: points_earned -=5 else: if bet.winner_prediction == 1: points_earned += 5 else: points_earned -= 5 #for goal difference prediction if bet.goal_difference == abs(match['homeGoals'] - match['awayGoals']): points_earned += 10 else: points_earned -=5 #for score prediction if (bet.home_goals_prediction == match['homeGoals']) and (bet.away_goals_prediction == match['awayGoals']): points_earned += 25 else: points_earned -= 5 bet.username.points += points_earned bet.username.save() except: if match['outcome'] is not None: winner = match['outcome']['winner'] else: winner = None new_match = Match( match_id = match['dbid'], league = match['league'], home_team = match['homeTeam'], away_team = match['awayTeam'], home_team_goals = match['homeGoals'], away_team_goals = match['awayGoals'], time = match['matchTime'], outcome = winner, ) new_match.save()