def events(): query = db.query(model.Event)\ .options( selectinload(model.Event.teams_part)\ .joinedload(model.EventTeam.team, innerjoin=True), selectinload(model.Event.judgings)\ .joinedload(model.EventJudging.judge, innerjoin=True) ) params = flask.request.values query = filter_events(query, params) if "limit" in params: query = query.limit(params.get("limit", type=int)) ret = query.all() return [{ "id": e.id, "block": { "id": e.block_id, "name": get_block(e).name }, "ts_sched": e.ts_sched, "arena": e.arena, "teams": list(map(jsonify, e.teams)), "judges": list(map(jsonify, e.judges)) } for e in ret]
def render_scoring(judging): ruleset = get_block(judging.event).ruleset return flask.render_template("judging/scoring-rescue-%s.html" % str(ruleset.difficulty), judging=judging, weights=WEIGHTS, event_data=get_event_data(judging))
def render_event_card(self, judging): ruleset = get_block(judging.event).ruleset try: renderer = card_renderer[ruleset] except AttributeError: return "" return renderer(judging)
def save_scores(self, judging, scores): ruleset = get_block(judging.event).ruleset team_scores = dict(scores) for s in judging.scores: s.data = ruleset.encode(team_scores[s.team_id]) judging.ts = int(time.time()) db.commit()
def save_score_post(self, judging): try: scores = self.parse_scoring_post(judging) except ScoreParserError as e: request_logger.warning("Failed to parse score post: %s" % str(e)) request_logger.log_post_body(logging.WARNING) return str(e), 400 ruleset = get_block(judging.event).ruleset try: ruleset.validate(*(s for _,s in scores)) except ValidationError as e: request_logger.warning("Failed to validate scores: %s" % str(e)) request_logger.log_post_body(logging.WARNING) return str(e), 400 if set(s.team_id for s in judging.scores) != set(s[0] for s in scores): request_logger.warning("Received scores for: %s | Expected: %s" % ( ",".join(str(s[0]) for s in scores), ",".join(str(s.team_id) for s in judging.scores) )) request_logger.log_post_body(logging.WARNING) return "Invalid team list", 400 # Jos tästä tulee joku sqlalchemy virhe vielä nyt vielä validoinnin # ja kaiken jälkeen niin se ei oo virhe pyynnössä vaan bugi, # joten turha enää tässä try catchata mitään self.save_scores(judging, scores) request_logger.info("Saved scores [event=%d in block %s]" % ( judging.event_id, judging.event.block_id )) score_info = [(team_id, score, base64.b64encode(ruleset.encode(score)).decode("utf8"))\ for team_id, score in scores] for si in score_info: request_logger.info("Team (id): %d | Score: %s | Base64: %s" % si) request_logger.log_post_body(logging.DEBUG) flask.flash( flask.render_template("judging/score-saved-message.html", score_info=score_info), "success" ) return "OK: %s" % str(score_info)
def parse_post_xs(judging, json): try: tid1 = int(json["team1"]) tid2 = int(json["team2"]) except TypeError as e: raise ScoreParserError("Failed to parse team id: %s" % str(e)) ruleset = get_block(judging.event).ruleset s1 = ruleset.create_score() s2 = ruleset.create_score() for r in json["rounds"]: if "first" in r: first = r["first"] if str(first) not in ("0", "1"): raise ScoreParserError("Invalid first value: %s" % first) first = int(first) else: first = None if "result" in r: res = str(r["result"]) if res == "tie": r1, r2 = "T", "T" elif res == "0": r1, r2 = "W", "L" elif res == "1": r1, r2 = "L", "W" else: raise ScoreParserError("Invalid result: %s" % res) else: r1, r2 = "L", "L" s1.rounds.append(XSRoundScore(first == 0, XSumoResult(r1))) s2.rounds.append(XSRoundScore(first == 1, XSumoResult(r2))) calc_results(s1, s2) return (tid1, s1), (tid2, s2)
def parse_post(judging, json): ruleset = get_block(judging.event).ruleset scores = json["scores"] ret = ruleset.create_score() for k, v in ret.score_categories: setattr(ret, k, cat_decoder[v](scores[k])) time = json["time"] try: t_min = int(time["min"]) t_sec = int(time["sec"]) except TypeError as e: raise ScoreParserError("Invalid time: %s" % str(e)) # Tässä ei tarkisteta että nää olis positiivia tai että t_sec<60 # mutta eipä se haittaa että tähän voi laittaa virheellisiä arvoja # koska koko score tarkistetaan myöhemmin ret.time = 60 * t_min + t_sec return (judging.event.team.id, ret),
def parse_scoring_post(self, judging): ruleset = get_block(judging.event).ruleset return post_parser[ruleset](judging)
def render_scoring_form(self, judging): ruleset = get_block(judging.event).ruleset return scoring_renderer[ruleset](judging)
def render_other_event(event): return flask.render_template("timetable/event-other.html", event=event, name=get_block(event).ruleset.name)