This repository was archived by the owner on Sep 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
181 lines (148 loc) · 5.01 KB
/
Copy pathapi.py
File metadata and controls
181 lines (148 loc) · 5.01 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from flask_restful import Resource, Api, fields, marshal_with
from fllipit import APP
from models import DB, Team
import pypyodbc
API = Api(APP)
# Get teams from the database
def getTeams():
teams = []
if APP.config['TEST_DB']:
# In test mode, use the sqlite database
teams = Team.query.all()
for team in teams:
team.sortScores()
else:
# In production mode, get the data from the Access database
# Create the database connection
conn = pypyodbc.connect(
r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
r"Dbq=" + APP.config['DB_FILE'] + ";")
cur = conn.cursor()
# Get the data from the database
cur.execute(
'''
SELECT TeamNumber,
TeamName,
Affiliation,
Trial1Score,
Trial2Score,
Trial3Score,
Trial4Score,
Trial5Score,
Trial6Score,
Trial7Score,
ToRound4,
ToRound5,
ToRound6,
ToRound7,
Trial1PenaltyCount,
Trial2PenaltyCount,
Trial3PenaltyCount,
Trial4PenaltyCount,
Trial5PenaltyCount,
playoffsround1score,
playoffsround2score,
playoffsround3score,
playoffsround4score,
playoffsround1penaltycount,
playoffsround2penaltycount,
playoffsround3penaltycount,
playoffsround4penaltycount
FROM ScoringSummaryQuery
''')
# Build the list of Team objects
for row in cur.fetchall():
# Build the team object
team = Team(
number=row[0],
name=row[1],
affiliation=row[2],
round1=row[3],
round2=row[4],
round3=row[5],
round4=row[6],
round5=row[7],
round6=row[8],
round7=row[9],
advanceTo4=row[10]=='Yes',
advanceTo5=row[11]=='Yes',
advanceTo6=row[12]=='Yes',
advanceTo7=row[13]=='Yes',
round1Penalties=row[14],
round2Penalties=row[15],
round3Penalties=row[16],
round4Penalties=row[17],
round5Penalties=row[18],
elim1=row[19],
elim2=row[20],
elim3=row[21],
elim4=row[22],
elim1Penalties=row[23],
elim2Penalties=row[24],
elim3Penalties=row[25],
elim4Penalties=row[26] )
# Add the current team to the list of all teams
teams.append(team)
# Close the database connection
cur.close()
conn.close()
return teams
def rankTeams(teams):
return sorted(
teams,
key=lambda x: (x.bestScore, x.secondBestScore, x.worstScore, -x.bestScorePenalties, -x.secondBestScorePenalties, -x.worstScorePenalties),
reverse=True)
# Setup the fields that will be used in the JSON output
teamFields = {
"number": fields.Integer,
"name": fields.String,
"affiliation": fields.String,
"round1": fields.Integer,
"round2": fields.Integer,
"round3": fields.Integer,
"round4": fields.Integer,
"round5": fields.Integer,
"bestScore": fields.Integer,
"rank": fields.Integer
}
playoffFields = {
"number": fields.Integer,
"name": fields.String,
"score": fields.Integer
}
class Rankings(Resource):
"""Setup a REST resource for the Team data."""
@marshal_with(teamFields)
def get(self):
teams = getTeams()
rankedTeams = rankTeams(teams)
i = 1
for team in rankedTeams:
team.rank = i
i += 1
return rankedTeams
class Playoffs(Resource):
"""Setup a REST resource for the playoff data"""
@marshal_with(playoffFields)
def get(self, roundNumber):
# Get only the teams that are marked to advance to the selected round
teams = [t for t in getTeams() if t.isAdvancingToRound(roundNumber)]
# Add a temporary attribute 'score' to the team objects, for generic REST output
for team in teams:
team.score = team.getRoundScore(roundNumber)
# Return team list sorted by scores from previous round
return sorted(
teams,
key=lambda x: (x.getRoundScore(roundNumber), -x.getRoundPenalties(roundNumber)),
reverse=True)
class TournamentSettings(Resource):
"""Setup a REST resource for the tournament settings"""
def get(self):
rounds = 3
if(APP.config['USE_5_QUAL_ROUNDS']):
rounds = 5
return {'qualifying_rounds': rounds}
# map resource to URL
API.add_resource(Rankings, '/api/teams')
API.add_resource(Playoffs, '/api/playoffs/<int:roundNumber>')
API.add_resource(TournamentSettings, '/api/settings')