2020-03-06 23:37:21 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
2022-10-24 23:58:39 +02:00
|
|
|
import os
|
2020-03-06 23:37:21 +01:00
|
|
|
import npyscreen
|
|
|
|
import json
|
2020-03-14 12:40:15 +01:00
|
|
|
import datetime
|
2020-03-22 21:35:36 +01:00
|
|
|
import sys
|
2020-03-18 19:31:02 +01:00
|
|
|
import pathlib
|
2020-03-06 23:37:21 +01:00
|
|
|
|
2020-03-14 15:03:53 +01:00
|
|
|
from base_data_model import base_data_model
|
|
|
|
from financial_data_model import financial_data_model
|
|
|
|
from membership_data_model import membership_data_model
|
2020-08-23 18:47:35 +02:00
|
|
|
from id import new_id
|
2020-03-14 15:03:53 +01:00
|
|
|
|
2020-03-06 23:37:21 +01:00
|
|
|
|
|
|
|
class FormApp(npyscreen.NPSAppManaged):
|
2020-03-16 10:45:50 +01:00
|
|
|
def __init__(self, filename):
|
2020-03-06 23:37:21 +01:00
|
|
|
super().__init__()
|
2020-03-20 13:15:20 +01:00
|
|
|
self.is_new_member = self.is_default_file(filename)
|
2020-03-18 19:31:02 +01:00
|
|
|
self.dir_path = pathlib.Path(filename).absolute().parent
|
2020-03-16 10:45:50 +01:00
|
|
|
self.member = json.load(open(filename, 'r'))
|
|
|
|
|
|
|
|
self.base_data = base_data_model(self.member['stammdaten'])
|
|
|
|
self.financial_data = financial_data_model(self.member['finanzdaten'])
|
|
|
|
self.membership_data = membership_data_model(self.member['mitgliederdaten'])
|
2020-03-14 15:03:53 +01:00
|
|
|
|
2020-03-06 23:37:21 +01:00
|
|
|
def main(self):
|
2020-03-14 15:03:53 +01:00
|
|
|
f = npyscreen.Form(name='Edit Member data')
|
2020-03-06 23:37:21 +01:00
|
|
|
|
2020-03-14 15:03:53 +01:00
|
|
|
self.base_data.add_edit_fields(f)
|
|
|
|
self.financial_data.add_edit_fields(f)
|
|
|
|
self.membership_data.add_edit_fields(f)
|
2020-03-06 23:37:21 +01:00
|
|
|
|
|
|
|
f.edit()
|
|
|
|
|
2020-03-14 15:03:53 +01:00
|
|
|
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()
|
2020-08-23 18:47:35 +02:00
|
|
|
try:
|
|
|
|
data_dict['id'] = self.member['id']
|
|
|
|
except KeyError:
|
|
|
|
data_dict['id'] = new_id()
|
2020-03-14 15:03:53 +01:00
|
|
|
return data_dict
|
|
|
|
|
|
|
|
def data_has_changed(self):
|
|
|
|
return self.base_data.has_changed() | self.financial_data.has_changed() | self.membership_data.has_changed()
|
|
|
|
|
2020-03-16 10:45:50 +01:00
|
|
|
def get_filepath(self):
|
2020-03-14 15:03:53 +01:00
|
|
|
name = self.base_data.get_form_values_dict()['fullname']
|
2020-03-22 18:33:56 +01:00
|
|
|
date = datetime.datetime.now().replace(microsecond=0).isoformat()
|
2020-03-23 09:48:32 +01:00
|
|
|
fn = '{}_{}.json'.format('_'.join(name.lower().split()), date.replace(':',''))
|
2020-03-20 13:15:20 +01:00
|
|
|
if self.is_new_member:
|
|
|
|
print('Enter destination directory for new Member data:')
|
|
|
|
self.dir_path = input()
|
2020-03-18 19:31:02 +01:00
|
|
|
file_path = pathlib.Path(self.dir_path, fn)
|
2020-03-16 10:45:50 +01:00
|
|
|
return file_path
|
2020-03-14 15:03:53 +01:00
|
|
|
|
2020-03-20 13:15:20 +01:00
|
|
|
def is_default_file(self, filename):
|
|
|
|
return pathlib.Path(filename).name == 'example.json'
|
|
|
|
|
2020-03-06 23:37:21 +01:00
|
|
|
|
|
|
|
def main(*_args):
|
2020-03-22 21:35:36 +01:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
file_path = pathlib.Path(sys.argv[1])
|
2022-10-24 23:58:39 +02:00
|
|
|
|
|
|
|
if(sys.argv[1].endswith(".json")):
|
|
|
|
# json file as argument
|
|
|
|
|
|
|
|
if not file_path.exists():
|
|
|
|
print("Trying to open nonexistent file. Aborting.")
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
elif file_path.is_dir():
|
|
|
|
# we want the latest json file in given folder
|
|
|
|
filesList = [f for f in os.listdir(file_path) if os.path.isfile(os.path.join(file_path, f)) and f.endswith(".json")]
|
|
|
|
filesList.sort()
|
|
|
|
file_path = os.path.join(file_path, filesList[len(filesList) - 1])
|
|
|
|
|
2020-03-06 23:37:21 +01:00
|
|
|
else:
|
2020-03-22 21:35:36 +01:00
|
|
|
file_path = pathlib.Path(pathlib.Path(__file__).absolute().parent, 'example.json')
|
2020-03-16 10:45:50 +01:00
|
|
|
|
2020-03-22 21:35:36 +01:00
|
|
|
app = FormApp(file_path)
|
2020-03-14 15:19:44 +01:00
|
|
|
|
2020-03-14 13:29:02 +01:00
|
|
|
try:
|
|
|
|
app.run()
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2020-03-22 21:35:36 +01:00
|
|
|
sys.exit(0)
|
2020-03-14 15:19:44 +01:00
|
|
|
|
2020-03-14 15:03:53 +01:00
|
|
|
if app.data_has_changed():
|
|
|
|
out_data = app.get_data_from_form()
|
2020-03-16 10:45:50 +01:00
|
|
|
outfile_path = app.get_filepath()
|
|
|
|
print(f"Writing changed Member Data to {outfile_path}")
|
2020-03-26 00:05:02 +01:00
|
|
|
with open(outfile_path, 'w', encoding='utf8') as outfile:
|
|
|
|
json.dump(out_data, outfile, sort_keys=True, indent=4, ensure_ascii=False)
|
2020-03-14 15:03:53 +01:00
|
|
|
else:
|
2020-03-14 15:16:53 +01:00
|
|
|
print("Nothing has changed, skipping writing file")
|
2020-03-14 15:19:44 +01:00
|
|
|
|
2020-03-06 23:37:21 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
npyscreen.wrapper_basic(main)
|