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()
def import_headers(self): done = False i = 0 sponsor = None color = None captain = None league = None while not done: # read the headers line = self.lines[i] columns = line.split(",") if self.clean_cell(columns[0]) == "sponsor": sponsor = columns[1].strip() elif self.clean_cell(columns[0]) == "color": color = columns[1].strip() elif self.clean_cell(columns[0]) == "captain": captain = columns[1].strip() elif self.clean_cell(columns[0]) == "league": league = columns[1].strip() else: # assuming done reading the headers done = True i += 1 player_index = i if sponsor is None: raise InvalidField(payload={"details": "No sponsor was given"}) if captain is None: raise InvalidField(payload={"details": "No captain was given"}) if color is None: raise InvalidField(payload={"details": "No color was given"}) if league is None: raise InvalidField(payload={"details": "No league was given"}) # check no examples were left and actually real info if (league.lower().startswith("ex.") or sponsor.lower().startswith("ex.") or color.lower().startswith("ex.") or captain.lower().startswith("ex.")): raise InvalidField(payload={"details": "The header's still had an example"}) sponsor_id = (DB.session.query(Sponsor).filter(Sponsor.name==sponsor) .first()) if sponsor_id is None: sponsor_id = (DB.session.query(Sponsor).filter(Sponsor.nickname==sponsor) .first()) if sponsor_id is None: # what kind of sponsor are you giving raise SponsorDoesNotExist(payload={'details': "The sponsor does not exist (check case)"}) sponsor_id = sponsor_id.id league_id = (DB.session.query(League).filter(League.name==league)).first() if league_id is None: raise LeagueDoesNotExist(payload={"details": "The league does not exist (check case)"}) league_id = league_id.id # check to see if team was already created teams = (DB.session.query(Team) .filter(Team.color==color) .filter(Team.sponsor_id==sponsor_id) .filter(Team.year==date.today().year).all()) team_found = None if len(teams) > 0: team_found = teams[0] # was the team not found then should create it if team_found is None: self.warnings.append("Team was created") team_found = Team(color=color, sponsor_id=sponsor_id, league_id=league_id) else: team_found.players = [] # remove all the players before self.team = team_found # set the team self.captain_name = captain # remember captains name return player_index
def import_headers(self): """Parse the headers of the csv and add team""" done = False i = 0 sponsor = None color = None captain = None league = None while not done: # read the headers line = self.lines[i] columns = line.split(",") if self.clean_cell(columns[0]) == "sponsor": sponsor = columns[1].strip() elif self.clean_cell(columns[0]) == "color": color = columns[1].strip() elif self.clean_cell(columns[0]) == "captain": captain = columns[1].strip() elif self.clean_cell(columns[0]) == "league": league = columns[1].strip() else: # assuming done reading the headers done = True i += 1 player_index = i if sponsor is None: raise InvalidField(payload={"details": "No sponsor was given"}) if captain is None: raise InvalidField(payload={"details": "No captain was given"}) if color is None: raise InvalidField(payload={"details": "No color was given"}) if league is None: raise InvalidField(payload={"details": "No league was given"}) # check no examples were left and actually real info if (league.lower().startswith("ex.") or sponsor.lower().startswith("ex.") or color.lower().startswith("ex.") or captain.lower().startswith("ex.")): t = "The header's still had an example" raise InvalidField(payload={"details": t}) sponsor_id = (DB.session.query(Sponsor).filter( Sponsor.name == sponsor).first()) if sponsor_id is None: sponsor_id = (DB.session.query(Sponsor).filter( Sponsor.nickname == sponsor).first()) if sponsor_id is None: # what kind of sponsor are you giving t = "The sponsor does not exist (check case)" raise SponsorDoesNotExist(payload={'details': t}) sponsor_id = sponsor_id.id league_id = (DB.session.query(League).filter( League.name == league)).first() if league_id is None: t = "The league does not exist (check case)" raise LeagueDoesNotExist(payload={"details": t}) league_id = league_id.id # check to see if team was already created teams = (DB.session.query(Team).filter(Team.color == color).filter( Team.sponsor_id == sponsor_id).filter( Team.year == date.today().year).all()) team_found = None if len(teams) > 0: team_found = teams[0] # was the team not found then should create it if team_found is None: self.warnings.append("Team was created") team_found = Team(color=color, sponsor_id=sponsor_id, league_id=league_id) else: team_found.players = [] # remove all the players before self.team = team_found # set the team self.captain_name = captain # remember captains name return player_index