AACResults/aacstats.py
2018-08-14 10:54:02 +02:00

128 lines
5.0 KiB
Python

from flask import Flask, request, render_template, url_for
import math
import MySQLdb
import MySQLdb.cursors
import datetime as dt
app = Flask(__name__)
@app.route('/')
def index():
start = int(request.args.get('start', '0'))
limit = int(request.args.get('limit', '10'))
results = read_db(start, limit)
total = int(total_entries())
return render_template('index.html', ltype='index', request=request, start=start, limit=limit, results=results, total=total)
@app.route('/race/<year>/<title>')
def race(start=0, year=None, title=None):
start = int(request.args.get('start', '0'))
limit = int(request.args.get('limit', '10'))
total = int(total_entries(event=title, year=year))
results = read_db(start, limit, event=title, year=year)
return render_template('index.html', ltype='race', title=title, year=year, request=request, start=start, limit=limit, results=results, total=total)
@app.route('/person/<title>')
@app.route('/person/<title>/<year>')
def person(start=0, title=None, year=None):
start = int(request.args.get('start', '0'))
limit = int(request.args.get('limit', '10'))
total = int(total_entries(person=title, year=year))
results = read_db(start, limit, person=title, year=year)
return render_template('index.html', ltype='person', title=title, year=year, request=request, start=start, limit=limit, results=results, total=total)
@app.route('/licence/<year>/<title>')
def licence(start=0, year=dt.datetime.now().year, title=None):
start = int(request.args.get('start', '0'))
limit = int(request.args.get('limit', '10'))
total = int(total_entries(licence=title, year=year))
results = read_db(start, limit, licence=title, year=year)
return render_template('index.html', ltype='licence', title=title, year=year, request=request, start=start, limit=limit, results=results, total=total)
def read_db(start=0, limit=10, person=None, licence=None, event=None, year=None):
db = MySQLdb.connect(user = 'aac', passwd = 'saOAcCWHg4LaoSSA', db = 'AAC', cursorclass = MySQLdb.cursors.DictCursor)
c = db.cursor()
where = 'club LIKE "AAC"'
if person:
where += ' AND CONCAT_WS(" ", name, surname) LIKE "{}"'.format(person)
if event:
where += ' AND event LIKE "{}"'.format(event)
if licence:
where += ' AND licence LIKE "{}"'.format(licence)
if year:
firstdate = dt.datetime.min
lastdate = dt.datetime.max
firstdate = firstdate.replace(year=int(year))
lastdate = lastdate.replace(year=int(year))
where += ' AND date > "{}" AND date < "{}"'.format(firstdate, lastdate)
sql = 'SELECT * FROM `results` WHERE {} ORDER BY date DESC, event, position LIMIT {},{};'.format(where, start, limit)
c.execute(sql)
results = c.fetchall()
return results
@app.template_filter('total_entries')
def total_entries(person=None, licence=None, event=None, year=None):
db = MySQLdb.connect(user = 'aac', passwd = 'saOAcCWHg4LaoSSA', db = 'AAC', cursorclass = MySQLdb.cursors.DictCursor)
c = db.cursor()
where = 'club LIKE "AAC"'
if person:
where += ' AND CONCAT_WS(" ", name, surname) LIKE "{}"'.format(person)
if event:
where += ' AND event LIKE "{}"'.format(event)
if licence:
where += ' AND licence LIKE "{}"'.format(licence)
if year:
firstdate = dt.datetime.min
lastdate = dt.datetime.max
firstdate = firstdate.replace(year=int(year))
lastdate = lastdate.replace(year=int(year))
where += ' AND date > "{}" AND date < "{}"'.format(firstdate, lastdate)
sql = 'SELECT COUNT(*) FROM `results` WHERE {}'.format(where)
c.execute(sql)
for x in c.fetchone().values():
return x
@app.template_filter('pace')
def pace(time):
return (dt.datetime(1,1,1) + time).strftime('%M:%S')
@app.template_filter('clean_date')
def clean_date(time):
if time.month == 1 and time.day == 1:
return time.strftime('%Y')
return time.strftime('%Y-%m-%d')
@app.template_filter('year')
def year(time):
return time.strftime('%Y')
@app.template_filter('ordinal')
def ordinal(n):
return "%d%s" % (n,"tsnrhtdd"[(math.floor(n/10)%10!=1)*(n%10<4)*n%10::4])
if __name__ == '__main__':
app.run(debug=True)
# most race KMs in the year, by distance
# fastest race pace over the year (time / KMs)
# individual KMs, and race results (race, position, time) by name
# - and by race number and year
# by race
# - and by gender
# default: list of races (sorted by recent)
# tabs: list of [races (sorted by recent), people (sorted by total kms for the year), licences for the year (sorted by number of races), podiums/winners (people sorted by total_position/total_races), ]
# click to expand by [race (all AAC members by position), person (races by recent), person (races by pace)]
# SEARCH
# /?sort={distance,pace}&sex={m,f}
# /race/2018/HEWAT ETC?sort={position,pace}&sex={m,f}
# /person/Timothy Allen?sort={pace,date}
# /person/Timothy Allen/2018
# /license/2018/4356
# /license/2018/4356/2018
#
# TODO LIMIT/pagination