def add_player(): add_id = request.form.get('add', 0) drop_id = request.form.get('drop', 0) if (add_id > 0 and drop_id > 0): add = db.get_by_id(add_id) drop = db.get_by_id(drop_id) cgw = current_gameweek() wgw = waiver_gameweek() waiver = waiver_status(add, cgw['week'], cgw['deadline'], cgw['waiver'], next_gameweek()['waiver']) if waiver['addable']: if waiver['type'] == 'free': add['team'] = current_user.get_name() drop['team'] = '' drop['startingxi'] = 0 db.save_all((add, drop)) add_waiver_claim(current_user.get_id(), current_user.get_name(), cgw['week'], add, drop, 'success') elif waiver['type'] == 'waiver': add_waiver_claim(current_user.get_id(), current_user.get_name(), wgw['week'], add, drop) return redirect(request.args.get('next', url_for('lineup')))
def add_player(): add_id = request.form.get('add', 0) drop_id = request.form.get('drop', 0) if (add_id > 0 and drop_id > 0): add = db.get_by_id(add_id) drop = db.get_by_id(drop_id) cgw = current_gameweek() wgw = waiver_gameweek() waiver = waiver_status(add, cgw['week'], cgw['deadline'], cgw['waiver'], next_gameweek()['waiver']) if waiver['addable']: if waiver['type'] == 'free': add['team'] = current_user.get_name() drop['team'] = '' drop['startingxi'] = 0 db.save_all((add, drop)) add_waiver_claim(current_user.get_id(), current_user.get_name(), cgw['week'], add, drop, 'success') elif waiver['type'] == 'waiver': add_waiver_claim(current_user.get_id(), current_user.get_name(), wgw['week'], add, drop) return redirect(request.args.get('next', url_for('lineup')))
def trade(team1, team2): team1, team2 = db.get_by_id(team1), db.get_by_id(team2) if (not team1) or (not team2): abort(404) if request.method == 'POST': cgw = current_gameweek() players = dict([(unicode(player['_id']), player) for player in db.get('players')]) firstplayer = [players.get(player) for player in request.form.getlist('firstplayer')] secondplayer = [players.get(player) for player in request.form.getlist('secondplayer')] for player in firstplayer: player['team'] = team2['name'] for player in secondplayer: player['team'] = team1['name'] trade = dict( week=cgw['week'], firstplayer=firstplayer, secondplayer=secondplayer, first=team1, second=team2) db.save_all(firstplayer + secondplayer) db.save(trade, 'trades') return redirect(url_for('scoring')) else: return render_template('trade.html', activepage="scoring", team1=team1, team2=team2)
def json_player(): player = db.get_by_id(request.args.get('id', 0)) if player: player = dict(id=player['_id'], text='%s %s' % (player['position'], player['name'])) else: player = dict(id='', text='') return jsonify(dict(players=[player]))
def json_club_players(clubid): query = request.args.get('q', '').lower() club = db.get_by_id(clubid) players = sorted(db.get('players', {'club': club['name'], 'searchname': lambda sn: (query in sn)}), key=sort_player) return jsonify({'players': [dict(id=player['_id'], text='%s %s' % (player['position'], player['name'])) for player in players]})
def json_team_players(teamid): query = request.args.get('q', '') team = db.get_by_id(teamid) players = sorted(db.get('players', {'team': team['name'], 'searchname': lambda sn: (query in sn)}), key=sort_player) return jsonify({'players': [dict(id=player['_id'], text='%s %s' % (player['position'], player['name'])) for player in players]})
def json_player(): player = db.get_by_id(request.args.get('id', 0)) if player: player = dict(id=player['_id'], text='%s %s' % (player['position'], player['name'])) else: player = dict(id='', text='') return jsonify(dict(players=[player]))
def trade(team1, team2): team1, team2 = db.get_by_id(team1), db.get_by_id(team2) if (not team1) or (not team2): abort(404) if request.method == 'POST': cgw = current_gameweek() players = dict([(unicode(player['_id']), player) for player in db.get('players')]) firstplayer = [ players.get(player) for player in request.form.getlist('firstplayer') ] secondplayer = [ players.get(player) for player in request.form.getlist('secondplayer') ] for player in firstplayer: player['team'] = team2['name'] for player in secondplayer: player['team'] = team1['name'] trade = dict(week=cgw['week'], firstplayer=firstplayer, secondplayer=secondplayer, first=team1, second=team2) db.save_all(firstplayer + secondplayer) db.save(trade, 'trades') return redirect(url_for('scoring')) else: return render_template('trade.html', activepage="scoring", team1=team1, team2=team2)
def json_club_players(clubid): query = request.args.get('q', '').lower() club = db.get_by_id(clubid) players = sorted(db.get( 'players', { 'club': club['name'], 'searchname': lambda sn: (query in sn) }), key=sort_player) return jsonify({ 'players': [ dict(id=player['_id'], text='%s %s' % (player['position'], player['name'])) for player in players ] })
def json_team_players(teamid): query = request.args.get('q', '') team = db.get_by_id(teamid) players = sorted(db.get( 'players', { 'team': team['name'], 'searchname': lambda sn: (query in sn) }), key=sort_player) return jsonify({ 'players': [ dict(id=player['_id'], text='%s %s' % (player['position'], player['name'])) for player in players ] })
def transfer_player(): player_id = request.form.get('transferplayer', 0) new_club = request.form.get('newclub', '') if player_id > 0: player = db.get_by_id(player_id) if (player_id == 0) or (not player): flash("Player not found or not specified - please try again") return redirect(url_for('scoring')) elif new_club == '': flash("No club specified - please try again") return redirect(url_for('scoring')) else: if new_club == 'No PL Club': player['club'] = '' else: player['club'] = new_club db.save(player) return redirect(url_for('players', q=player.get('searchname', '')))
def transfer_player(): player_id = request.form.get('transferplayer', 0) new_club = request.form.get('newclub', '') if player_id > 0: player = db.get_by_id(player_id) if (player_id == 0) or (not player): flash("Player not found or not specified - please try again") return redirect(url_for('scoring')) elif new_club == '': flash("No club specified - please try again") return redirect(url_for('scoring')) else: if new_club == 'No PL Club': player['club'] = '' else: player['club'] = new_club db.save(player) return redirect(url_for('players', q=player.get('searchname', '')))
def process_waivers_now(cgw=None): if not cgw: cgw = current_gameweek() teams = get_teams(reverse=True) players = db.get('players') claims = db.get('claims', dict(week=cgw['week'])) # mark all players currently on teams for player in players: if player['team'] != '': player['onteam'] = cgw['week'] # convert player list to dictionary so we can look up by id players = dict([(player['_id'], player) for player in players]) def next_claim(team): try: return sorted([claim for claim in claims if claim['user'] == team['userid'] and claim['status'] == ''], key=lambda claim: claim['priority'])[0] except IndexError: return False # process waiver claims done = 0 seq = 0 while not done: done = 1 # until we find out otherwise! for team in teams: while True: claim = next_claim(team) if claim: done = 0 # update status of target player from master player list: claim['add'] = players[claim['add']['_id']] claim['drop'] = players[claim['drop']['_id']] # update sequence of claim for correct ordering in after-event views claim['order'] = seq seq += 1 # process claim if claim['add']['team'] != '': claim['status'] = 'failure' claim['whynot'] = 'player added by %s' % claim['add']['team'] elif claim['drop']['team'] != team['name']: claim['status'] = 'failure' claim['whynot'] = 'no longer have player to drop' else: claim['status'] = 'success' claim['add']['team'] = team['name'] claim['add']['onteam'] = cgw['week'] claim['drop']['team'] = '' claim['drop']['startingxi'] = 0 # success, exit loop break else: # no more claims for this user, exit loop break # save changes cgw = db.get_by_id(cgw['_id']) cgw['waivers_done'] = True db.save_all(players.values() + teams + claims + [cgw])
def sale_form(id): product = db.get_by_id(db.open_db(db_url), id) return render_template('sale.html', product=product)
def details_by_id(id): product = db.get_by_id(db.open_db(db_url), id) return render_template('details.html', product=product)