Skip to content

Commit

Permalink
přidána infrastruktura pro přepisování udajů dárce
Browse files Browse the repository at this point in the history
 - přidána tabulka donors_override
 - přidán formulář pro obsluhu tabulky

Záznamy z tabulky donors_override se zatím neprojeví v donors_overview.
  • Loading branch information
j-jzk committed May 25, 2021
1 parent 554290b commit c0946c8
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 4 deletions.
33 changes: 33 additions & 0 deletions migrations/versions/60a681c463a2_create_donors_override.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""create_donors_override
Revision ID: 60a681c463a2
Revises: 6fe71f4f1aba
Create Date: 2021-05-25 12:50:58.779359
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "60a681c463a2"
down_revision = "6fe71f4f1aba"
branch_labels = None
depends_on = None


def upgrade():
op.create_table(
"donors_override",
sa.Column("rodne_cislo", sa.CHAR(length=10), nullable=False),
sa.Column("first_name", sa.String(), nullable=True),
sa.Column("last_name", sa.String(), nullable=True),
sa.Column("address", sa.String(), nullable=True),
sa.Column("city", sa.String(), nullable=True),
sa.Column("postal_code", sa.CHAR(length=5), nullable=True),
sa.Column("kod_pojistovny", sa.CHAR(length=3), nullable=True),
sa.PrimaryKeyConstraint("rodne_cislo"),
)


def downgrade():
op.drop_table("donors_override")
74 changes: 72 additions & 2 deletions registry/donor/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from flask_wtf import FlaskForm
from wtforms import BooleanField, HiddenField, TextAreaField
from wtforms import BooleanField, HiddenField, StringField, TextAreaField
from wtforms.validators import DataRequired

from registry.donor.models import AwardedMedals
from registry.donor.models import AwardedMedals, DonorsOverride


class RemoveMedalForm(FlaskForm):
Expand Down Expand Up @@ -37,3 +37,73 @@ def add_one_rodne_cislo(self, rodne_cislo):
class NoteForm(FlaskForm):
rodne_cislo = HiddenField(validators=[DataRequired()])
note = TextAreaField("Poznámka k dárci")


class DonorsOverrideForm(FlaskForm):
rodne_cislo = StringField("Rodné číslo", validators=[DataRequired()])
first_name = StringField("Jméno")
last_name = StringField("Příjmení")
address = StringField("Adresa")
city = StringField("Město")
postal_code = StringField("PSČ")
kod_pojistovny = StringField("Pojišťovna")

_fields_ = [
"rodne_cislo",
"first_name",
"last_name",
"address",
"city",
"postal_code",
"kod_pojistovny",
]

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

valid = True

if self.postal_code.data:
self.postal_code.data = self.postal_code.data.replace(" ", "")
if not self.postal_code.data.isdigit():
self.postal_code.errors.append(
"PSČ může obsahovat pouze číslice a mezeru"
)
valid = False
if len(self.postal_code.data) != 5:
self.postal_code.errors.append("PSČ musí mít 5 znaků kromě mezer")
valid = False

if self.kod_pojistovny.data:
if not self.kod_pojistovny.data.isdigit():
self.kod_pojistovny.errors.append("Kód pojišťovny musí být číslo")
valid = False
if len(self.kod_pojistovny.data) != 3:
self.kod_pojistovny.errors.append("Kód pojišťovny musí být třímístný")
valid = False

self._get_field_data()

return valid

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

if override is not None:
for field in self._fields_:
data = getattr(override, field)
if data is not None:
getattr(self, field).data = data

self.rodne_cislo.data = rodne_cislo

return self

def _get_field_data(self):
self.field_data = {}
for field in self._fields_:
data = getattr(self, field).data
if data != "":
self.field_data[field] = data
11 changes: 11 additions & 0 deletions registry/donor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,14 @@ class Note(db.Model):
__tablename__ = "notes"
rodne_cislo = db.Column(db.String(10), primary_key=True)
note = db.Column(db.Text)


class DonorsOverride(db.Model):
__tablename__ = "donors_override"
rodne_cislo = db.Column(db.String(10), primary_key=True)
first_name = db.Column(db.String)
last_name = db.Column(db.String)
address = db.Column(db.String)
city = db.Column(db.String)
postal_code = db.Column(db.String(5))
kod_pojistovny = db.Column(db.String(3))
39 changes: 37 additions & 2 deletions registry/donor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@

from registry.extensions import db
from registry.list.models import DonationCenter, Medals
from registry.utils import flash_errors

from .forms import AwardMedalForm, NoteForm, RemoveMedalForm
from .models import AwardedMedals, DonorsOverview, Note, Record
from .forms import (
AwardMedalForm,
DonorsOverrideForm,
NoteForm,
RemoveMedalForm,
)
from .models import AwardedMedals, DonorsOverride, DonorsOverview, Note, Record

blueprint = Blueprint("donor", __name__, static_folder="../static")

Expand Down Expand Up @@ -79,6 +85,8 @@ def detail(rc):
note_form = NoteForm()
if overview.note:
note_form.note.data = overview.note.note
donors_override_form = DonorsOverrideForm().init_fields(rc)

return render_template(
"donor/detail.html",
overview=overview,
Expand All @@ -89,6 +97,7 @@ def detail(rc):
remove_medal_form=remove_medal_form,
award_medal_form=award_medal_form,
note_form=note_form,
donors_override_form=donors_override_form,
)


Expand Down Expand Up @@ -172,3 +181,29 @@ def save_note():
db.session.commit()
flash("Poznámka uložena.", "success")
return redirect(url_for("donor.detail", rc=note_form.rodne_cislo.data))


@blueprint.post("/override")
@login_required
def save_override():
override_form = DonorsOverrideForm()
delete = True if "delete_btn" in request.form else False

if override_form.validate_on_submit():
if not delete:
override = DonorsOverride(**override_form.field_data)
db.session.add(override)
db.session.commit()
flash("Výjimka uložena", "success")
else:
override = DonorsOverride.query.get(override_form.rodne_cislo.data)
if override is not None:
db.session.delete(override)
db.session.commit()
flash("Výjimka smazána", "success")
else:
flash("Není co mazat", "warning")
else:
flash_errors(override_form)

return redirect(url_for("donor.detail", rc=override_form.rodne_cislo.data))
33 changes: 33 additions & 0 deletions registry/templates/donor/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ <h2>Poznámky</h2>
{% endwith %}
</div>
</div>

<h2>Přepsání údajů</h2>
{% with form=donors_override_form %}
<form id="donorsOverrideForm" action="{{ url_for('donor.save_override') }}" method="POST">
{{ form.csrf_token }}
<table class="table table-striped table-hovered table-hover">
<thead class="thead-dark">
<th>Rodné číslo</th>
<th>Jméno</th>
<th>Příjmení</th>
<th>Adresa</th>
<th>Město</th>
<th>PSČ</th>
<th>Pojišťovna</th>
<th></th>
</thead>
<tbody>
<td>{{ form.rodne_cislo(readonly=True, size=8, class_="form-control") }}</td>
<td>{{ form.first_name(size=8, class_="form-control") }}</td>
<td>{{ form.last_name(size=8, class_="form-control") }}</td>
<td>{{ form.address(size=10, class_="form-control") }}</td>
<td>{{ form.city(size=8, class_="form-control") }}</td>
<td>{{ form.postal_code(size=3, class_="form-control") }}</td>
<td>{{ form.kod_pojistovny(size=3, class_="form-control") }}</td>
<td>
<button name="save_btn" class="btn btn-success" type="submit" title="Uložit výjimku"></button>
<button name="delete_btn" class="btn btn-danger" type="submit" title="Smazat výjimku">🗑</button>
</td>
</tbody>
</table>
</form>
{% endwith %}

<h2>Historie importů</h2>

<table id="records" class="table table-striped table-hovered table-hover">
Expand Down

0 comments on commit c0946c8

Please sign in to comment.