-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkpffl.py
137 lines (110 loc) · 3.74 KB
/
kpffl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import re
from math import floor
from statistics import mean
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from database import getDB
from sleeper import getTeams
from sportsdata import getTimeframe
def _getCoachesPoll(teams):
# get current week from Sportsdata API
time = getTimeframe()
# initialize dict of team rankings
ranks = {team["id"]: [] for team in teams}
db = getDB()
votes = db.coaches_polls.find({"week": time["week"], "season": time["season"]})
for vote in votes:
for i, team in enumerate(vote["rankings"]):
ranks[team].append(i + 1)
teamsWithRank = [
{
"id": team["id"],
"name": team["name"],
"owner": team["owner"],
"rank": floor(mean(ranks[team["id"]] or [1])),
"topVotes": ranks[team["id"]].count(1),
}
for team in teams
]
return {
"week": time["week"],
"season": time["season"],
"numVotes": votes.count(),
"teams": sorted(teamsWithRank, key=lambda team: team["rank"]),
}
def _getStandings(teams):
# sorted(student_objects, key=attrgetter('age'))
teamsByPoints = sorted(teams, key=lambda team: team["stats"]["pf"], reverse=True)
teamsByWins = sorted(
teamsByPoints, key=lambda team: team["stats"]["w"], reverse=True
)
return {"teams": teamsByWins}
def getRankings():
"""Gets Rankings."""
# get team information, skipping players
teams = getTeams(True)
return {"cp": _getCoachesPoll(teams), "standings": _getStandings(teams)}
def addCoachesPollVote(votes, userID):
"""Adds one vote to the database."""
# votes should be in the form ["team|3", "team|4", "team|1", etc...]
db = getDB()
time = getTimeframe()
db.coaches_polls.update(
{"user_id": userID, "week": time["week"], "season": time["season"]},
{
"user_id": userID,
"week": time["week"],
"season": time["season"],
"rankings": votes,
},
upsert=True,
)
def sendEmail(body, subject, email=""):
"""Sends email to commisioners."""
dest = ["[email protected]", "[email protected]"]
if re.match(r"\w+@\w+\.\w+", email):
if email not in dest:
dest.append(email)
# TODO create a new proposal in the DB with rc_id = 0
# fill in author, title, why, what, how
# send email to commish with an embedded approve link in the form:
# https://kpffl.com/rc/approve/<ID>
# that link will set the rc_id to the next largest item and make the page live
print(dest, subject, body)
message = Mail(
from_email="[email protected]",
to_emails=dest,
subject=subject,
html_content=body,
)
try:
sg = SendGridAPIClient(os.environ.get("SENDGRID_KEY"))
res = sg.send(message)
except Exception as e:
print(e, res)
def getProposal(rc_id):
"""This gets yes and no votes for a given proposal from the DB."""
db = getDB()
proposal = db.proposals.find_one({"rc_id": rc_id})
if not proposal:
return None
votes = db.proposal_votes.find({"proposal_id": proposal["_id"]})
yes, no = 0, 0
for vote in votes:
if vote["yes_vote"]:
yes += 1
else:
no += 1
proposal["yes"] = yes
proposal["no"] = no
return proposal
def addProposalVote(user_id, rc_id, vote):
"""Adds rule change proposal to the DB."""
db = getDB()
proposal = db.proposals.find_one({"rc_id": rc_id})
db.proposal_votes.update(
{"user_id": user_id, "proposal_id": proposal["_id"]},
{"user_id": user_id, "yes_vote": vote == "yes", "proposal_id": proposal["_id"]},
upsert=True,
)