Merge pull request 'refactor-datamodel' (#10) from refactor-datamodel into master

This commit is contained in:
Stefan Haun 2020-03-14 22:08:55 +00:00
commit 3cfb69c1b6
5 changed files with 133 additions and 84 deletions

3
.gitignore vendored
View file

@ -1 +1,2 @@
venv/ venv/
__pycache__

40
base_data_model.py Normal file
View file

@ -0,0 +1,40 @@
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())))
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
def has_changed(self):
return self.base_data_dict != self.get_form_values_dict()

View file

@ -6,75 +6,45 @@ import json
import datetime import datetime
from sys import argv 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): class FormApp(npyscreen.NPSAppManaged):
def __init__(self, member_struct): def __init__(self, member_struct):
super().__init__() super().__init__()
self.member = member_struct 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): def main(self):
f = npyscreen.Form(name='Edit Member data') f = npyscreen.Form(name='Edit Member data')
sd = self.member['stammdaten'] self.base_data.add_edit_fields(f)
base_data_widgets = {} self.financial_data.add_edit_fields(f)
base_data_widgets['fullname'] = f.add( self.membership_data.add_edit_fields(f)
npyscreen.TitleText, name="Name: ", value=sd['fullname'])
base_data_widgets['nickname'] = f.add(npyscreen.TitleText, name="Nickname: ",
value=sd['nickname'])
base_data_widgets['address_street'] = f.add(
npyscreen.TitleText, name="Adresse - Straße: ", value=sd['address_street'])
base_data_widgets['address_locality'] = f.add(
npyscreen.TitleText, name="Adresse - Stadt: ", value=sd['address_locality'])
base_data_widgets['address_code'] = f.add(npyscreen.TitleText,
name="Adresse - PLZ: ", value=sd['address_code'])
base_data_widgets['address_country'] = f.add(
npyscreen.TitleText, name="Adresse - Land: ", value=sd['address_country'])
base_data_widgets['birth_date'] = f.add(npyscreen.TitleText,
name="Geburtstag: ", value=sd['birth_date'])
base_data_widgets['birth_location'] = f.add(
npyscreen.TitleText, name="Geburtsort: ", value=sd['birth_location'])
base_data_widgets['email'] = f.add(npyscreen.TitleText,
name="email: ", value=sd['email'])
base_data_widgets['pgp-key'] = f.add(npyscreen.TitleText,
name="URI PGP Key: ", value=sd['pgp-key'])
base_data_widgets['ssh-key'] = f.add(npyscreen.TitleText,
name="URI SSH Key: ", value=sd['ssh-key'])
self.base_data_widgets = base_data_widgets
fin = self.member['finanzdaten']
finance_widgets = {}
finance_widgets['iban'] = f.add(
npyscreen.TitleText, name="IBAN: ", value=fin['iban'])
finance_widgets['bic'] = f.add(
npyscreen.TitleText, name="BIC: ", value=fin['bic'])
finance_widgets['holder'] = f.add(
npyscreen.TitleText, name="Kontoinhaber: ", value=fin['holder'])
finance_widgets['issuance'] = f.add(npyscreen.TitleText, name="Tag der Erteilung Lastschriftmandat: ",
value=fin['issuance'])
finance_widgets['reference'] = f.add(npyscreen.TitleText, name="Mandatsreferenznummer: ",
value=fin['reference'])
finance_widgets['scan-sepa-mandate'] = f.add(npyscreen.TitleText, name="URI Scan SEPA-Mandat: ",
value=fin['scan-sepa-mandate'])
self.finance_widgets = finance_widgets
mem = self.member['mitgliederdaten']
member_widgets = {}
member_widgets['status'] = f.add(
npyscreen.TitleText, name="Status: ", value=mem['status'])
member_widgets['von'] = f.add(
npyscreen.TitleText, name="Von: ", value=mem['von'])
member_widgets['bis'] = f.add(
npyscreen.TitleText, name="Bis: ", value=mem['bis'])
member_widgets['mitgliedsbeitrag'] = f.add(npyscreen.TitleText, name="Mitgliedsbeitrag: ",
value=mem['mitgliedsbeitrag'])
member_widgets['schliessberechtigung'] = f.add(npyscreen.TitleText, name="Schliessberechtigung: ",
value=mem['schliessberechtigung'])
member_widgets['scan-antrag'] = f.add(npyscreen.TitleText, name="URI Scan Antrag: ",
value=mem['scan-antrag'])
self.member_widgets = member_widgets
f.edit() 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):
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): def main(*_args):
if len(argv) > 1: if len(argv) > 1:
@ -82,36 +52,22 @@ def main(*_args):
else: else:
member_struct = json.load(open('example.json', 'r')) member_struct = json.load(open('example.json', 'r'))
app = FormApp(member_struct) app = FormApp(member_struct)
try: try:
app.run() app.run()
except Exception as e: except Exception as e:
print(e) print(e)
return 0 return 0
new_data = {} if app.data_has_changed():
new_data['stammdaten'] = dict( out_data = app.get_data_from_form()
zip(app.base_data_widgets.keys(), map(lambda f: f.value, app.base_data_widgets.values()))) outfile_name = app.get_filename()
new_data['finanzdaten'] = dict( print(f"Writing changed Member Data to {outfile_name}")
zip(app.finance_widgets.keys(), map(lambda f: f.value, app.finance_widgets.values()))) with open(outfile_name, 'w') as outfile:
new_data['mitgliederdaten'] = dict( json.dump(out_data, outfile, sort_keys=True, indent=4)
zip(app.member_widgets.keys(), map(lambda f: f.value, app.member_widgets.values()))) else:
print("Nothing has changed, skipping writing file")
new_data['stammdaten'] = dict(zip(app.base_data_widgets.keys(), map(lambda f: f.value, app.base_data_widgets.values())))
new_data['stammdaten']['address_label'] = \
f"{new_data['stammdaten']['fullname']}\n\
{new_data['stammdaten']['address_street']}\n\
{new_data['stammdaten']['address_code']} {new_data['stammdaten']['address_locality']}"
new_data['finanzdaten'] = dict(zip(app.finance_widgets.keys(), map(lambda f: f.value, app.finance_widgets.values())))
new_data['mitgliederdaten'] = dict(zip(app.member_widgets.keys(), map(lambda f: f.value, app.member_widgets.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 __name__ == "__main__": if __name__ == "__main__":
npyscreen.wrapper_basic(main) npyscreen.wrapper_basic(main)

25
financial_data_model.py Normal file
View file

@ -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()

27
membership_data_model.py Normal file
View file

@ -0,0 +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(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()