Skip to content
Snippets Groups Projects
Commit 32d5abf6 authored by Martin Mareš's avatar Martin Mareš
Browse files

Abstrahován mechanismus zakládání soutěží

parent f6d771fb
Branches
No related tags found
No related merge requests found
# Pomocné funkce pro práci se soutěžemi
from typing import Optional
import mo.db as db
from mo.rights import Right, Gatekeeper
import mo.util
class ContestError(RuntimeError):
pass
def add_contest(master_round: db.Round, place: db.Place, reason: str, gatekeeper: Optional[Gatekeeper] = None):
# Počáteční stav soutěže
if master_round.state != db.RoundState.delegate:
state = master_round.state
else:
state = db.RoundState.preparing
# Soutěž vytvoříme vždy v hlavním kole
contest = db.Contest(round=master_round, place=place, state=state)
if gatekeeper is not None:
rr = gatekeeper.rights_for_contest(contest)
if not rr.have_right(Right.add_contest):
raise ContestError(f'Vaše role nedovoluje vytvořit soutěž {place.name_locative()}')
sess = db.get_session()
sess.add(contest)
sess.flush()
contest.master_contest_id = contest.contest_id
sess.add(contest)
sess.flush()
mo.util.log(
type=db.LogType.contest,
what=contest.contest_id,
details={'action': 'add', 'contest': db.row2dict(contest), 'reason': reason},
)
mo.util.logger.info(f"{reason.title()}: Soutěž #{contest.contest_id} založena: {db.row2dict(contest)}")
create_subcontests(master_round, contest, reason)
def create_subcontests(master_round: db.Round, master_contest: db.Contest, reason: str):
if master_round.part == 0:
return
sess = db.get_session()
subrounds = master_round.get_group_rounds()
for subround in subrounds:
subcontest = db.Contest(
round_id=subround.round_id,
master_contest_id=master_contest.contest_id,
place_id=master_contest.place_id,
state=master_contest.state,
)
sess.add(subcontest)
sess.flush()
mo.util.log(
type=db.LogType.contest,
what=subcontest.contest_id,
details={'action': 'add', 'contest': db.row2dict(subcontest), 'reason': reason},
)
mo.util.logger.info(f"{reason.title()}: Podsoutěž #{subcontest.contest_id} založena: {db.row2dict(subcontest)}")
......@@ -17,6 +17,7 @@ from wtforms import validators
from wtforms.widgets import NumberInput
import mo.config as config
import mo.contests
import mo.db as db
import mo.imports
import mo.jobs.notify
......@@ -103,7 +104,10 @@ def add_contest(round: db.Round, form: AddContestForm) -> bool:
flash(f'Pro {place.type_name()} {place.name} už toto kolo existuje', 'danger')
return False
if not do_add_contest(round.master, place, True):
try:
mo.contests.add_contest(round.master, place, reason='web', gatekeeper=g.gatekeeper)
except mo.contests.ContestError as e:
flash(str(e), 'danger')
return False
sess.commit()
......@@ -111,64 +115,6 @@ def add_contest(round: db.Round, form: AddContestForm) -> bool:
return True
def do_add_contest(master_round: db.Round, place: db.Place, check_rights: bool) -> bool:
# Počáteční stav soutěže
if master_round.state != db.RoundState.delegate:
state = master_round.state
else:
state = db.RoundState.preparing
# Soutěž vytvoříme vždy v hlavním kole
contest = db.Contest(round=master_round, place=place, state=state)
if check_rights:
rr = g.gatekeeper.rights_for_contest(contest)
if not rr.have_right(Right.add_contest):
flash(f'Vaše role nedovoluje vytvořit soutěž {place.name_locative()}', 'danger')
return False
sess = db.get_session()
sess.add(contest)
sess.flush()
contest.master_contest_id = contest.contest_id
sess.add(contest)
sess.flush()
mo.util.log(
type=db.LogType.contest,
what=contest.contest_id,
details={'action': 'add', 'contest': db.row2dict(contest)},
)
app.logger.info(f"Soutěž #{contest.contest_id} založena: {db.row2dict(contest)}")
create_subcontests(master_round, contest)
return True
# XXX: Používá se i v registraci účastníků
def create_subcontests(master_round: db.Round, master_contest: db.Contest):
if master_round.part == 0:
return
sess = db.get_session()
subrounds = master_round.get_group_rounds()
for subround in subrounds:
subcontest = db.Contest(
round_id=subround.round_id,
master_contest_id=master_contest.contest_id,
place_id=master_contest.place_id,
state=master_contest.state,
)
sess.add(subcontest)
sess.flush()
mo.util.log(
type=db.LogType.contest,
what=subcontest.contest_id,
details={'action': 'add', 'contest': db.row2dict(subcontest)},
)
app.logger.info(f"Podsoutěž #{subcontest.contest_id} založena: {db.row2dict(subcontest)}")
@dataclass
class ContestStat:
region: db.Place
......@@ -696,8 +642,7 @@ def org_round_create_contests(round_id: int):
form = CreateContestsForm()
if form.validate_on_submit():
for place in new_places:
ok = do_add_contest(round, place, False)
assert ok
mo.contests.add_contest(round, place, reason='web')
sess.commit()
flash(inflect_with_number(len(new_places), 'Založena %s soutěž.', 'Založeny %s soutěže.', 'Založeno %s soutěží.'), 'success')
return redirect(ctx.url_for('org_round'))
......
......@@ -12,6 +12,7 @@ import werkzeug.exceptions
import wtforms
from wtforms.validators import DataRequired
import mo.contests
import mo.config as config
import mo.email
import mo.db as db
......@@ -208,7 +209,7 @@ def join_create_contest(round: db.Round, pant: db.Participant) -> db.Contest:
details={'action': 'created', 'reason': 'user-join'},
)
mo.web.org_round.create_subcontests(round, c)
mo.contests.create_subcontests(round, c, reason='user-join')
return c
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment