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__":