Skip to content

Commit

Permalink
feat: filter leagues by year
Browse files Browse the repository at this point in the history
Now the main dropdown only shows leagues for the current year, while a secondary dropdown shows all historical leagues
  • Loading branch information
Desi Pilla committed Nov 15, 2022
1 parent 9615779 commit dff0438
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 41 deletions.
118 changes: 79 additions & 39 deletions fantasy_stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,99 @@
django_luck_index,
django_power_rankings,
django_standings,
django_weekly_stats
django_weekly_stats,
)


# Create your views here.
def index(request):
all_leagues = LeagueInfo.objects.order_by('-league_id', '-league_year')
return HttpResponse(render(request, 'fantasy_stats/index.html', {'all_leagues': all_leagues}))
current_year = (datetime.datetime.today() - datetime.timedelta(weeks=12)).year
leagues_current_year = (
LeagueInfo.objects.filter(league_year=current_year)
.order_by("-league_id", "-league_year")
.distinct("league_id", "league_year")
)
leagues_previous_year = (
LeagueInfo.objects.filter(league_year__lt=current_year)
.order_by("-league_id", "-league_year")
.distinct("league_id", "league_year")
)
return HttpResponse(
render(
request,
"fantasy_stats/index.html",
{
"leagues_current_year": leagues_current_year,
"leagues_previous_year": leagues_previous_year,
},
)
)


def league_input(request):
league_id = request.POST.get('league_id', None)
league_year = int(request.POST.get('league_year', None))
swid = request.POST.get('swid', None)
espn_s2 = request.POST.get('espn_s2', None)
league_id = request.POST.get("league_id", None)
league_year = int(request.POST.get("league_year", None))
swid = request.POST.get("swid", None)
espn_s2 = request.POST.get("espn_s2", None)

try:
print('Checking for League {} ({}) in database...'.format(
league_id, league_year))
print(
"Checking for League {} ({}) in database...".format(league_id, league_year)
)
league_info = LeagueInfo.objects.get(
league_id=league_id, league_year=league_year)
print('League found!')
league_id=league_id, league_year=league_year
)
print("League found!")
except LeagueInfo.DoesNotExist:
print('League {} ({}) NOT FOUND! Fetching league from ESPN...'.format(
league_id, league_year))
print(
"League {} ({}) NOT FOUND! Fetching league from ESPN...".format(
league_id, league_year
)
)
league = fetch_league(league_id, league_year, swid, espn_s2)
league_info = LeagueInfo(league_id=league_id,
league_year=league_year,
swid=swid,
espn_s2=espn_s2,
league_name=league.name)
league_info = LeagueInfo(
league_id=league_id,
league_year=league_year,
swid=swid,
espn_s2=espn_s2,
league_name=league.name,
)
league_info.save()
print('League {} ({}) fetched and saved to the databse.'.format(
league_id, league_year))
print(
"League {} ({}) fetched and saved to the databse.".format(
league_id, league_year
)
)

return redirect('/fantasy_stats/league/{}/{}'.format(league_year, league_id, week=None))
return redirect(
"/fantasy_stats/league/{}/{}".format(league_year, league_id, week=None)
)


def league(request, league_id, league_year, week=None):
league_info = LeagueInfo.objects.get(
league_id=league_id, league_year=league_year)
league = fetch_league(league_info.league_id, league_info.league_year,
league_info.swid, league_info.espn_s2)
league_info = LeagueInfo.objects.get(league_id=league_id, league_year=league_year)
league = fetch_league(
league_info.league_id,
league_info.league_year,
league_info.swid,
league_info.espn_s2,
)

# Set default week to display on page
if week is None:
if datetime.datetime.now().strftime('%A') in ["Tuesday", "Wednesday"]:
if datetime.datetime.now().strftime("%A") in ["Tuesday", "Wednesday"]:
week = league.current_week - 1
else:
week = league.current_week

if week == 0:
box_scores, weekly_awards, power_rankings, luck_index, standings = [], [], [], [], []
box_scores, weekly_awards, power_rankings, luck_index, standings = (
[],
[],
[],
[],
[],
)

else:
box_scores = league.box_scores(week)
Expand All @@ -69,22 +109,22 @@ def league(request, league_id, league_year, week=None):
standings = django_standings(league)

context = {
'league_info': league_info,
'league': league,
'page_week': week,
'box_scores': box_scores,
'weekly_awards': weekly_awards,
'power_rankings': power_rankings,
'luck_index': luck_index,
'standings': standings
"league_info": league_info,
"league": league,
"page_week": week,
"box_scores": box_scores,
"weekly_awards": weekly_awards,
"power_rankings": power_rankings,
"luck_index": luck_index,
"standings": standings,
}
return HttpResponse(render(request, 'fantasy_stats/league.html', context))
return HttpResponse(render(request, "fantasy_stats/league.html", context))


def standings(reqeust):
return HttpResponse('League still exitst')
return HttpResponse("League still exitst")


def all_leagues(request):
leagues = LeagueInfo.objects.order_by('-league_id', '-league_year')
return render(request, 'fantasy_stats/all_leagues.html', {'leagues': leagues})
leagues = LeagueInfo.objects.order_by("-league_id", "-league_year")
return render(request, "fantasy_stats/all_leagues.html", {"leagues": leagues})
17 changes: 15 additions & 2 deletions templates/fantasy_stats/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,30 @@
<h1>Welcome to Dorito Stats!</h1>
<hr>

{# Dropdown list containing all leagues in database #}
{# Dropdown list containing all leagues in database from current year #}
<h2>Select your leage</h2>
<div class="select">
<select name="league-select" id="league-select" onchange="location = this.value">
<option value="/fantasy_stats/"> - Select your league - </option>
{% for league in all_leagues %}
{% for league in leagues_current_year %}
<option value="/fantasy_stats/league/{{ league.league_year}}/{{ league.league_id }}">{{ league.league_id }} - {{ league.league_name}} ({{ league.league_year}})</option>
{% endfor %}
</select>
<span class="focus"></span>
</div>
<br>

{# Dropdown list containing all leagues in database from previous years #}
<div class="unselected-field" style="display: inline-block;" id="selectCountry">
<label>Looking for leagues from previous seasons?
<select name="league-select" id="league-select" onchange="location = this.value">
<option value="/fantasy_stats/"> - Select your historical league - </option>
{% for league in leagues_previous_year %}
<option value="/fantasy_stats/league/{{ league.league_year}}/{{ league.league_id }}">{{ league.league_id }} - {{ league.league_name}} ({{ league.league_year}})</option>
{% endfor %}
</select>
</label>
</div>
<hr>


Expand All @@ -56,6 +68,7 @@ <h3>Don't see your league? Add it manually by entering your league's details bel
<button type="submit">Fetch league</button>
</form>
<hr>

<h3><em>Don't know your SWID or espn_s2? (instructions for Mac / PC)</em></h3>
<p>
<ol>
Expand Down

0 comments on commit dff0438

Please sign in to comment.