Repozitař s webem k orgodostihům
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

145 lines
3.6 KiB

#!/usr/bin/python3
from flask import Flask,render_template,session,request,redirect,abort
import json
import sys
import logging
import datetime
import toml
SECRET_KEY = '!Êrû÷FwÙxIER'
CONFIG_FILE = 'config.toml'
app = Flask(__name__)
app.secret_key = SECRET_KEY
app.logger.setLevel(logging.DEBUG)
SERVER_NAME = '127.0.0.1'
config = {}
with open(CONFIG_FILE) as f:
config = toml.load(f)
print(config)
if not config["orgs"]:
app.logger.error("Orgs part of config file missing")
sys.exit(1)
if not config["teams"]:
app.logger.error("Teams part of config file missing")
sys.exit(1)
if not config["times"]:
app.logger.error("Times part of config file missing")
sys.exit(1)
order = [{"id":i,"value":str(i+1)+"."} for i in range(len(config["teams"]))]
order.append({"id":"t", "value":"Třezalka"})
order.append({"id":"n", "value":"Neběžel"})
around = 0
def check_form(form,config):
errors = []
for t in config["teams"]:
if "poradi1_"+str(t['id']) not in form:
errors.append("Tým "+t['name']+" nemá vyplněné pořadí u 1. člověka")
if "poradi2_"+str(t['id']) not in form:
errors.append("Tým "+t['name']+" nemá vyplněné pořadí u 2. člověka")
return errors
def team_points(per1,order1,per2,order2,config):
# FIXME hardcoded interactions
if (per1 == 'Kodein' and per2 == 'Paralen') or \
(per1 == 'Penicilin' and per2 == 'Strepsils'):
# FIXME co třezalka?
order1 = min(order1,order2)
order2 = min(order1,order2)
pts = 0
# Pokud neběžel první, neběžel ani druhý
if order1 == 'n':
return config['no_pts']
# První měl třezalku
if order1 == 't':
pts += config['base_pts']
# První běžel normálně
else:
org = get_org_by_id(per1,config)
pts +=config['base_pts']*config['order_coeff'][int(order1)]*org['coeff']
# Druhý neběžěl, body za oba jsou body prvního, můžeme vrátit
if order2 == 'n':
return pts
# Druhý měl třezalku
if order2 == 't':
pts += config['base_pts']
# Druhý běžel normálně
else:
org = get_org_by_id(per2,config)
pts += config['base_pts']*config['order_coeff'][int(order2)]*org['coeff']
return pts
def get_person(form,tid,pid,config):
for org in config["orgs"]:
if form["clovek{}_{}_{}".format(pid,tid,org["id"])] == "true":
return org["id"]
return None
def get_team_by_id(aid,config):
for t in config["teams"]:
if t["id"] == aid:
return t
def get_org_by_id(aid,config):
for o in config["orgs"]:
if o["id"] == aid:
return o
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/form',methods=['GET','POST'])
def form_page():
if request.method == 'GET':
return render_template("form.html",
round = config["times"][around],
people = config["orgs"],
order = order,
teams = config["teams"],
)
if request.method == 'POST':
print(request.form)
errors = check_form(request.form,config)
if errors:
return render_template("form.html",
round = config["times"][around],
people = config["orgs"],
order = order,
teams = config["teams"],
errors = errors
)
for team in config["teams"]:
order1 = request.form['poradi1_'+str(team['id'])]
order2 = request.form['poradi2_'+str(team['id'])]
per1 = request.form['clovek1_'+str(team['id'])]
per2 = request.form.get('clovek2_'+str(team['id']),None)
team["points"] += team_points(per1,order1,per2,order2,config)
return render_template("form.html",
round = config["times"][around],
people = config["orgs"],
order = order,
teams = config["teams"],
errors = []
)
@app.route('/tablo')
def tablo():
return render_template("tablo.html",teams=config["teams"])