-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebapp.py
executable file
·269 lines (205 loc) · 8.64 KB
/
webapp.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python
"""
This is a webapp written in Flask to access the Challonge tools backend,
and make it easier for the average user to use.
"""
import challonge
from datetime import timedelta
from dotenv import load_dotenv
from flask import Flask, render_template, redirect, request, flash, session,\
url_for
from flask_sslify import SSLify
import os
from os.path import dirname, abspath
import re
from requests.exceptions import HTTPError
from create_amateur_bracket import AmateurBracketAlreadyExistsError
from create_amateur_bracket import AmateurBracketRequiredMatchesIncompleteError
from create_amateur_bracket import create_amateur_bracket
import garpr_seeds_challonge
app = Flask(__name__)
sslify = SSLify(app)
app.url_map.strict_slashes = False
# Get SECRET_KEY from .env file in parent directory
parent_dir = dirname(dirname(abspath(__file__)))
load_dotenv(os.path.join(parent_dir, '.env'))
app.secret_key = os.getenv('SECRET_KEY')
@app.before_request
def make_session_persistent():
"""
Make credentials persist after closing the browser, and expire after
7 days.
"""
session.permanent = True
app.permanent_session_lifetime = timedelta(days=7)
def link(text, src=None):
"""
Create links for alerts.
@param text: Link text.
@param src: URL link points to. Set to 'text' if omitted.
@returns: HTML for link.
"""
if src is None:
src = text
return ('<a href="{0}" class="alert-link">{1}</a>'
.format(src, text))
settings_msg = ('Make sure you add your Challonge credentials on the '
'{} page.'.format(link('Settings', '/settings')))
def create_unknown_players_html(unknown_players):
"""Return html of an unordered list to show to the user."""
# TODO(timkovich): I'd like to put this in the template, but I'm not sure
# the best way to do this.
if not unknown_players:
return ''
unknown_text = "GAR PR info not found for the following players:<ul>"
for player in unknown_players:
unknown_text += ("<li>{name}, seeding {seed}</li>".format(**player))
unknown_text += "</ul><hr>"
return unknown_text
def needs_credentials():
"""Checks if Challonge settings have been set."""
return not all(session.get(key) for key in ['username', 'api_key'])
# Make this function accessible from templates
app.jinja_env.globals.update(needs_credentials=needs_credentials)
def valid_tourney_url(name):
if not name:
return False, 'Tournament name is required.'
elif not re.match(r'[\w:/.]+$', name):
return False, 'Invalid tournament name.'
return True, None
@app.route('/', methods=['GET', 'POST'])
def main():
if request.method == 'GET':
if needs_credentials():
flash(settings_msg)
tourney_url = request.args.get('tourney_url', '')
shuffle = request.args.get('shuffle', 'on')
return render_template('index.html',
tourney_url=tourney_url,
shuffle=shuffle)
elif request.method == 'POST':
params = {
'tourney_url': request.form.get('tourney_url'),
'shuffle': request.form.get('shuffle', 'off'),
}
is_valid_name, err = valid_tourney_url(params['tourney_url'])
if not is_valid_name:
flash(err, 'danger')
return redirect(url_for('main', **params))
challonge.set_credentials(session['username'], session['api_key'])
try:
sorted_players, unknown_players = garpr_seeds_challonge.\
seed_tournament(params['tourney_url'],
region=session['region'],
shuffle=params['shuffle'])
except ValueError as e:
flash(str(e), 'warning')
return redirect(url_for('main', **params))
except garpr_seeds_challonge.NoSuchTournamentError as e:
flash(str(e), 'warning')
return redirect(url_for('main', **params))
except HTTPError as e:
app.logger.info(e)
flash('Error accessing Challonge API. Make sure your API key is '
'correct.', 'danger')
return redirect(url_for('main', **params))
try:
garpr_seeds_challonge.update_seeds(params['tourney_url'],
sorted_players)
except HTTPError as e:
app.logger.info(e)
flash("Couldn't access {} with the API, are you sure you have "
"access to this bracket?".format(params['tourney_url']),
'danger')
return redirect(url_for('main', **params))
except challonge.api.ChallongeException as e:
flash(str(e), 'danger')
return redirect(url_for('main', **params))
unknown_html = create_unknown_players_html(unknown_players)
flash(unknown_html + 'Your tournament has been seeded! Check it out '
'{} to make adjustments. Feel free to run '
'this again if you add more players.'
.format(link('here', params['tourney_url'] + '/participants')),
'success')
return redirect(url_for('main', **params))
@app.route('/amateur', methods=['GET', 'POST'])
def amateur():
if request.method == 'GET':
if needs_credentials():
flash(settings_msg)
default_params = {
'tourney_url': '',
'losers_round': 2,
'elimination': 2,
'randomize': False,
'incomplete': False
}
params = {}
for p, default in default_params.items():
params[p] = request.args.get(p, default)
return render_template('amateur.html', **params)
elif request.method == 'POST':
params = {}
for p in ['tourney_url', 'losers_round', 'elimination', 'randomize',
'incomplete']:
params[p] = request.form.get(p)
is_valid_name, err = valid_tourney_url(params['tourney_url'])
if not is_valid_name:
flash(err, 'danger')
return redirect(url_for('amateur', **params))
challonge.set_credentials(session['username'], session['api_key'])
try:
amateur_tourney_url = create_amateur_bracket(
params['tourney_url'],
single_elimination=params['elimination'] == 1,
losers_round_cutoff=int(params['losers_round']),
randomize_seeds=params['randomize'],
incomplete=params['incomplete'])
except AmateurBracketAlreadyExistsError:
flash('Amateur bracket for this tournament already exists.',
'danger')
return redirect(url_for('amateur', **params))
except AmateurBracketRequiredMatchesIncompleteError as e:
flash("The main tournament is not far enough along in the loser's "
"bracket to create amateur bracket yet. "
"There are <b>{}</b> matches remaining."
.format(e.matches_remaining), 'warning')
return redirect(url_for('amateur', **params))
except HTTPError as e:
app.logger.info(e)
status_code = e.response.status_code
if status_code == 404:
flash("Couldn't find tournament: {}"
.format(params['tourney_url']), 'danger')
elif status_code == 401:
flash('Error accessing Challonge API. Make sure your API key '
'is correct.', 'danger')
else:
flask('Something went wrong: {} error.'.format(status_code),
'danger')
return redirect(url_for('amateur', **params))
flash('Your tournament amateur bracket has been created! '
'{}'.format(link(amateur_tourney_url)), 'success')
return redirect(url_for('amateur', **params))
@app.route('/settings', methods=['GET', 'POST'])
def settings():
if request.method == 'GET':
args = {
'username': session.get('username', ''),
'api_key': session.get('api_key', ''),
'region': session.get('region', 'norcal'),
}
return render_template('settings.html', **args)
elif request.method == 'POST':
for value in ['username', 'api_key', 'region']:
session[value] = request.form.get(value)
flash('Credentials saved!', 'success')
return redirect(url_for('settings'))
@app.route('/logout')
def logout():
session.clear()
flash('You are logged out.')
return redirect('settings')
@app.route('/privacy')
def privacy():
return render_template('privacy.html')