Skip to content

Commit

Permalink
znovu upravena struktura formuláře donors_override
Browse files Browse the repository at this point in the history
V HTML byl formulář zachován jako flexbox, ale formuláře pro uložení a
smazání výjimky byly sloučeny zpět do jednoho (WebTest si neuměl poradit
s formulářovými prvky, které jsou mimo formulář a jsou s ním propojené
přes atribut `form`).
  • Loading branch information
j-jzk committed Jun 8, 2021
1 parent 91ef1f2 commit 628097c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 63 deletions.
10 changes: 0 additions & 10 deletions registry/donor/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ class RemoveFromIgnoredForm(FlaskForm):


class DonorsOverrideForm(FlaskForm):
class Meta:
def render_field(self, field, render_kw):
# Set the HTML "form" parameter for each of the fields
render_kw["form"] = "donorsOverrideForm"
return field.widget(field, **render_kw)

rodne_cislo = StringField("Rodné číslo", validators=[DataRequired()])
first_name = StringField("Jméno")
last_name = StringField("Příjmení")
Expand Down Expand Up @@ -124,7 +118,3 @@ def _get_field_data(self):
self.field_data[field] = data
else:
self.field_data[field] = None


class DonorsOverrideDeleteForm(FlaskForm):
rodne_cislo = HiddenField(validators=[DataRequired()])
46 changes: 19 additions & 27 deletions registry/donor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from .forms import (
AwardMedalForm,
DonorsOverrideDeleteForm,
DonorsOverrideForm,
IgnoreDonorForm,
NoteForm,
Expand Down Expand Up @@ -104,7 +103,6 @@ def detail(rc):
if overview.note:
note_form.note.data = overview.note.note
donors_override_form = DonorsOverrideForm().init_fields(rc)
donors_override_delete_form = DonorsOverrideDeleteForm()

return render_template(
"donor/detail.html",
Expand All @@ -117,7 +115,6 @@ def detail(rc):
award_medal_form=award_medal_form,
note_form=note_form,
donors_override_form=donors_override_form,
donors_override_delete_form=donors_override_delete_form,
)


Expand Down Expand Up @@ -257,36 +254,31 @@ def unignore_donor():
@blueprint.post("/override/")
@login_required
def save_override():
override_form = DonorsOverrideForm()

if override_form.validate_on_submit():
override = DonorsOverride(**override_form.field_data)
db.session.merge(override)
db.session.commit()

DonorsOverview.refresh_overview()
flash("Výjimka uložena", "success")
else:
flash_errors(override_form)

return redirect(url_for("donor.detail", rc=override_form.rodne_cislo.data))


@blueprint.post("/override/delete")
@login_required
def delete_override():
form = DonorsOverrideDeleteForm()
form = DonorsOverrideForm()
delete = "delete_btn" in request.form

if form.validate_on_submit():
override = DonorsOverride.query.get(form.rodne_cislo.data)
if override is not None:
db.session.delete(override)
if not delete:
# Save the override
override = DonorsOverride(**form.field_data)
db.session.merge(override)
db.session.commit()

DonorsOverview.refresh_overview()
flash("Výjimka smazána", "success")
flash("Výjimka uložena", "success")
else:
flash("Není co mazat", "warning")
# Delete the override
override = DonorsOverride.query.get(form.rodne_cislo.data)
if override is not None:
db.session.delete(override)
db.session.commit()

DonorsOverview.refresh_overview()
flash("Výjimka smazána", "success")
else:
flash("Není co mazat", "warning")
else:
flash_errors(form)

return redirect(url_for("donor.detail", rc=form.rodne_cislo.data))

Expand Down
39 changes: 15 additions & 24 deletions registry/templates/donor/detail.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{% extends "layout.html" %}
{% block css %}
<style>
#override_forms {
#donorsOverrideForm {
display: flex;
flex-wrap: wrap;
}

#override_forms div {
#donorsOverrideForm div {
background-color: #0000000d;
flex-grow: 1;
margin-top: 12px;
}

#override_forms label {
#donorsOverrideForm label {
color: white;
background-color: #343a40;
font-weight: bold;
Expand All @@ -21,18 +21,18 @@
padding: 12px;
}

#override_forms input {
#donorsOverrideForm input {
margin: 6px;
width: calc(100% - 12px);
}

#override_forms .btn_container {
#donorsOverrideForm .btn_container {
flex-grow: unset;
}
#override_forms .btn_container label {
#donorsOverrideForm .btn_container label {
padding: 12px 0;
}
#override_forms .btn_container button {
#donorsOverrideForm .btn_container button {
margin: 6px 0;
}
</style>
Expand Down Expand Up @@ -128,7 +128,8 @@ <h2>Poznámky</h2>

<h2>Přepsání údajů</h2>
{% with form=donors_override_form %}
<div id="override_forms">
<form id="donorsOverrideForm" action="/override/" method="POST">
{{ form.csrf_token }}
<div style="flex-grow: 8;">
<label for="rodne_cislo">Rodné číslo</label>
{{ form.rodne_cislo(readonly=True, size=8, class_="form-control") }}
Expand Down Expand Up @@ -159,26 +160,16 @@ <h2>Přepsání údajů</h2>
</div>

<div class="btn_container">
<form id="donorsOverrideForm" action="{{ url_for('donor.save_override') }}" method="POST">
<label>&nbsp;</label>

{{ form.csrf_token }}
<button class="btn btn-success" type="submit" title="Uložit výjimku"></button>
</form>
<label>&nbsp;</label>
<button class="btn btn-success" type="submit" name="save_btn" title="Uložit výjimku"></button>
</div>
{% endwith %}
{% with form=donors_override_delete_form %}
<div class="btn_container">
<form id="donorsOverrideDeleteForm" action="{{ url_for('donor.delete_override') }}" method="POST">
<label>&nbsp;</label>

{{ form.csrf_token }}
{{ form.rodne_cislo(value=overview.rodne_cislo) }}
<button class="btn btn-danger" type="submit" title="Smazat výjimku">🗑</button>
</form>
<div class="btn_container">
<label>&nbsp;</label>
<button class="btn btn-danger" type="submit" name="delete_btn" title="Smazat výjimku">🗑</button>
</div>
</form>
{% endwith %}
</div>


<h2>Historie importů</h2>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,8 @@ def test_override(self, user, testapp, rodne_cislo):
assert "Příjmení: --Last--" in res

# Test deleting the override
form = res.forms["donorsOverrideDeleteForm"]
res = form.submit().follow()
form = res.forms["donorsOverrideForm"]
res = form.submit("delete_btn").follow()

assert "Výjimka smazána" in res
assert ("Jméno: " + old_first_name) in res
Expand Down

0 comments on commit 628097c

Please sign in to comment.