From 84baee51cfa20bef6a774c5fa52a979ec30876bb 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
Změna komunikace z GET na POST. Zavedení používání validate_on_submit a
rozdrobení metody validate na validate_[fields].
---
mo/web/org_users.py | 68 ++++++++++++++++++---------------
mo/web/templates/org_users.html | 7 ++--
2 files changed, 41 insertions(+), 34 deletions(-)
diff --git a/mo/web/org_users.py b/mo/web/org_users.py
index bfe5e22b..134aa2c1 100644
--- a/mo/web/org_users.py
+++ b/mo/web/org_users.py
@@ -28,14 +28,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")
@@ -52,34 +52,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()
@@ -87,8 +93,8 @@ 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()
+ filter.validate_on_submit()
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..c1c63aae 100644
--- a/mo/web/templates/org_users.html
+++ b/mo/web/templates/org_users.html
@@ -7,7 +7,8 @@
{% endif %}
<div class="form-frame">
-<form action="" method="GET" role="form">
+<form action="" method="POST" role="form">
+ {{ filter.csrf_token }}
<div class="row">
<div class='col-sm-2'><strong>Filtr podle účasti v kole:</strong></div>
<div class="col-sm-2">
@@ -20,7 +21,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 +33,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