From 8665f4b308912285d8c3e1c53e564b3873e2b128 Mon Sep 17 00:00:00 2001
From: Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
Date: Sun, 19 Dec 2021 21:31:23 +0100
Subject: [PATCH] =?UTF-8?q?Maz=C3=A1n=C3=AD=20registrace=20do=20ro=C4=8Dn?=
 =?UTF-8?q?=C3=ADku?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mo/web/org_users.py | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/mo/web/org_users.py b/mo/web/org_users.py
index f50abead..683c0f79 100644
--- a/mo/web/org_users.py
+++ b/mo/web/org_users.py
@@ -572,6 +572,40 @@ class ParticipantEditForm(FlaskForm):
     submit = wtforms.SubmitField("Uložit")
 
 
+@app.route('/org/user/<int:user_id>/participant/<int:year>/delete', methods=('POST',))
+def org_user_participant_delete(user_id: int, year: int):
+    sess = db.get_session()
+    user = mo.users.user_by_uid(user_id)
+    if not user:
+        raise werkzeug.exceptions.NotFound()
+
+    rr = g.gatekeeper.rights_generic()
+    if not rr.can_edit_user(user):
+        raise werkzeug.exceptions.Forbidden()
+
+    participant = sess.query(db.Participant).filter_by(user_id=user.user_id).filter_by(year=year).one_or_none()
+    if participant is None:
+        raise werkzeug.exceptions.NotFound()
+
+    if sess.query(db.Participation).filter_by(user_id=user.user_id).filter(db.Participation.contest.has(db.Contest.round.has(year=year))).count() != 0:
+        flash('Registraci není možná smazat, soutěžící se v daném ročníku účastní nějakého kola.', 'danger')
+    else:
+        sess.delete(participant)
+
+        app.logger.info(f"Participant id {user_id} year {year} deleted")
+        mo.util.log(
+            type=db.LogType.participant,
+            what=user_id,
+            details={'action': 'delete-participant', 'year': year, 'participant': db.row2dict(participant)},
+        )
+
+        sess.commit()
+
+        flash('Registrace smazána.', 'success')
+
+    return redirect(url_for('org_user', id=user_id))
+
+
 @app.route('/org/user/<int:user_id>/participant/<int:year>/edit', methods=('GET', 'POST'))
 def org_user_participant_edit(user_id: int, year: int):
     sess = db.get_session()
-- 
GitLab