Skip to content

Commit

Permalink
malé upravy
Browse files Browse the repository at this point in the history
- funkce DonorsOverrideForm.init_fields nic nevrací
- mv DonorsOverrideForm.{_,}get_field_data
- funkce DonorsOverrideForm.get_field_data se chová smysluplněji
    - vrací výslednou hodnotu místo toho, aby ji ukládala
    - volá se samostatně místo toho, aby se volala automaticky při
      validaci
    - byl zkrácen její kod
- test pro DonorsOverride získává informace z databáze a ne
  regexem ze stránky
- view donor/get_overrides nevolá zbytečně str() na string
- styl pro podbarvení elementu <mark> byl přesunut na sjednocené
  místo
  • Loading branch information
j-jzk committed Aug 13, 2021
1 parent 8c2f2b4 commit 05cdc13
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 42 deletions.
22 changes: 5 additions & 17 deletions registry/donor/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ class DonorsOverrideForm(FlaskForm):
"kod_pojistovny",
]

def validate(self):
initial_validation = super(DonorsOverrideForm, self).validate()
if not initial_validation:
return False

self._get_field_data()
return True

def init_fields(self, rodne_cislo):
override = DonorsOverride.query.get(rodne_cislo)

Expand All @@ -87,13 +79,9 @@ def init_fields(self, rodne_cislo):

self.rodne_cislo.data = rodne_cislo

return self

def _get_field_data(self):
self.field_data = {}
def get_field_data(self):
field_data = {}
for field in self._fields_:
data = getattr(self, field).data
if data:
self.field_data[field] = data
else:
self.field_data[field] = None
field_data[field] = getattr(self, field).data or None

return field_data
7 changes: 4 additions & 3 deletions registry/donor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def detail(rc):
note_form = NoteForm()
if overview.note:
note_form.note.data = overview.note.note
donors_override_form = DonorsOverrideForm().init_fields(rc)
donors_override_form = DonorsOverrideForm()
donors_override_form.init_fields(rc)

return render_template(
"donor/detail.html",
Expand Down Expand Up @@ -286,7 +287,7 @@ def save_override():
if form.validate_on_submit():
if not delete:
# Save the override
override = DonorsOverride(**form.field_data)
override = DonorsOverride(**form.get_field_data())
db.session.merge(override)
db.session.commit()

Expand Down Expand Up @@ -315,6 +316,6 @@ def get_overrides():
overrides_dict = {}

for override in DonorsOverride.query.all():
overrides_dict[str(override.rodne_cislo)] = override.to_dict()
overrides_dict[override.rodne_cislo] = override.to_dict()

return jsonify(overrides_dict)
8 changes: 0 additions & 8 deletions registry/templates/donor/award_prep.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ <h1>Přehled dárců k ocenění: {{ medal.title }}</h1>

{% endblock %}

{% block css %}
<style>
mark {
background-color: #f7ebb1;
}
</style>
{% endblock %}

{% block js %}
<script src="/static/donors_override_highlight.js"></script>
<script type="text/javascript">
Expand Down
8 changes: 0 additions & 8 deletions registry/templates/donor/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ <h1>Přehled dárců</h1>

{% endblock %}

{% block css %}
<style>
mark {
background-color: #f7ebb1;
}
</style>
{% endblock %}

{% block js %}
<script src="/static/donors_override_highlight.js"></script>
<script type="text/javascript">
Expand Down
7 changes: 7 additions & 0 deletions registry/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
Evidence dárců ČČK Frýdek-Místek
{% endblock %}
</title>

<style>
mark {
background-color: #f7ebb1;
}
</style>

{% block css %}{% endblock %}
</head>
<body>
Expand Down
10 changes: 4 additions & 6 deletions tests/test_donor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from random import randint

import pytest
Expand Down Expand Up @@ -96,8 +95,7 @@ def test_override(self, user, testapp, rodne_cislo):
login(user, testapp)
res = testapp.get(url_for("donor.detail", rc=rodne_cislo))

old_first_name = re.search(r"<li>\s*Jméno: (.*)\s*</li>", str(res)).group(1)
old_last_name = re.search(r"<li>\s*Příjmení: (.*)\s*</li>", str(res)).group(1)
old_data = DonorsOverview.query.get(rodne_cislo)

# Test save
form = res.forms["donorsOverrideForm"]
Expand All @@ -123,13 +121,13 @@ def test_override(self, user, testapp, rodne_cislo):
res = form.submit("save_btn").follow()

assert "Výjimka uložena" in res
assert ("Jméno: " + old_first_name) in res
assert ("Jméno: " + str(old_data.first_name)) in res
assert "Příjmení: --Last--" in res

# Test deleting the override
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
assert ("Příjmení: " + old_last_name) in res
assert ("Jméno: " + str(old_data.first_name)) in res
assert ("Příjmení: " + str(old_data.last_name)) in res

0 comments on commit 05cdc13

Please sign in to comment.