Esempio n. 1
0
def pull_teams(url, player_lookup, sponsor_lookup, league_lookup):
    """
    pull_teams
        Returns a lookup of teams that were pulled
        from the website into local DB
        Parameters:
            url: the url of the main site
        Returns:
            a dictionary lookup
                for the website team id to local team object
                e.g. team_lookup = {1: Team(), etc..}
    """
    _teams = requests.get(url + "/api/teams").json()
    if isinstance(_teams, dict):
        _teams = pull_all_pages(url, _teams)
    team_lookups = {}
    for team in tqdm(_teams, desc="Pulling teams from {}".format(url)):
        temp = Team(color=team['color'],
                    sponsor_id=sponsor_lookup[team['sponsor_id']].id,
                    league_id=league_lookup[team['league_id']].id,
                    year=team['year'])
        # need to add the players from the roster to the team
        players = requests.get(url + "/api/teamroster/" +
                               team['team_id']).json()
        for player in players:
            temp.insert_player(player_lookup[player['player_id']].id,
                               is_player_captain(player, team))
        team_lookups[team['team_id']] = temp
        DB.session.add(temp)
    DB.session.commit()
    return team_lookups
Esempio n. 2
0
    def add_team_functional(self):
        """ Add a team to the database using functions instead of methods"""
        # parse out the parts - background, header, players
        parts = parse_lines(self.lines)
        self.warnings = parts['warnings']

        # extract the background such a league, sponsor and color
        background = extract_background(parts['background'])

        # extract the players using the header as lookup
        lookup = extract_column_indices_lookup(parts['header'])
        players = extract_players(parts["players"], lookup)
        self.warnings = self.warnings + players['warnings']

        # add the players
        player_models = []
        for player_json in players['player_info']:
            try:
                if (player_json['player_id'] is None):
                    # need to create the player
                    player = Player(player_json['name'],
                                    player_json['email'],
                                    gender=player_json["gender"])
                    self.session.add(player)
                    self.session.commit()
                else:
                    email = player_json['email']
                    player = Player.query.filter(
                        func.lower(Player.email) == func.lower(email)).first()
                player_models.append(player.json())
            except Exception as error:
                player_info = "-".join(
                    [player_json["name"], player_json["email"]])
                self.warnings.append(
                    INVALID_PLAYER.format(player_info, str(error)))

        # get the team, create if does not exist
        if background['team']['team_id'] is None:
            team = Team(color=background['team']['color'],
                        sponsor_id=background['sponsor']['sponsor_id'],
                        league_id=background['league']['league_id'],
                        year=date.today().year)
            self.session.add(team)
        else:

            # get the team and remove all players
            team = Team.query.get(background['team']['team_id'])
            team.players = []
        set_captain = False
        for player in player_models:
            if (player["player_name"].lower() == background["captain"]
                ["player_name"].lower()):
                set_captain = True
                team.insert_player(player["player_id"], captain=True)
            else:
                team.insert_player(player["player_id"], captain=False)
        if not set_captain:
            self.warnings.append(CAPTAIN_NOT_ASSIGNED)
        self.session.commit()