From a1543f9a4f0d8ea08331d234f6a58fcd491f3779 Mon Sep 17 00:00:00 2001 From: NovaOSS Admins Date: Sun, 15 Oct 2023 22:51:43 +0000 Subject: [PATCH] Fixes --- requirements.txt | 7 +++++++ web/finances.py | 28 ++++++++++++++++++++++++---- web/tos.py | 2 +- 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fd4d749 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +flask +pandas +cherrypy +requests +werkzeug +python-dotenv +requests-oauthlib \ No newline at end of file diff --git a/web/finances.py b/web/finances.py index 37b88c5..611209c 100755 --- a/web/finances.py +++ b/web/finances.py @@ -36,17 +36,37 @@ def register(app): donations = data['donations'] expenses = data['expenses'] + try: + avg = round(sum([i['amount_usd'] for i in donations]) / len(donations), 2) + except ZeroDivisionError: + avg = 0 + + try: + most_donation = max(donations, key=lambda x: x['amount_usd'])['currency'] + except ValueError: + most_donation = '?' + + try: + most_expense = max(expenses, key=lambda x: x['amount_usd'])['currency'] + except ValueError: + most_expense = '?' + + try: + donations_max = round(max([i['amount_usd'] for i in donations])) + except ValueError: + donations_max = 0 + finances = { 'last_update': data['timestamp'], 'donations_total': round(sum([i['amount_usd'] for i in donations])), 'donations_num': len(donations), - 'donations_avg': round(sum([i['amount_usd'] for i in donations]) / len(donations), 2), - 'donations_max': round(max([i['amount_usd'] for i in donations])), - 'most_used_donation_currency': max(donations, key=lambda x: x['amount_usd'])['currency'], + 'donations_avg': avg, + 'donations_max': donations_max, + 'most_used_donation_currency': most_donation, 'expenses_total': round(sum([i['amount_usd'] for i in expenses])), 'expenses_wages': round(sum([i['amount_usd'] for i in expenses if i['type'] == 'wage'])), - 'most_used_expense_currency': max(expenses, key=lambda x: x['amount_usd'])['currency'], + 'most_used_expense_currency': most_expense, } transaction_table = [] diff --git a/web/tos.py b/web/tos.py index 00799c8..6dcab3f 100755 --- a/web/tos.py +++ b/web/tos.py @@ -19,8 +19,8 @@ def register(app): emoji = random.choice(open('config/emojis.txt', encoding='utf8').read()) if not os.path.exists('data/tos.json'): + os.makedirs('data', exist_ok=True) open('data/tos.json', 'w', encoding='utf8').write('{}') - try: data = ujson.load(open('data/tos.json', encoding='utf8')) except JSONDecodeError: