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

Zakládání a zobrazování soutěží

parent 24a7cc27
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
import argparse
import sys
import mo.db as db
import mo.util
parser = argparse.ArgumentParser(description='Založí soutěže pro dané kolo')
parser.add_argument(dest='round_id', type=int, help='ID kola')
parser.add_argument('-n', '--dry-run', default=False, action='store_true', help='pouze ukáže, co by bylo provedeno')
args = parser.parse_args()
sess = db.get_session()
round = sess.query(db.Round).get(args.round_id)
if not round:
print("Kolo s tímto ID neexistuje!", file=sys.stderr)
sys.exit(1)
regions = sess.query(db.Place).filter_by(level=round.level).all()
assert regions, "Neexistují žádná místa dané úrovně"
for r in regions:
print(f"Zakládám pro místo {r.name}")
if not args.dry_run:
c = db.Contest(round=round, place=r)
sess.add(c)
sess.flush()
mo.util.log(db.LogType.contest, c.contest_id, {
'action': 'created',
})
sess.commit()
...@@ -82,9 +82,9 @@ CREATE TABLE rounds ( ...@@ -82,9 +82,9 @@ CREATE TABLE rounds (
-- Soutěže (instance kola v konkrétním místě) -- Soutěže (instance kola v konkrétním místě)
CREATE TABLE contests ( CREATE TABLE contests (
contest_id serial PRIMARY KEY, contest_id serial PRIMARY KEY,
round int NOT NULL REFERENCES rounds(round_id), round_id int NOT NULL REFERENCES rounds(round_id),
region int NOT NULL REFERENCES places(place_id), place_id int NOT NULL REFERENCES places(place_id),
UNIQUE (round, region) UNIQUE (round_id, place_id)
); );
-- Detaily účastníka -- Detaily účastníka
......
...@@ -125,15 +125,15 @@ class User(Base): ...@@ -125,15 +125,15 @@ class User(Base):
class Contest(Base): class Contest(Base):
__tablename__ = 'contests' __tablename__ = 'contests'
__table_args__ = ( __table_args__ = (
UniqueConstraint('round', 'region'), UniqueConstraint('round_id', 'place_id'),
) )
contest_id = Column(Integer, primary_key=True, server_default=text("nextval('contests_contest_id_seq'::regclass)")) contest_id = Column(Integer, primary_key=True, server_default=text("nextval('contests_contest_id_seq'::regclass)"))
round = Column(Integer, ForeignKey('rounds.round_id'), nullable=False) round_id = Column(Integer, ForeignKey('rounds.round_id'), nullable=False)
region = Column(Integer, ForeignKey('places.place_id'), nullable=False) place_id = Column(Integer, ForeignKey('places.place_id'), nullable=False)
region_object = relationship('Place') place = relationship('Place')
round_object = relationship('Round') round = relationship('Round')
class LogType(str, PythonEnum): class LogType(str, PythonEnum):
general = auto() general = auto()
......
...@@ -62,3 +62,7 @@ def send_password_reset_email(user: db.User, link: str): ...@@ -62,3 +62,7 @@ def send_password_reset_email(user: db.User, link: str):
if sm.returncode != 0: if sm.returncode != 0:
raise RuntimeError('Sendmail failed with return code {}'.format(sm.returncode)) raise RuntimeError('Sendmail failed with return code {}'.format(sm.returncode))
def get_root_place():
return db.get_session().query(db.Place).filter_by(parent=None).one()
...@@ -7,6 +7,7 @@ import wtforms ...@@ -7,6 +7,7 @@ import wtforms
import mo import mo
import mo.db as db import mo.db as db
import mo.rights import mo.rights
import mo.util
from mo.web import app from mo.web import app
import wtforms.validators as validators import wtforms.validators as validators
...@@ -220,7 +221,7 @@ def org_place_new_child(id: int): ...@@ -220,7 +221,7 @@ def org_place_new_child(id: int):
@app.route('/org/place/') @app.route('/org/place/')
def org_place_root(): def org_place_root():
root = db.get_session().query(db.Place).filter_by(parent=None).one() root = mo.util.get_root_place()
return redirect(url_for('org_place', id=root.place_id)) return redirect(url_for('org_place', id=root.place_id))
...@@ -258,11 +259,29 @@ def org_contest_root(): ...@@ -258,11 +259,29 @@ def org_contest_root():
return render_template('org_contest_root.html', rounds=rounds, level_names=mo.db.place_level_names) return render_template('org_contest_root.html', rounds=rounds, level_names=mo.db.place_level_names)
@app.route('/org/contest/<int:id>/') @app.route('/org/contest/r/<int:id>/')
def org_contest(id: int): def org_contest_round(id: int):
sess = db.get_session() sess = db.get_session()
round = sess.query(db.Round).get(id) round = sess.query(db.Round).get(id)
if not round: if not round:
raise werkzeug.exceptions.NotFound() raise werkzeug.exceptions.NotFound()
return render_template('org_contest.html', round=round, level_names=mo.db.place_level_names) contests = (sess.query(db.Contest)
.filter_by(round=round)
.options(joinedload(db.Contest.place))
.all())
return render_template('org_contest_round.html', round=round, contests=contests, level_names=mo.db.place_level_names)
@app.route('/org/contest/c/<int:id>')
def org_contest(id):
sess = db.get_session()
contest = (sess.query(db.Contest)
.options(joinedload(db.Contest.place),
joinedload(db.Contest.round))
.get(id))
if not contest:
raise werkzeug.exceptions.NotFound()
return render_template('org_contest.html', contest=contest)
{% extends "base.html" %} {% extends "base.html" %}
{% block body %} {% block body %}
<h2>Soutěžní kolo {{ round.round_code() }}</h2> <h2>Soutěž {{ contest.round.round_code() }}: {{ contest.place.name }}</h2>
<table class=data> <table class=data>
<tr><td>Ročník<td>{{ round.year }} <tr><td>Ročník<td>{{ contest.round.year }}
<tr><td>Kategorie<td>{{ round.category }} <tr><td>Kategorie<td>{{ contest.round.category }}
<tr><td>Pořadí<td>{{ round.seq }} <tr><td>Pořadí<td>{{ contest.round.seq }}
<tr><td>Oblast<td>{{ level_names[round.level] }} <tr><td>Název<td>{{ contest.round.name }}
<tr><td>Název<td>{{ round.name }} <tr><td>Oblast<td>{{ contest.place.name }}
</table> </table>
{% endblock %} {% endblock %}
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<th>Název <th>Název
{% for r in rounds %} {% for r in rounds %}
<tr> <tr>
<td><a href='{{ url_for('org_contest', id=r.round_id) }}'>{{ r.round_code() }}</a> <td><a href='{{ url_for('org_contest_round', id=r.round_id) }}'>{{ r.round_code() }}</a>
<td>{{ r.year }} <td>{{ r.year }}
<td>{{ r.category }} <td>{{ r.category }}
<td>{{ r.seq }} <td>{{ r.seq }}
......
{% extends "base.html" %}
{% block body %}
<h2>Soutěžní kolo {{ round.round_code() }} ({{ round.name }})</h2>
<table class=data>
<tr><td>Ročník<td>{{ round.year }}
<tr><td>Kategorie<td>{{ round.category }}
<tr><td>Pořadí<td>{{ round.seq }}
<tr><td>Název<td>{{ round.name }}
<tr><td>Oblast<td>{{ level_names[round.level] }}
</table>
<h3>Soutěže</h3>
{% if contests %}
<table class=data>
{% for c in contests %}
<tr>
<td><a href='{{ url_for('org_contest', id=c.contest_id) }}'>{{ c.place.name }}</a>
{% endfor %}
</table>
{% else %}
<p>Zatím nebyly založeny v žádné oblasti.
{% endif %}
{% endblock %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment