Beispiel #1
0
def update_settings():
  user = session['db_user']

  try:
    if user['subreddit'] != request.form['subreddit']:
      stats.record_one('users.change-subreddit')

    user['subreddit'] = request.form['subreddit']
    user['bracket_id'] = int(request.form['bracket_id'])

    bracket_name = espn.get_bracket_name(user['bracket_id'])
    if (app.config.get('ENFORCE_BRACKET_NAMES_MATCH', False) and
        user['username'].strip() != bracket_name.strip()):
      flash('ESPN bracket name must be the same as your reddit username: '******'found "%s", expected "%s"' % (bracket_name, user['username']),
          category = 'error')
      return redirect('/settings')

    user_subreddits = reddit_auth_instance.get_subreddits(
        session['reddit_user']['name'])
    if not [subreddit for subreddit in user_subreddits if user['subreddit'].strip() == subreddit['display_name']]:
      flash('Subreddit "%s" not found. Ensure you are subscribed to the '
          'subreddit and that it is typed correctly.' % user['subreddit'],
          category = 'error')
      return redirect('/settings')

    if user_manager.save(user):
      flash('Settings saved.', category = 'info');

  except ValueError:
    flash('Settings not saved: bracket ID must be an integer (see the '
        'entryID=XXXXX value in your ESPN bracket URL).', category = 'error')

  return redirect('/settings')
Beispiel #2
0
 def get_score(self, bracket_id):
     # TODO: this could fail at any step here.
     try:
         print('Fetching score for', bracket_id)
         page = self.__get_bracket_page(bracket_id)
         score_el = page.find(class_='value points')
         score = score_el.get_text()
         # >1000 scores have commas
         return int(score.replace(',', ''))
     except:
         # Lazy hack. Easy way to notice something is wrong though.
         stats.record_one('espn-update-failure')
         return -1
Beispiel #3
0
def csrf_protect():
  if request.method == 'POST':
    token = session.get('_csrf_token', None)
    if not token or token != request.form.get('_csrf_token'):
      stats.record_one('csrf_failure')
      flask.abort(403)
Beispiel #4
0
def record_request():
  stats.record_one('request')
Beispiel #5
0
def error_500(error):
  stats.record_one('500')
  from traceback import format_exc
  return format_exc(), 500, {'Content-Type': 'text/plain'}