def post(self): """ POST request for Espys List Route: Routes['team'] Parameters : league_id: the league's id (int) sponsor_id: the sponsor's id (int) color: the color of the team (string) year: the year the team is playing in (int) espys: the team espys points (int) Returns: if successful status: 200 mimetype: application/json data: the create team id (int) possible errors status: 400, IFSC, LDNESC, PDNESC or SDNESC mimetype: application/json data: the create team id (int) """ # create a new user args = post_parser.parse_args() description = None sponsor_id = None team_id = None points = None receipt = None date = None time = None if args['description']: description = args['description'] if args['sponsor_id']: sponsor_id = args['sponsor_id'] if args['team_id']: team_id = args['team_id'] if args['points']: points = args['points'] if args['receipt']: points = args['receipt'] if args['date'] and args['time']: date = args['date'] time = args['time'] espy = Espys(sponsor_id=sponsor_id, team_id=team_id, description=description, points=points, receipt=receipt, date=date, time=time) DB.session.add(espy) DB.session.commit() result = espy.id handle_table_change(Tables.ESPYS, item=espy.json()) return Response(dumps(result), status=201, mimetype="application/json")
def pull_espys(url, team_lookup, sponsor_lookup): """ pull_espys Returns a lookup of espys that were pulled from the website into local DB Parameters: url: the url of the main site Returns: a dictionary lookup for the website espy id to local espy object e.g. bat_lookup = {1: Espys(), etc..} """ _espys = requests.get(url + "/api/espys").json() if isinstance(_espys, dict): _espys = pull_all_pages(url, _espys) espy_lookup = {} for espy in tqdm(_espys, desc="Pulling espys from {}".format(url)): temp = Espys(team_lookup[espy['team_id']].id, sponsor_lookup[espy['sponsor_id']].id, espy['description'], espy['points'], espy['receipt'], espy['time'], espy['date']) espy_lookup[espy['espy_id']] = temp DB.session.add(temp) DB.session.commit() return espy_lookup
def post(self): """ POST request for submitting a transaction Route: Route['bottransaction'] Parameters: player_id: the id of the player submitting the receipt (int) amount: the amount spent (int) sponsor: the name of the sponsor (str) team_id: the id of the team (int) Returns: status: 200 mimetype: application/json data: True """ args = parser.parse_args() player_id = args['player_id'] team_id = args['team_id'] amount = args['amount'] sponsor_name = args['sponsor'] # ensure the player exists player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) # ensure the team exist team = Team.query.get(team_id) if team is None: raise TeamDoesNotExist(payload={'details': team_id}) # ensure the sponsor exists sponsor = Sponsor.query.filter_by(name=sponsor_name).first() if sponsor is None: raise SponsorDoesNotExist(payload={'details': sponsor_name}) # player can only submit receipts to their own team if not team.is_player_on_team(player): raise PlayerNotOnTeam(payload={'details': player_id}) # should be good to add the espy now espy = Espys(team_id=team.id, sponsor_id=sponsor.id, points=amount, description="Bot transaction") DB.session.add(espy) DB.session.commit() handle_table_change(Tables.ESPYS) return Response(dumps(espy.id), status=200, mimetype="application/json")
def post(self): """ POST request for submitting a transaction Route: Route['kiktransaction'] Parameters: kik: the kik user name (str) amount: the amount spent (str) sponsor: the name of the sponsor (str) Returns: status: 200 mimetype: application/json data: True """ args = parser.parse_args() kik = args['kik'] amount = args['amount'] sponsor_name = args['sponsor'] player = Player.query.filter_by(kik=kik).first() if player is None: # player is found raise PlayerNotSubscribed(payload={'details': kik}) sponsor = Sponsor.query.filter_by(name=sponsor_name).first() if sponsor is None: # sponsor is not found raise SponsorDoesNotExist(payload={'details': sponsor_name}) teams = find_team_subscribed(kik) if len(teams) == 0: # kik user is not subscribed to any teams raise PlayerNotSubscribed(payload={'details': kik}) # always give points to team first subscribed to (of current year) espy = Espys(team_id=teams[0], sponsor_id=sponsor.id, points=amount, description="Kik transaction") DB.session.add(espy) DB.session.commit() return Response(dumps(espy.id), status=200, mimetype="application/json")
def mock_teams_games(league, sponsor_lookup): """ mock_team_games Mocks up some data for the league by add some players to a team, then a few games to the league and a few scores for some games Parameters: league: the league object sponsor_lookup: a dictionary lookup for a id to its given sponsor Returns: None """ # add some players domain = "@" + league.name.replace(" ", "") team_one_players = [ Player("Captain1", "captain1" + domain, gender="M"), Player("MalePlayer1", "mp1" + domain, gender="M"), Player("FemalePlayer1", "fp1" + domain, gender="F") ] team_two_players = [ Player("Captain2", "captain2" + domain, gender="F"), Player("MalePlayer2", "mp2" + domain, gender="M"), Player("FemalePlayer2", "fp2" + domain, gender="F") ] team_three_players = [ Player("Captain3", "captain3" + domain, gender="M"), Player("MalePlayer3", "mp3" + domain, gender="M"), Player("FemalePlayer3", "fp3" + domain, gender="F") ] team_four_players = [ Player("Captain4", "captain4" + domain, gender="F"), Player("MalePlayer4", "mp4" + domain, gender="M"), Player("FemalePlayer4", "fp4" + domain, gender="F") ] team_players = [ team_one_players, team_two_players, team_three_players, team_four_players ] for team in tqdm(team_players, desc="Adding mock Players"): for player in team: DB.session.add(player) DB.session.commit() # add four teams with some players teams = [ Team(color="Black", sponsor_id=random_value_lookup(sponsor_lookup).id, league_id=league.id), Team(color="Blue", sponsor_id=random_value_lookup(sponsor_lookup).id, league_id=league.id), Team(color="Red", sponsor_id=random_value_lookup(sponsor_lookup).id, league_id=league.id), Team(color="Green", sponsor_id=random_value_lookup(sponsor_lookup).id, league_id=league.id) ] for i in tqdm(range(0, len(teams)), desc="Adding mock players to Teams"): team = teams[i] DB.session.add(team) # add the players to the team for player in team_players[i]: team.insert_player(player.id, "captain" in player.name.lower()) DB.session.commit() # add some random espsy to each team and # create a lookup for team id to players team_player_lookup = {} random_prices = [9.99, 4.75, 100, 15.50, 12.99] for i in tqdm(range(0, len(teams)), desc="Adding mock espys to Teams"): team_player_lookup[team.id] = team_players[i] team = teams[i] for __ in range(0, 4): points = random_value_list(random_prices) espy = Espys(team.id, sponsor_id=random_value_lookup(sponsor_lookup).id, description="Purchase", points=points) DB.session.add(espy) DB.session.commit() # add some games between the teams # need a bunch of games today = datetime.date.today() games = [] for day in range(-3, 3): date_string = (today + datetime.timedelta(days=day)).strftime("%Y-%m-%d") status = "Completed" if day < 0 else "To Be Played" games.append( Game(date_string, "10:00", teams[0].id, teams[1].id, league.id, status=status, field="WP1")) games.append( Game(date_string, "10:00", teams[2].id, teams[3].id, league.id, status=status, field="WP2")) games.append( Game(date_string, "11:00", teams[0].id, teams[2].id, league.id, status=status, field="WP1")) games.append( Game(date_string, "11:00", teams[2].id, teams[1].id, league.id, status=status, field="WP2")) games.append( Game(date_string, "12:00", teams[0].id, teams[3].id, league.id, status=status, field="WP1")) games.append( Game(date_string, "12:00", teams[2].id, teams[1].id, league.id, status=status, field="WP2")) for game in tqdm(games, "Adding mock games"): DB.session.add(game) DB.session.commit() # now add a random score to the game for game in tqdm(games[:18], desc="Mocking scores for games"): add_random_score(game.id, game.away_team_id, team_player_lookup[game.away_team_id]) add_random_score(game.id, game.home_team_id, team_player_lookup[game.home_team_id]) DB.session.commit()