def update_challenge(handle): """Fetch Leaderboard and active golfers of the specified challenge, and update datastore.""" logging.info('update_challenge(%s)' % handle) soup = BeautifulSoup(fetch('challenges/' + handle)) title = soup.findAll('h3')[1].text golfers = [row.text.split('@')[-1] for row in soup.findAll('h5')[-1].parent.findAll('h6')] record = Challenge(key_name=handle, handle=handle, title=title, active_golfers=golfers) record.put() logging.info('updated Challenge(%s, %s) with %d golfers' % (handle, title, len(golfers))) count = increment('challenge_tasks', -1) logging.info('challenge_tasks = %d' % count) if count == 0: taskqueue.add(url='/top')
def GET(self): clist = memcache.get("clist") if clist is None: clist = sorted(Challenge.all(), key=lambda c: len(c.active_golfers), reverse=1) memcache.set("clist", clist) render = web.template.render("templates") return render.index(clist)
def post_challenge(t, d, l, f): """Creates a new challenge and adds it to the db""" new_challenge = Challenge(title=t, description=d, difficulty=l, image_path=f) db.session.add(new_challenge) db.session.commit()
def load_challenges(): """ load challenge estimates from challenges.csv into database """ challenges_file = open("./seed_data/challenges.csv") for text in challenges_file: lines = text.strip().splitlines() for line in lines: line = line.split(",") a_challenge = Challenge(original_items=line[1], original_cost=float(line[2]), alternative_items=line[3], alternative_cost=float(line[4])) db.session.add(a_challenge) db.session.commit()
def POST(self): """Update Golfer table from Challenge data.""" logging.info('top()') challenges = list(Challenge.all()) logging.info('gathering active golfers') handles = set(h for c in challenges for h in c.active_golfers) ranksum = sum(len(c.active_golfers) for c in challenges) golfers = dict((h, Golfer(key_name='@'+h, handle=h, global_rank=ranksum)) for h in handles) logging.info('global rank sum %d' % ranksum) logging.info('calculating global rank for each golfer') for c in challenges: n = len(c.active_golfers) for i, h in enumerate(c.active_golfers): golfers[h].global_rank -= n - i - 1 logging.info('calculating rank for each golfer') glist = golfers.values() db.put(glist) logging.info('done updating golfer ranking')
from flask import Markup from flask import Response from flask import session, redirect, url_for, escape, request, jsonify from flask import g from model import db, DATABASE from model import get_without_failing from model import read_file, write_file from model import User, Task, Challenge, Record ### config with open(os.path.join(ROOT, 'config.yml'), "rt") as f: CONFIG = yaml.load(f) CHALLENGES = [Challenge(info) for info in CONFIG['challenges']] ### class CustomFlask(Flask): jinja_options = Flask.jinja_options.copy() jinja_options.update( dict( block_start_string='<%', block_end_string='%>', variable_start_string='%%', variable_end_string='%%', comment_start_string='<#', comment_end_string='#>', ))
def add_challenge(segment_id, year, month): segment = client.get_segment(segment_id) print(segment) Challenge.add(segment, year, month)
def GET(self, handle=None): challenge = Challenge.get_by_key_name(str(handle)) if not challenge: raise web.seeother('/') render = web.template.render('templates') return render.challenges(challenge)
def json_challenges(): return json.dumps(dict( (c.handle, { 'title':c.title, 'active_golfers':len(c.active_golfers), }) for c in Challenge.all()))