Esempio n. 1
0
def user_settings(request):
    team_service = TeamService(request.user)
    user_team = team_service.get_team()

    form = UserSettingsForm()
    form.fields['auto_manager'].initial = user_team.auto_manager

    if request.method == "POST":

        form = UserSettingsForm(request.POST)

        if form.is_valid():
            user_team.auto_manager = form.cleaned_data['auto_manager']
            user_team.save()
            messages.add_message(request, messages.SUCCESS,
                                 'Settings updated.')

        else:
            messages.add_message(request, messages.ERROR, 'Something wrong.')
            logger.debug("something went wrong. form invalid")

    return render(request, 'frontoffice/user_settings.html', {
        'user_team': user_team,
        'form': form
    })
Esempio n. 2
0
def league_roto_projections(request, projections_only=False):
    team_service = TeamService(request.user)
    league = team_service.league

    if projections_only:
        league = team_service.set_roto_league_team_proj_score(league)

        page_title = 'Pure Projections'
        description = """These projections are taken from the ZIPS forecast. 
                        These only show projections from the time 
                        they are uploaded and are NOT adjusted by Wiz Draft. 
                        They are only summed by roster."""
    else:
        # add league points so far plus the projections for remainder of season
        league = team_service.set_roto_league_team_proj_score(
            league, for_remaining_season=True)

        page_title = 'Projections'
        description = """These projections are taken from the ZIPS forecast. 
                        They are pro-rated by the remaining weeks and added to 
                        the current team stats."""

    set_teams_projected_category_points(team_service.league,
                                        league.teams_projections)

    return render(
        request, 'frontoffice/league_roto_stats.html', {
            'teams_stats': league.teams_projections,
            'league': league,
            'page_title': page_title,
            'description': description,
        })
Esempio n. 3
0
def testing_delete_data(request):
    if request.user.is_staff:

        team_service = TeamService(request.user)
        team_service.delete_yahoo_data()
        return JsonResponse({'data': 'success', 'status': 'success'})

    return JsonResponse({'data': 'error', 'status': 'not authorized'})
Esempio n. 4
0
def ajax_add_player(request):

    response = {'data': 'Something went wrong!', 'status': 'error'}
    player_id_from_front_end = request.GET.get('player', None)

    try:
        player_id = int(player_id_from_front_end)
    except ValueError:
        return JsonResponse(response)

    if not isinstance(player_id, int):
        return JsonResponse(response)

    # check if player is in database
    try:
        player = Player.objects.get(pk=player_id)
    except ObjectDoesNotExist:
        return JsonResponse(response)

    if settings.USEREALQUERY:

        team_service = TeamService(request.user)
        manager_profile = team_service.manager_profile
        team = team_service.get_team()

        # Check if player is already on team
        num_roster_entry = RosterEntry.objects.filter(
            team=team.id, player=player.id).count()
        if num_roster_entry >= 1:
            response = {'data': 'Player already on team!', 'status': 'error'}
            return JsonResponse(response)

        # player exists and player not on team already
        # go ahead and add the player
        if team_service.add_player(player, team):

            response = {
                'data': 'Success! ' + player.full_name + ' added!',
                'status': 'Success',
                'player_id': player_id
            }
            messages.add_message(request, messages.SUCCESS,
                                 'Success! ' + player.full_name + ' added!')

            return JsonResponse(response)

    messages.add_message(request, messages.ERROR, 'Something went wrong!')
    return JsonResponse(response)
Esempio n. 5
0
def players_all(request):
    team_service = TeamService(request.user)
    user_team = team_service.get_team()

    if team_service.league.scoring_type == "headpoint":
        players = team_service.get_proj_player_points_by_league(user_team.league)
    else:
        players = Player.objects.all()

    return render(request,
        'frontoffice/league_projections.html',
        {
        'page_title': 'All Players',
        'players': players,
        'league' : user_team.league
        })
Esempio n. 6
0
def ajax_initialize_league(request):
    response = {'data': 'Something went wrong!', 'status': 'error'}
    yahoo_league_id = request.GET.get('yahoo_league_id', None)

    try:
        yahoo_league_id = int(yahoo_league_id)
    except ValueError:
        return JsonResponse(response)

    if not isinstance(yahoo_league_id, int):
        return JsonResponse(response)

    team_service = TeamService(request.user, initial_setup=True)
    team_service.initialize_league_data_from_yahoo(yahoo_league_id)

    return JsonResponse({'data': 'success', 'status': 'success'})
Esempio n. 7
0
def free_agents(request):
    team_service = TeamService(request.user)
    user_team = team_service.get_team()

    if team_service.league.scoring_type == "headpoint":
        players = team_service.get_proj_player_points_for_free_agents(user_team.league)
    else:
        players = team_service.get_free_agents_in_league()

    return render(request,
        'frontoffice/league_projections.html',
        {
        'page_title': 'Free Agents',
        'players':  players,
        'league' : user_team.league
        })
    def setUpTestData(cls):
        # update this with your test league id
        cls.test_league_yahoo_id = 156718
        cls.test_team_id = "1"

        # disable logging output for the setup of data
        logging.disable(logging.CRITICAL)

        cls.username = '******'
        cls.password = '******'
        cls.user = User.objects.create_user(cls.username, '*****@*****.**', cls.password)

        cls.team_service = TeamService(cls.user, initial_setup=True)
        cls.team_service.initialize_league_data_from_yahoo(cls.test_league_yahoo_id)
        cls.team_service = TeamService(cls.user)

        # re-enable logging again during testing
        logging.disable(logging.DEBUG)
Esempio n. 9
0
def ajax_drop_player(request):

    response = {'data': 'Something went wrong!', 'status': 'error'}
    roster_entry_id_from_front_end = request.GET.get('roster', None)

    try:
        roster_entry_id = int(roster_entry_id_from_front_end)
    except ValueError:
        return JsonResponse(response)

    if not isinstance(roster_entry_id, int):
        return JsonResponse(response)

    if settings.USEREALQUERY:
        team_service = TeamService(request.user)
        manager_profile = team_service.manager_profile
        team = team_service.get_team()
        try:
            roster_entry = RosterEntry.objects.get(pk=roster_entry_id)
        except ObjectDoesNotExist:
            return JsonResponse(response)

        # get team and check if roster is on team
        # check if player is benched because you cannot drop non-benched players
        if roster_entry.team != team or roster_entry.at_position != "BN":
            print("in error message")
            return JsonResponse(response)

        # all good to drop player and update db
        player_name = roster_entry.player.full_name
        if team_service.drop_player(roster_entry.player, team):
            response = {
                'data': 'Success! ' + player_name + ' dropped!',
                'status': 'Success',
                'roster_entry_id': roster_entry_id
            }
            messages.add_message(request, messages.SUCCESS,
                                 'Success! ' + player_name + ' dropped!')

            return JsonResponse(response)

    messages.add_message(request, messages.ERROR, 'Something went wrong!')
    return JsonResponse(response)
Esempio n. 10
0
def league_roto_stats(request):
    team_service = TeamService(request.user)
    league = team_service.league

    league_stats = team_service.get_league_stats()

    set_teams_projected_category_points(league, league_stats)

    return render(
        request, 'frontoffice/league_roto_stats.html', {
            'teams_stats':
            league_stats,
            'league':
            league,
            'page_title':
            'League Standings',
            'description':
            "Taken directly from Yahoo. They don't include live stats.",
        })
Esempio n. 11
0
def ajax_update_team_roster(request):
    response = {
        'data':
        'Team Roster Updated too recently. Please wait 5 minutes before updating again.',
        'status': 'error'
    }
    if settings.USEREALQUERY:

        team_service = TeamService(request.user)
        team = team_service.get_team()
        if team.can_update_roster():
            team_service.update_team_roster(team)
            messages.add_message(request, messages.SUCCESS,
                                 'Roster updated successfully.')
            response['data'] = 'Success. Roster Updated.'
            response['status'] = 'success'
        else:
            messages.add_message(request, messages.ERROR,
                                 'Roster updated too recently.')

    return JsonResponse(response)
Esempio n. 12
0
def best_lineup(request):
    team_service = TeamService(request.user)
    user_team = team_service.get_team()
    best_lineup = team_service.get_best_lineup(user_team)

    if team_service.league.scoring_type == "headpoint":
        current_roster = team_service.get_team_roster(user_team,
                                                      by_position=True,
                                                      with_proj_points=True)
        current_roster_total_points = get_total_points_by_roster(
            current_roster)
        best_lineup_total_points = get_total_points_by_roster(best_lineup)
    else:
        current_roster = team_service.get_team_roster(user_team,
                                                      by_position=True)
        best_lineup_total_points = 0
        current_roster_total_points = 0

    return render(
        request, 'frontoffice/best_lineup.html', {
            'user_team': user_team,
            'current_roster': current_roster,
            'current_roster_total_points': current_roster_total_points,
            'best_lineup': best_lineup,
            'best_lineup_total_points': best_lineup_total_points,
        })
Esempio n. 13
0
def index(request):

    #check if user needs to get a verifier code from yahoo
    oauth_helper = OauthGetAuthKeyHelper(request.user.id)
    if oauth_helper.need_verifier_code():
        return redirect('enterVerifierTokenForm')

    team_service = TeamService(request.user)
    manager_profile = team_service.manager_profile
    league = team_service.league

    # no league found, choose league
    if not league:
        messages.add_message(request, messages.ERROR,
                             'League not found. Re-initializing league.')
        return redirect('choose_league')

    user_team = team_service.get_team()
    other_league_teams = league.teams_in_league.exclude(id=user_team.id)

    if league.scoring_type == "headpoint":
        current_roster = team_service.get_team_roster(user_team,
                                                      by_position=True,
                                                      with_proj_points=True)
        user_team.total_projected_points = get_total_points_by_roster(
            current_roster)

        for team in other_league_teams:
            team_roster = team_service.get_team_roster(team,
                                                       by_position=True,
                                                       with_proj_points=True)
            team.total_projected_points = get_total_points_by_roster(
                team_roster)
    else:
        current_roster = team_service.get_team_roster(user_team,
                                                      by_position=True)

    return render(
        request, 'frontoffice/dashboard.html', {
            'team': user_team,
            'current_roster': current_roster,
            'manager_profile': manager_profile,
            'league': league,
            'other_league_teams': other_league_teams,
            'matchup': team_service.get_team_matchup_for_week(user_team),
            'editable_roster': True,
            'league_standings': team_service.get_league_standings(),
        })
Esempio n. 14
0
def team_roster_projections(request):
    team_service = TeamService(request.user)
    user_team = team_service.get_team()
    current_roster = team_service.get_team_roster(user_team,
                                                  by_position=True,
                                                  with_proj_points=True)

    if team_service.league.scoring_type == "headpoint":
        current_roster_total_points = get_total_points_by_roster(
            current_roster)
    else:
        current_roster_total_points = 0

    if team_service.league.scoring_type == "roto":
        user_team.stat_projections = get_team_stat_projections(
            team_service.league, current_roster)

    return render(
        request, 'frontoffice/team_roster_projections.html', {
            'team': user_team,
            'current_roster': current_roster,
            'current_roster_total_points': current_roster_total_points,
        })
Esempio n. 15
0
def ajax_update_league(request):

    response = {
        'data':
        'League updated too recently. Please wait 5 minutes before updating again.',
        'status': 'error'
    }

    if settings.USEREALQUERY:

        team_service = TeamService(request.user)
        manager_profile = team_service.manager_profile
        if team_service.league.can_update_leauge():
            team_service.update_league_rosters(forceUpdate=True)
            messages.add_message(request, messages.SUCCESS,
                                 'League updated successfully.')
            response['data'] = 'Success. League Updated.'
            response['status'] = 'success'
        else:
            messages.add_message(request, messages.ERROR,
                                 'League updated too recently.')

    return JsonResponse(response)
Esempio n. 16
0
def choose_league(request):
    team_service = TeamService(request.user, initial_setup=True)
    leagues = team_service.get_leagues_from_yahoo()

    # if 1 league found, redirect to account linked page
    if len(leagues) == 1:
        return yahoo_account_linked_success(request, leagues['league'])

    elif len(leagues) < 1:
        logger.debug('no leagues found')

    # prompt user for which league
    league_choices = []

    for ld in leagues:
        league_choices.append((str(ld['league'].league_id), ld['league'].name))

    form = ConfirmLeagueForm()
    logger.debug(leagues)
    form.fields['league'].choices = league_choices

    if request.method == "POST":

        form = ConfirmLeagueForm(request.POST)
        form.fields['league'].choices = league_choices

        if form.is_valid():
            league_id = str(form.cleaned_data['league'])

            for ld in leagues:
                if league_id == str(ld['league'].league_id):
                    return yahoo_account_linked_success(request, ld['league'])

            #if got here, error something went wrong.
            logger.debug("Something went wrong. Chosen league not found.")

    return render(request, 'frontoffice/choose_league.html', {'form': form})
Esempio n. 17
0
def current_matchup(request):
    team_service = TeamService(request.user)
    user_team = team_service.get_team()

    current_week = team_service.get_current_week()
    if current_week == 0 or not isinstance(current_week, int):
        current_week = 1

    matchup = team_service.get_team_matchup_for_week(user_team,
                                                     week=current_week)
    opposing_team = matchup.opposing_team

    if team_service.league.scoring_type == "headpoint":
        current_roster = team_service.get_team_roster(user_team,
                                                      by_position=True,
                                                      with_proj_points=True)
        user_team.total_projected_points = get_total_points_by_roster(
            current_roster)
        opposing_team_roster = team_service.get_team_roster(
            opposing_team, by_position=True, with_proj_points=True)
        opposing_team.total_projected_points = get_total_points_by_roster(
            opposing_team_roster)
    else:
        current_roster = team_service.get_team_roster(user_team,
                                                      by_position=True)
        user_team.total_projected_points = 0
        opposing_team_roster = team_service.get_team_roster(opposing_team,
                                                            by_position=True)
        opposing_team.total_projected_points = 0

    return render(
        request, 'frontoffice/matchup.html', {
            'user_team': user_team,
            'user_team_roster': current_roster,
            'opposing_team': opposing_team,
            'opposing_team_roster': opposing_team_roster,
        })