Exemple #1
0
    def _query_or_err(self, srch):
        ret = query_selectors(self.db, model.Team, [srch]).first()

        if ret is None:
            raise RsxError("No such team: '%s'" % srch)

        return ret
Exemple #2
0
def insert_missing_interactive(db,
                               cls,
                               selectors,
                               creator=None,
                               autoconfirm=None,
                               echo=True):
    ids, names = _split_selectors(selectors)
    have_ids = db.query(cls).filter(cls.id.in_(ids)).all()

    if len(have_ids) < len(ids):
        raise RsxError("Missing ids: %s (from class: %s)" %
                       (set(ids).difference(x.id
                                            for x in have_ids), cls.__name__))

    have_names = db.query(cls).filter(cls.name.in_(names)).all()

    if len(have_names) < len(names):
        missing = set(names).difference(x.name for x in have_names)

        if autoconfirm is False or creator is None:
            raise RsxError("Missing names: %s (from class: %s)" %
                           (missing, cls.__name__))

        if not autoconfirm:
            click.echo("Not in db (%d): %s" %
                       (len(missing), ", ".join(missing)))
            click.confirm("OK to add?", default=True, abort=True)

        add = list(map(creator, missing))

        db.add_all(add)

        # commit että tietokanta antaa niille primary keyt
        db.commit()

        if echo:
            for x in add:
                click.echo("%s %s" % (add_sym, nameid(x)))

        have_names += add

    return have_ids + have_names
Exemple #3
0
    def del_(self, srch):
        team = self._query_or_err(srch)
        self.db.delete(team)

        try:
            self.db.commit()
        except IntegrityError:
            # jos joukkueella on suorituksia niin ei anna poistaa
            self.db.rollback()
            raise RsxError("Cannot delete team with events")

        click.echo("%s %s" % (del_sym, nameid(team)))
Exemple #4
0
def show_ranking(ranking, opt):
    try:
        ranks = opt.init.tournament.rankings[ranking]
    except KeyError:
        raise RsxError("No such ranking: '%s'" % ranking)

    ranks = ranks(opt.db)

    if opt.hide_shadows:
        ranks = [r for r in ranks if not r[0].is_shadow]

    opt.fmt.print_ranking(ranks)
Exemple #5
0
    def rename(self, srch, name):
        team = self._query_or_err(srch)
        old_nameid = nameid(team)
        team.name = name

        try:
            self.db.commit()
        except IntegrityError as e:
            # todennäkösesti nimi on jo jollakin toisella joukkueella
            self.db.rollback()
            raise RsxError("Rename failed: %s" % str(e))

        click.echo("%s %s %s" %
                   (old_nameid, click.style("=>", bold=True), nameid(team)))
Exemple #6
0
def shadow_command(**kwargs):
    db = kwargs["db"]
    team = query_selectors(db, model.Team, [kwargs["selector"]]).first()

    if team is None:
        raise RsxError("Team not found: %s" % kwargs["selector"])

    old_nameid = nameid(team)

    v = {
        "on": True,
        "off": False,
        "toggle": not team.is_shadow
    }[kwargs["state"]]
    team.is_shadow = v

    click.echo("%s %s %s" %
               (old_nameid, click.style("=>", bold=True), nameid(team)))

    db.commit()
Exemple #7
0
def import_command(db, strict, y, **kwargs):
    timetable = parse_timetable(kwargs["file"].read(),
                                time_fmt=kwargs["datefmt"])

    if not timetable:
        return

    k = len(timetable[0]) - kwargs["j"] - 1
    teams = set(timetable[:, 1:1 + k].labels)
    judges = set(timetable[:, 1 + k:].labels)

    autoconfirm = False if strict else (True if y else None)
    teams = insert_missing_interactive(
        db,
        model.Team,
        teams,
        creator=lambda name: model.Team(name=name,
                                        is_shadow=name.startswith("::")),
        autoconfirm=autoconfirm)

    judges = insert_missing_interactive(
        db,
        model.Judge,
        judges,
        creator=lambda name: model.Judge(name=name),
        autoconfirm=autoconfirm)

    teams = dict((t.name, t) for t in teams)
    judges = dict((j.name, j) for j in judges)

    events = []
    for e in timetable:
        event = model.Event(block_id=kwargs["block"],
                            ts_sched=int(e.time.timestamp()),
                            arena=e[0].name,
                            teams_part=[
                                model.EventTeam(team_id=teams[l.name].id)
                                for l in e[1:1 + k]
                            ],
                            judgings=[
                                model.EventJudging(judge_id=judges[l.name].id)
                                for l in e[1 + k:]
                            ])

        #print([teams[l.name].id for l in e[1:1+k]])

        #event.team_ids.extend(teams[l.name].id for l in e[1:1+k])
        #event.judge_ids.extend(judges[l.name].id for l in e[1+k:])
        events.append(event)

    db.add_all(events)

    try:
        db.commit()
    except IntegrityError:
        # TODO tähän joku hyödyllisempi virhe, etsi vaikka mitkä eventit aiheuttaa ongelmia
        db.rollback()
        raise RsxError("Block conflict")

    click.echo("%s Added %d events to block %s" % (click.style(
        "[+]", fg="green", bold=True), len(events), kwargs["block"]))
Exemple #8
0
 def wrapper(opt):
     if opt.param is None:
         raise RsxError("Missing parameter: %s" % name)
     return f(**{name: opt.param, "opt": opt})
Exemple #9
0
 def ret(opt):
     if opt.init is None:
         raise RsxError("No init given")
     return f(opt)