Skip to content

Commit

Permalink
Merge pull request #23 from DesiPilla/migrate-to-railway
Browse files Browse the repository at this point in the history
Migrate to railway
  • Loading branch information
DesiPilla authored Nov 15, 2022
2 parents 6d8aeda + dff0438 commit 8f8f87d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web gunicorn doritostats.wsgi:application --log-file -
web: gunicorn doritostats.wsgi:application --log-file -
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})
12 changes: 12 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Django>=3.2.7
dj-database-url>=0.5.0
django-on-heroku>=1.1.2
django-environ>=0.8.1
python-dotenv>=0.21.0
espn-api>=0.17.0
gunicorn>=20.1.0
joblib>=1.2.0
numpy>=1.17.5
pandas>=0.25.3
psycopg2-binary>=2.9.1
tabulate>=0.8.9
23 changes: 18 additions & 5 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 @@ -46,16 +58,17 @@ <h3>Don't see your league? Add it manually by entering your league's details bel
<form method="POST" action="{% url 'fantasy_stats:league_input' %}">
{% csrf_token %}
<label>League ID: </label><br>
<input type="text" name="league_id", value=1086064><br>
<input type="text" name="league_id"><br>
<label>League year: </label><br>
<input type="number" min="2017" name="league_year", value=2022><br>
<label>SWID: </label> <br>
<input type="password" name="swid", value='55B70875-0A4B-428F-B708-750A4BB28FA1'><br>
<input type="password" name="swid", value=''><br>
<label>ESPN s2: </label><br>
<input type="password" name="espn_s2", value='AEApIqrIV9DE%2F6ZyCAHsoG7FnThiXorn2ejnLGJol8qqRUDI6IVkY11AZLoYlNgj6uoA1M3aSYjcJ2%2F1NiBBzvyI4GzanP%2BCmvF0DhcYSDxaGFzBwYKhzXxkHGd2a9rAC%2Bz3A%2FT3EgEIg8%2FqiUEGDiUhtrRpwQBtDHzelXXBjyCbjH2kSNYBrhON5pZPcvNbJhwRZXr%2F%2Fd1tVQQ7U4FnltTB7VGFPcf7TwT4RYuylGyHixEQAdtzb4YAcbXJOJKNBJF0%2BJgLg2mAb4F4REaXbyX8'><br><br>
<input type="password" name="espn_s2", value=''><br><br>
<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
6 changes: 3 additions & 3 deletions templates/fantasy_stats/league.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="{% static 'fantasy_stats/css/league.css' %}">
<link rel="shortcut icon" type="image/png" href="{% static 'fantasy_stats/img/favicon.png' %}" />
<title>Dorito Stats | League Stats</title>
<title>Dorito Stats | {{league_info.league_name}}</title>

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-45P4BQ6XJR"></script>
Expand All @@ -26,7 +26,6 @@ <h1>Welcome to Dorito Stats!</h1>
<h2>{{league_info.league_name}}</h2>
<p>League ID: {{league_info.league_id}}</p>
<p>League year: {{league_info.league_year}}</p>
{% comment %} <p>Week: {{page_week}}</p> {% endcomment %}

{% comment %} Select a different week {% endcomment %}
<div class="select">
Expand All @@ -52,6 +51,8 @@ <h2>{{league_info.league_name}}</h2>
</select>
<span class="focus"></span>
</div>
<br>
<a href="/fantasy_stats/">Return to home page</a>
<hr>


Expand Down Expand Up @@ -195,4 +196,3 @@ <h2>Standings</h2>

</body>
</html>

0 comments on commit 8f8f87d

Please sign in to comment.