From 2ef024f7cd3fa940314af9417ce419f15a199d85 Mon Sep 17 00:00:00 2001
From: Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
Date: Fri, 23 Jul 2021 23:40:35 +0200
Subject: [PATCH] =?UTF-8?q?P=C5=99id=C3=A1n=20formul=C3=A1=C5=99=20na=20ed?=
 =?UTF-8?q?itaci=20registrace=20do=20ro=C4=8Dn=C3=ADku?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #96
---
 mo/web/org_users.py                           | 45 +++++++++++++++++++
 mo/web/templates/org_user.html                |  3 +-
 .../templates/org_user_participant_edit.html  |  8 ++++
 3 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 mo/web/templates/org_user_participant_edit.html

diff --git a/mo/web/org_users.py b/mo/web/org_users.py
index c29beed4..b2ceb96e 100644
--- a/mo/web/org_users.py
+++ b/mo/web/org_users.py
@@ -17,6 +17,7 @@ from mo.rights import Right
 import mo.util
 import mo.users
 from mo.web import app
+import mo.web.fields as mo_fields
 from mo.web.util import PagerForm
 
 
@@ -485,3 +486,47 @@ def org_user_new():
     if not is_duplicate_name:
         del form.allow_duplicate_name
     return render_template('org_user_new.html', form=form, is_org=is_org)
+
+
+class ParticipantEditForm(FlaskForm):
+    school = mo_fields.School("Škola", validators=[Required()], render_kw={'autofocus': True})
+    grade = mo_fields.Grade("Třída", validators=[Required()])
+    birth_year = mo_fields.BirthYear("Rok narození", validators=[Required()])
+    submit = wtforms.SubmitField("Uložit")
+
+
+@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()
+    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()
+
+    form = ParticipantEditForm(obj=participant)
+    if form.validate_on_submit():
+        form.populate_obj(participant)
+        if sess.is_modified(participant):
+            changes = db.get_object_changes(participant)
+
+            app.logger.info(f"Participant id {id} year {year} modified, changes: {changes}")
+            mo.util.log(
+                type=db.LogType.participant,
+                what=user_id,
+                details={'action': 'edit-participant', 'year': year, 'changes': changes},
+            )
+            sess.commit()
+            flash('Změny registrace uloženy', 'success')
+        else:
+            flash('Žádné změny k uložení', 'info')
+
+        return redirect(url_for('org_user', id=user_id))
+
+    return render_template('org_user_participant_edit.html', user=user, year=year, form=form)
diff --git a/mo/web/templates/org_user.html b/mo/web/templates/org_user.html
index e76644b2..966d4b94 100644
--- a/mo/web/templates/org_user.html
+++ b/mo/web/templates/org_user.html
@@ -43,7 +43,7 @@
 <table class="data full">
 	<thead>
 		<tr>
-			<th>Ročník<th>Škola<th>Třída<th>Rok narození
+			<th>Ročník<th>Škola<th>Třída<th>Rok narození<th>Akce
 		</tr>
 	</thead>
 {% for participant in participants %}
@@ -52,6 +52,7 @@
 		<td><a href="{{ url_for('org_place', id=participant.school) }}">{{ participant.school_place.name }}</a>
 		<td>{{ participant.grade }}
 		<td>{{ participant.birth_year }}
+		<td><a class="btn btn-xs btn-primary" href="{{ url_for('org_user_participant_edit', user_id=user.user_id, year=participant.year) }}">Editovat</a>
 	</tr>
 {% endfor %}
 </table>
diff --git a/mo/web/templates/org_user_participant_edit.html b/mo/web/templates/org_user_participant_edit.html
new file mode 100644
index 00000000..a4245de6
--- /dev/null
+++ b/mo/web/templates/org_user_participant_edit.html
@@ -0,0 +1,8 @@
+{% extends "base.html" %}
+{% import "bootstrap/wtf.html" as wtf %}
+{% block title %}Editace registrace soutěžícího {{ user.full_name() }} v {{ year }}. ročníku{% endblock %}
+{% block body %}
+
+{{ wtf.quick_form(form, form_type='horizontal', button_map={'submit': 'primary'}) }}
+
+{% endblock %}
-- 
GitLab