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

Oprava na formdata=request.args.
Rozdrobení metody validate na validate_[fields].
---
 mo/web/org_users.py             | 69 ++++++++++++++++++---------------
 mo/web/templates/org_users.html |  4 +-
 2 files changed, 40 insertions(+), 33 deletions(-)

diff --git a/mo/web/org_users.py b/mo/web/org_users.py
index bd30d6f6..78e023ed 100644
--- a/mo/web/org_users.py
+++ b/mo/web/org_users.py
@@ -29,14 +29,14 @@ class UsersFilterForm(PagerForm):
     search_email = wtforms.TextField("E-mail")
 
     # participants
-    year = wtforms.IntegerField("Ročník")
-    school_code = wtforms.StringField("Škola")
+    year = mo_fields.OptionalInt("Ročník")
+    school = mo_fields.School()
 
     # rounds->participations
-    round_year = wtforms.IntegerField("Ročník")
+    round_year = mo_fields.OptionalInt("Ročník")
     round_category = wtforms.SelectField("Kategorie")
     round_seq = wtforms.SelectField("Kolo")
-    contest_site_code = wtforms.StringField("Soutěžní oblast")
+    contest_site = mo_fields.Place("Soutěžní oblast")
     participation_state = wtforms.SelectField('Účast', choices=[('*', '*')] + list(db.PartState.choices()))
 
     submit = wtforms.SubmitField("Filtrovat")
@@ -53,34 +53,40 @@ class UsersFilterForm(PagerForm):
     f_contest_site: Optional[db.Place] = None
     f_participation_state: Optional[db.PartState] = None
 
-    def __init__(self, formdata, **kwargs):
-        super().__init__(formdata=formdata, **kwargs)
+    def __init__(self, **kwargs):
+        super().__init__(**kwargs)
         self.round_category.choices = ['*'] + sorted(db.get_categories())
         self.round_seq.choices = ['*'] + sorted(db.get_seqs())
 
-    def validate(self):
-        self.f_search_name = f"%{self.search_name.data}%" if self.search_name.data else None
-        self.f_search_email = f"%{self.search_email.data}%" if self.search_email.data else None
-        self.f_year = self.year.data
-        self.f_round_year = self.round_year.data
-
-        if self.school_code.data:
-            self.f_school = db.get_place_by_code(self.school_code.data)
-            if not self.f_school:
-                flash(f"Zadaná škola '{self.school_code.data}' neexistuje", "danger")
-                self.f_school = db.Place()
-        if self.contest_site_code.data:
-            self.f_contest_site = db.get_place_by_code(self.contest_site_code.data)
-            if not self.f_contest_site:
-                flash(f"Zadaná soutěžní oblast '{self.contest_site_code.data}' neexistuje", "danger")
-                self.f_contest_site = db.Place()
-
-        self.f_round_category = None if self.round_category.data == '*' else self.round_category.data
-        self.f_round_seq = None if self.round_seq.data == '*' else self.round_seq.data
-        self.f_participation_state = None if self.participation_state.data == '*' else self.participation_state.data
-
-
-@app.route('/org/user/')
+    def validate_search_name(self, field):
+        self.f_search_name = f"%{field.data}%"
+
+    def validate_search_email(self, field):
+        self.f_search_email = f"%{field.data}%"
+
+    def validate_year(self, field):
+        self.f_year = field.data
+
+    def validate_round_year(self, field):
+        self.f_round_year = field.data
+
+    def validate_school(self, field):
+        self.f_school = field.place
+
+    def validate_contest_site(self, field):
+        self.f_contest_site = field.place
+
+    def validate_round_category(self, field):
+        self.f_round_category = None if field.data == '*' else field.data
+
+    def validate_round_seq(self, field):
+        self.f_round_seq = None if field.data == '*' else field.data
+
+    def validate_participation_state(self, field):
+        self.f_participation_state = None if field.data == '*' else field.data
+
+
+@app.route('/org/user/', methods=('GET', 'POST'))
 def org_users():
     sess = db.get_session()
     rr = g.gatekeeper.rights_generic()
@@ -88,8 +94,9 @@ def org_users():
     q = sess.query(db.User).filter_by(is_admin=False, is_org=False).options(
         subqueryload(db.User.participants).joinedload(db.Participant.school_place)
     )
-    filter = UsersFilterForm(request.args)
-    filter.validate()
+    filter = UsersFilterForm(formdata=request.args)
+    if request.args:
+        filter.validate()
 
     if filter.f_search_name:
         q = q.filter(or_(
diff --git a/mo/web/templates/org_users.html b/mo/web/templates/org_users.html
index e1b2b2f6..c8c16ddf 100644
--- a/mo/web/templates/org_users.html
+++ b/mo/web/templates/org_users.html
@@ -20,7 +20,7 @@
 			{{ wtf.form_field(filter.round_seq) }}
 		</div>
 		<div class="col-sm-2">
-			{{ wtf.form_field(filter.contest_site_code, placeholder='Kód') }}
+			{{ wtf.form_field(filter.contest_site, placeholder='Kód') }}
 		</div>
 		<div class="col-sm-2">
 			{{ wtf.form_field(filter.participation_state) }}
@@ -32,7 +32,7 @@
 			{{ wtf.form_field(filter.year) }}
 		</div>
 		<div class="col-sm-2">
-			{{ wtf.form_field(filter.school_code, placeholder='Kód') }}
+			{{ wtf.form_field(filter.school, placeholder='Kód') }}
 		</div>
 		<div class="col-sm-3">
 			{{ wtf.form_field(filter.search_name, placeholder='Libovolná část jména') }}
-- 
GitLab