From ee187ada9c11003a5df3dced4a7f310b472ffd3d Mon Sep 17 00:00:00 2001
From: Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
Date: Sun, 1 Aug 2021 14:16:00 +0200
Subject: [PATCH] Fields: ParticipantsFilterForm
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Změna parsování dat z formuláře: obj=args -> formdata=args. Rozdrobení metody validate na
validate_[fields].
---
 mo/web/org_contest.py                  | 40 +++++++++++---------------
 mo/web/org_round.py                    |  2 +-
 mo/web/templates/org_contest_list.html |  4 +--
 mo/web/templates/org_round_list.html   |  6 ++--
 4 files changed, 23 insertions(+), 29 deletions(-)

diff --git a/mo/web/org_contest.py b/mo/web/org_contest.py
index 456c0b05..ab5cacfd 100644
--- a/mo/web/org_contest.py
+++ b/mo/web/org_contest.py
@@ -49,9 +49,9 @@ class ImportForm(FlaskForm):
 
 
 class ParticipantsFilterForm(PagerForm):
-    school = wtforms.StringField("Škola")
-    participation_place = wtforms.StringField("Soutěžní místo", render_kw={'autofocus': True})
-    contest_place = wtforms.StringField("Soutěžní oblast", render_kw={'autofocus': True})
+    school = mo_fields.School()
+    participation_place = mo_fields.Place("Soutěžní místo", render_kw={'autofocus': True})
+    contest_place = mo_fields.Place("Soutěžní oblast", render_kw={'autofocus': True})
     participation_state = wtforms.SelectField('Stav účasti', choices=[('*', '*')] + list(db.PartState.choices()), default='*')
 
     # format = wtforms.RadioField(choices=[('', 'Zobrazit'), ('csv', 'Stáhnout vše v CSV'), ('tsv', 'Stáhnout vše v TSV')])
@@ -66,24 +66,17 @@ class ParticipantsFilterForm(PagerForm):
     f_contest_place: Optional[db.Place] = None
     f_participation_state: Optional[db.PartState] = None
 
-    def validate(self):
-        if self.school.data:
-            self.f_school = db.get_place_by_code(self.school.data)
-            if not self.f_school:
-                flash(f"Zadaná škola '{self.school.data}' neexistuje", "danger")
-                self.f_school = db.School()
-        if self.participation_place.data:
-            self.f_participation_place = db.get_place_by_code(self.participation_place.data)
-            if not self.f_participation_place:
-                flash(f"Zadané soutěžní místo '{self.participation_place.data}' neexistuje", "danger")
-                self.f_participation_place = db.Place()
-        if self.contest_place.data:
-            self.f_contest_place = db.get_place_by_code(self.contest_place.data)
-            if not self.f_contest_place:
-                flash(f"Zadaná soutěžní oblast '{self.contest_place.data}' neexistuje", "danger")
-                self.f_contest_place = db.Place()
-
-        self.f_participation_state = None if self.participation_state.data == '*' else self.participation_state.data
+    def validate_school(self, field):
+        self.f_school = field.place
+
+    def validate_participation_place(self, field):
+        self.f_participation_place = field.place
+
+    def validate_contest_place(self, field):
+        self.f_contest_place = field.place
+
+    def validate_participation_state(self, field):
+        self.f_participation_state = None if field.data == '*' else field.data
 
 
 class ParticipantsActionForm(FlaskForm):
@@ -452,8 +445,9 @@ def org_contest_list(id: int, site_id: Optional[int] = None):
     can_edit = rr.have_right(Right.manage_contest) and request.endpoint != 'org_contest_list_emails'
     format = request.args.get('format', "")
 
-    filter = ParticipantsFilterForm(request.args)
-    filter.validate()
+    filter = ParticipantsFilterForm(formdata=request.args)
+    if request.args:
+        filter.validate()
     query = get_contestants_query(
         round=master_contest.round, contest=master_contest, site=site,
         school=filter.f_school,
diff --git a/mo/web/org_round.py b/mo/web/org_round.py
index 19e4f1e6..94e4aaca 100644
--- a/mo/web/org_round.py
+++ b/mo/web/org_round.py
@@ -369,7 +369,7 @@ def org_round_list(id: int):
     can_edit = rr.have_right(Right.manage_round) and request.endpoint != 'org_round_list_emails'
     format = request.args.get('format', "")
 
-    filter = ParticipantsFilterForm(request.args)
+    filter = ParticipantsFilterForm(formdata=request.args)
     filter.validate()
     query = get_contestants_query(
         round=master_round,
diff --git a/mo/web/templates/org_contest_list.html b/mo/web/templates/org_contest_list.html
index edf82b92..e8b01899 100644
--- a/mo/web/templates/org_contest_list.html
+++ b/mo/web/templates/org_contest_list.html
@@ -16,9 +16,9 @@ Seznam účastníků {% if site %}v soutěžním místě {{ site.name }}{% else
 <form action="" method="GET" class="form form-inline" role="form">
 	<div class="form-row">
 		{% if not site %}
-		{{ wtf.form_field(filter.participation_place, placeholder='Kód', size=8) }}
+		{{ wtf.form_field(filter.participation_place, size=8) }}
 		{% endif %}
-		{{ wtf.form_field(filter.school, placeholder='Kód', size=8) }}
+		{{ wtf.form_field(filter.school, size=8) }}
 		{{ wtf.form_field(filter.participation_state) }}
 		<div class="btn-group">
 			{{ wtf.form_field(filter.submit, class='btn btn-primary') }}
diff --git a/mo/web/templates/org_round_list.html b/mo/web/templates/org_round_list.html
index 343977ac..4dbdcf32 100644
--- a/mo/web/templates/org_round_list.html
+++ b/mo/web/templates/org_round_list.html
@@ -11,9 +11,9 @@
 <div class="form-frame">
 <form action="" method="GET" class="form form-inline" role="form">
 	<div class="form-row">
-		{{ wtf.form_field(filter.contest_place, placeholder='Kód', size=8) }}
-		{{ wtf.form_field(filter.participation_place, placeholder='Kód', size=8) }}
-		{{ wtf.form_field(filter.school, placeholder='Kód', size=8) }}
+		{{ wtf.form_field(filter.contest_place, size=8) }}
+		{{ wtf.form_field(filter.participation_place, size=8) }}
+		{{ wtf.form_field(filter.school, size=8) }}
 		{{ wtf.form_field(filter.participation_state) }}
 	</div>
 	<div class="form-row" style="margin-top: 5px;">
-- 
GitLab