def comparison(tid1, tid2):
        #Percent of statistics of all stat for team
        stats1 = TeamRepository().getAvgStats(tid1)
        stats2 = TeamRepository().getAvgStats(tid2)
        comulative_statistics_team1 = dict(
            zip(SOCCER_COMPATISON_STAT_FORMAT, stats1))
        comulative_statistics_team2 = dict(
            zip(SOCCER_COMPATISON_STAT_FORMAT, stats2))

        #Most favored to win
        points = {'t1': 0, 't2': 0}
        for i in range(len(stats1)):
            if stats1[i] > stats2[i]:
                points['t1'] += 1
            else:
                points['t2'] += 1

        most_favored = 'team1' if points['t1'] > points['t2'] else 'team2'

        # TODO: compare using possession, goals scored, shot and past results.
        return {
            'average_stats_team1': comulative_statistics_team1,
            'average_stats_team2': comulative_statistics_team2,
            'most_favored': most_favored
        }
 def edit(self, tid, json):
     if tid and json['team_name'] and json['team_info'] and json[
             'sport_name']:
         new_team = Team(tid, json['team_name'], json['team_info'],
                         json['sport_name'])
         teams = TeamRepository().edit(new_team)
         return jsonify(Teams=[team.serialize() for team in teams]), CREATED
     else:
         return jsonify(Error='Unexpected attributes in post'), BAD_REQUEST
 def getAll(self):
     teams = TeamRepository().getAll()
     validTeams = list()
     for team in teams:
         if SoccerTeamSpecification().isValidTeam(team):
             validTeams.append(team)
         else:
             pass
     return jsonify(Teams=[team.serialize() for team in validTeams]), OK
 def get(self, tid):
     teams = TeamRepository().get(tid)
     validTeams = list()
     # for team in teams:
     # if SoccerTeamSpecification().isValidTeam(team):
     # validTeams.append(team)
     # else:
     # pass
     # return jsonify(Teams=[team.serialize() for team in validTeams]), OK
     return jsonify(Teams=[team.serialize() for team in teams]), OK
 def search(self, args):
     team_name = args.get("keyword")
     sport_name = args.get("sport_name")
     repository = TeamRepository()
     teams = []
     if (len(args) == 2) and team_name and sport_name:
         teams = repository.getByNameAndSport(team_name, sport_name)
     elif (len(args) == 1) and team_name:
         teams = repository.getByName(team_name)
     elif (len(args) == 1) and sport_name:
         teams = repository.getBySport(sport_name)
     else:
         return jsonify(Error='Malformed query string'), NOT_FOUND
     validTeams = list()
     for team in teams:
         if SoccerTeamSpecification().isValidTeam(team):
             validTeams.append(team)
         else:
             pass
     return jsonify(Teams=[team.serialize() for team in validTeams]), OK
 def getMyTeams(self, json):
     if 'username' in json:
         teams = TeamRepository().getByManager(json['username'])
         return jsonify(Teams=[team.serialize() for team in teams]), OK
     else:
         return jsonify(Error='Unexpected attributes in post'), BAD_REQUEST
 def delete(self, tid):
     teams = []
     teams = TeamRepository().delete(tid)
     return jsonify(Teams=[team.serialize() for team in teams]), OK