From 7ed933da9c7eb2d2977d0baf89e6029c69f93f0c Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 14 Mar 2020 13:16:36 +0100 Subject: [PATCH 1/8] membership data model in own class --- membership_data_model.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 membership_data_model.py diff --git a/membership_data_model.py b/membership_data_model.py new file mode 100644 index 0000000..e9c8fec --- /dev/null +++ b/membership_data_model.py @@ -0,0 +1,19 @@ +class membership_data_model(): + def __init__(self, membership_data_dict): + self.membership_data_dict = membership_data_dict + self.forms = {} + + def add_edit_fields(self, parent_form): + self.forms['status'] = parent_form.add(npyscreen.TitleText, name="Status: ", value=mem['status']) + self.forms['von'] = parent_form.add(npyscreen.TitleText, name="Von: ", value=mem['von']) + self.forms['bis'] = parent_form.add(npyscreen.TitleText, name="Bis: ", value=mem['bis']) + self.forms['mitgliedsbeitrag'] = parent_form.add(npyscreen.TitleText, name="Mitgliedsbeitrag: ", + value=mem['mitgliedsbeitrag']) + self.forms['schliessberechtigung'] = parent_form.add(npyscreen.TitleText, name="Schliessberechtigung: ", + value=mem['schliessberechtigung']) + self.forms['scan-antrag'] = parent_form.add(npyscreen.TitleText, name="URI Scan Antrag: ", + value=mem['scan-antrag']) + + def get_form_values_dict(self): + membership_data_model = dict(zip(app.mem2.keys(), map(lambda f: f.value, app.mem2.values()))) + return membership_data_model \ No newline at end of file From 363c710a8173cf76bb745c72f81c1fe51bc82b89 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 14 Mar 2020 15:03:37 +0100 Subject: [PATCH 2/8] add data models --- base_data_model.py | 35 +++++++++++++++++++++++++++++++++++ financial_data_model.py | 25 +++++++++++++++++++++++++ membership_data_model.py | 12 ++++++++++-- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 base_data_model.py create mode 100644 financial_data_model.py diff --git a/base_data_model.py b/base_data_model.py new file mode 100644 index 0000000..1951e77 --- /dev/null +++ b/base_data_model.py @@ -0,0 +1,35 @@ +import npyscreen + +class base_data_model(): + def __init__(self, base_data_dict): + self.base_data_dict = base_data_dict + self.forms = {} + + def add_edit_fields(self, form): + base_data = self.base_data_dict + self.forms['fullname'] = form.add(npyscreen.TitleText, name="Name: ", value=base_data['fullname']) + self.forms['nickname'] = form.add(npyscreen.TitleText, name="Nickname: ", + value=base_data['nickname']) + self.forms['address_street'] = form.add( + npyscreen.TitleText, name="Adresse - Straße: ", value=base_data['address_street']) + self.forms['address_locality'] = form.add( + npyscreen.TitleText, name="Adresse - Stadt: ", value=base_data['address_locality']) + self.forms['address_code'] = form.add(npyscreen.TitleText, + name="Adresse - PLZ: ", value=base_data['address_code']) + self.forms['address_country'] = form.add( + npyscreen.TitleText, name="Adresse - Land: ", value=base_data['address_country']) + self.forms['birth_date'] = form.add(npyscreen.TitleText, + name="Geburtstag: ", value=base_data['birth_date']) + self.forms['birth_location'] = form.add( + npyscreen.TitleText, name="Geburtsort: ", value=base_data['birth_location']) + self.forms['email'] = form.add(npyscreen.TitleText, + name="email: ", value=base_data['email']) + self.forms['pgp-key'] = form.add(npyscreen.TitleText, name="URI PGP Key: ", value=base_data['pgp-key']) + self.forms['ssh-key'] = form.add(npyscreen.TitleText, name="URI SSH Key: ", value=base_data['ssh-key']) + + def get_form_values_dict(self): + base_data = dict(zip(self.forms.keys(), map(lambda f: f.value, self.forms.values()))) + return base_data + + def has_changed(self): + return self.base_data_dict != self.get_form_values_dict() \ No newline at end of file diff --git a/financial_data_model.py b/financial_data_model.py new file mode 100644 index 0000000..3773b90 --- /dev/null +++ b/financial_data_model.py @@ -0,0 +1,25 @@ +import npyscreen + +class financial_data_model(): + def __init__(self, financial_data_dict): + self.financial_data_dict = financial_data_dict + self.forms = {} + + def add_edit_fields(self, form): + fin = self.financial_data_dict + self.forms['iban'] = form.add(npyscreen.TitleText, name="IBAN: ", value=fin['iban']) + self.forms['bic'] = form.add(npyscreen.TitleText, name="BIC: ", value=fin['bic']) + self.forms['holder'] = form.add(npyscreen.TitleText, name="Kontoinhaber: ", value=fin['holder']) + self.forms['issuance'] = form.add(npyscreen.TitleText, name="Tag der Erteilung Lastschriftmandat: ", + value=fin['issuance']) + self.forms['reference'] = form.add(npyscreen.TitleText, name="Mandatsreferenznummer: ", + value=fin['reference']) + self.forms['scan-sepa-mandate'] = form.add(npyscreen.TitleText, name="URI Scan SEPA-Mandat: ", + value=fin['scan-sepa-mandate']) + + def get_form_values_dict(self): + financial_data_model = dict(zip(self.forms.keys(), map(lambda f: f.value, self.forms.values()))) + return financial_data_model + + def has_changed(self): + return self.financial_data_dict != self.get_form_values_dict() \ No newline at end of file diff --git a/membership_data_model.py b/membership_data_model.py index e9c8fec..800f2b2 100644 --- a/membership_data_model.py +++ b/membership_data_model.py @@ -1,19 +1,27 @@ +import npyscreen + class membership_data_model(): def __init__(self, membership_data_dict): self.membership_data_dict = membership_data_dict self.forms = {} def add_edit_fields(self, parent_form): + mem = self.membership_data_dict self.forms['status'] = parent_form.add(npyscreen.TitleText, name="Status: ", value=mem['status']) self.forms['von'] = parent_form.add(npyscreen.TitleText, name="Von: ", value=mem['von']) self.forms['bis'] = parent_form.add(npyscreen.TitleText, name="Bis: ", value=mem['bis']) self.forms['mitgliedsbeitrag'] = parent_form.add(npyscreen.TitleText, name="Mitgliedsbeitrag: ", value=mem['mitgliedsbeitrag']) + self.forms['spendenbeitrag'] = parent_form.add(npyscreen.TitleText, name="Spendenbeitrag: ", + value=mem['spendenbeitrag']) self.forms['schliessberechtigung'] = parent_form.add(npyscreen.TitleText, name="Schliessberechtigung: ", value=mem['schliessberechtigung']) self.forms['scan-antrag'] = parent_form.add(npyscreen.TitleText, name="URI Scan Antrag: ", value=mem['scan-antrag']) def get_form_values_dict(self): - membership_data_model = dict(zip(app.mem2.keys(), map(lambda f: f.value, app.mem2.values()))) - return membership_data_model \ No newline at end of file + membership_data_model = dict(zip(self.forms.keys(), map(lambda f: f.value, self.forms.values()))) + return membership_data_model + + def has_changed(self): + return self.membership_data_dict != self.get_form_values_dict() \ No newline at end of file From b73b51ee50568ec5d0d04ed29c7bb96e1e0b497d Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 14 Mar 2020 15:03:53 +0100 Subject: [PATCH 3/8] use new datamodels in base form --- edit_data_form.py | 91 +++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 55 deletions(-) diff --git a/edit_data_form.py b/edit_data_form.py index 4914660..729310e 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -6,63 +6,46 @@ import json import datetime from sys import argv +from base_data_model import base_data_model +from financial_data_model import financial_data_model +from membership_data_model import membership_data_model + class FormApp(npyscreen.NPSAppManaged): def __init__(self, member_struct): super().__init__() self.member = member_struct + self.base_data = base_data_model(member_struct['stammdaten']) + self.financial_data = financial_data_model(member_struct['finanzdaten']) + self.membership_data = membership_data_model(member_struct['mitgliederdaten']) + def main(self): - f = npyscreen.Form(name='Edit Member data') - - sd = self.member['stammdaten'] - self.sd2 = {} - self.sd2['fullname'] = f.add(npyscreen.TitleText, name="Name: ", value=sd['fullname']) - self.sd2['nickname'] = f.add(npyscreen.TitleText, name="Nickname: ", - value=sd['nickname']) - self.sd2['address_street'] = f.add( - npyscreen.TitleText, name="Adresse - Straße: ", value=sd['address_street']) - self.sd2['address_locality'] = f.add( - npyscreen.TitleText, name="Adresse - Stadt: ", value=sd['address_locality']) - self.sd2['address_code'] = f.add(npyscreen.TitleText, - name="Adresse - PLZ: ", value=sd['address_code']) - self.sd2['address_country'] = f.add( - npyscreen.TitleText, name="Adresse - Land: ", value=sd['address_country']) - self.sd2['birth_date'] = f.add(npyscreen.TitleText, - name="Geburtstag: ", value=sd['birth_date']) - self.sd2['birth_location'] = f.add( - npyscreen.TitleText, name="Geburtsort: ", value=sd['birth_location']) - self.sd2['email'] = f.add(npyscreen.TitleText, - name="email: ", value=sd['email']) - self.sd2['pgp-key'] = f.add(npyscreen.TitleText, name="URI PGP Key: ", value=sd['pgp-key']) - self.sd2['ssh-key'] = f.add(npyscreen.TitleText, name="URI SSH Key: ", value=sd['ssh-key']) + f = npyscreen.Form(name='Edit Member data') - fin = self.member['finanzdaten'] - self.fin2 = {} - self.fin2['iban'] = f.add(npyscreen.TitleText, name="IBAN: ", value=fin['iban']) - self.fin2['bic'] = f.add(npyscreen.TitleText, name="BIC: ", value=fin['bic']) - self.fin2['holder'] = f.add(npyscreen.TitleText, name="Kontoinhaber: ", value=fin['holder']) - self.fin2['issuance'] = f.add(npyscreen.TitleText, name="Tag der Erteilung Lastschriftmandat: ", - value=fin['issuance']) - self.fin2['reference'] = f.add(npyscreen.TitleText, name="Mandatsreferenznummer: ", - value=fin['reference']) - self.fin2['scan-sepa-mandate'] = f.add(npyscreen.TitleText, name="URI Scan SEPA-Mandat: ", - value=fin['scan-sepa-mandate']) - - mem = self.member['mitgliederdaten'] - self.mem2 = {} - self.mem2['status'] = f.add(npyscreen.TitleText, name="Status: ", value=mem['status']) - self.mem2['von'] = f.add(npyscreen.TitleText, name="Von: ", value=mem['von']) - self.mem2['bis'] = f.add(npyscreen.TitleText, name="Bis: ", value=mem['bis']) - self.mem2['mitgliedsbeitrag'] = f.add(npyscreen.TitleText, name="Mitgliedsbeitrag: ", - value=mem['mitgliedsbeitrag']) - self.mem2['schliessberechtigung'] = f.add(npyscreen.TitleText, name="Schliessberechtigung: ", - value=mem['schliessberechtigung']) - self.mem2['scan-antrag'] = f.add(npyscreen.TitleText, name="URI Scan Antrag: ", - value=mem['scan-antrag']) + self.base_data.add_edit_fields(f) + self.financial_data.add_edit_fields(f) + self.membership_data.add_edit_fields(f) f.edit() + def get_data_from_form(self): + data_dict = {} + data_dict['stammdaten'] = self.base_data.get_form_values_dict() + data_dict['finanzdaten'] = self.financial_data.get_form_values_dict() + data_dict['mitgliederdaten'] = self.membership_data.get_form_values_dict() + data_dict['timestamp'] = datetime.datetime.now().replace(microsecond=0).isoformat() + return data_dict + + def data_has_changed(self): + print(f"{self.base_data.has_changed()} | {self.financial_data.has_changed()} | {self.membership_data.has_changed()}") + return self.base_data.has_changed() | self.financial_data.has_changed() | self.membership_data.has_changed() + + def get_filename(self): + name = self.base_data.get_form_values_dict()['fullname'] + fn = '{}_{}.json'.format('_'.join(name.split()), datetime.datetime.now().replace(microsecond=0).isoformat()) + return fn + def main(*_args): if len(argv) > 1: @@ -71,15 +54,13 @@ def main(*_args): member_struct = json.load(open('example.json', 'r')) app = FormApp(member_struct) app.run() - new_data = {} - new_data['stammdaten'] = dict(zip(app.sd2.keys(), map(lambda f: f.value, app.sd2.values()))) - new_data['finanzdaten'] = dict(zip(app.fin2.keys(), map(lambda f: f.value, app.fin2.values()))) - new_data['mitgliederdaten'] = dict(zip(app.mem2.keys(), map(lambda f: f.value, app.mem2.values()))) - new_data['timestamp'] = datetime.datetime.now().replace(microsecond=0).isoformat() - - outfile_name = '{}_{}.json'.format('_'.join(new_data['stammdaten']['fullname'].split()),datetime.datetime.now().replace(microsecond=0).isoformat()) - with open(outfile_name, 'w') as outfile: - json.dump(new_data, outfile, sort_keys=True, indent=4) + if app.data_has_changed(): + out_data = app.get_data_from_form() + outfile_name = app.get_filename() + with open(outfile_name, 'w') as outfile: + json.dump(out_data, outfile, sort_keys=True, indent=4) + else: + print("Nothing has changed") if __name__ == "__main__": From 029b9c2c22208290e683275fcb7d31288bf83ea2 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 14 Mar 2020 15:04:07 +0100 Subject: [PATCH 4/8] add __pycache__ to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index eba74f4..8bc348e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -venv/ \ No newline at end of file +venv/ +__pycache__ \ No newline at end of file From 028b4b1c72e000dff5827e226fceca3906b3c1fe Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 14 Mar 2020 15:12:11 +0100 Subject: [PATCH 5/8] add empty string for unsed field --- base_data_model.py | 1 + 1 file changed, 1 insertion(+) diff --git a/base_data_model.py b/base_data_model.py index 1951e77..1370662 100644 --- a/base_data_model.py +++ b/base_data_model.py @@ -29,6 +29,7 @@ class base_data_model(): def get_form_values_dict(self): base_data = dict(zip(self.forms.keys(), map(lambda f: f.value, self.forms.values()))) + base_data['address_region'] = '' return base_data def has_changed(self): From 520edd22aa000a371eb5b4570db4757fc231dc6b Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 14 Mar 2020 15:12:41 +0100 Subject: [PATCH 6/8] base_data_model: add address_label composition --- base_data_model.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/base_data_model.py b/base_data_model.py index 1370662..464fe57 100644 --- a/base_data_model.py +++ b/base_data_model.py @@ -29,6 +29,10 @@ class base_data_model(): def get_form_values_dict(self): base_data = dict(zip(self.forms.keys(), map(lambda f: f.value, self.forms.values()))) + base_data['address_label'] = \ + f"{base_data['fullname']}\n\ +{base_data['address_street']}\n\ +{base_data['address_code']} {base_data['address_locality']}" base_data['address_region'] = '' return base_data From 52d98488f9f224bed303c5d8eed179ed63fc3cf9 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 14 Mar 2020 15:16:15 +0100 Subject: [PATCH 7/8] data_form: remove debug output --- edit_data_form.py | 1 - 1 file changed, 1 deletion(-) diff --git a/edit_data_form.py b/edit_data_form.py index 729310e..8cc5701 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -38,7 +38,6 @@ class FormApp(npyscreen.NPSAppManaged): return data_dict def data_has_changed(self): - print(f"{self.base_data.has_changed()} | {self.financial_data.has_changed()} | {self.membership_data.has_changed()}") return self.base_data.has_changed() | self.financial_data.has_changed() | self.membership_data.has_changed() def get_filename(self): From 464c74866a194f34e812fa4b832b85e3a39e7473 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 14 Mar 2020 15:16:53 +0100 Subject: [PATCH 8/8] data_form: add clear message regarding data changes --- edit_data_form.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/edit_data_form.py b/edit_data_form.py index 8cc5701..52bd943 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -56,10 +56,11 @@ def main(*_args): if app.data_has_changed(): out_data = app.get_data_from_form() outfile_name = app.get_filename() + print(f"Writing changed Member Data to {outfile_name}") with open(outfile_name, 'w') as outfile: json.dump(out_data, outfile, sort_keys=True, indent=4) else: - print("Nothing has changed") + print("Nothing has changed, skipping writing file") if __name__ == "__main__":