From efbaf39e97179565fcc666721fdde0e885100087 Mon Sep 17 00:00:00 2001 From: ozdemirozcelik Date: Wed, 5 Apr 2023 11:56:09 -0700 Subject: [PATCH] v4.1 release (minor updates) --- README.md | 11 +++-- demo.py | 1 - email_notifications.py | 81 ------------------------------------ models/signals.py | 3 +- notify.py | 3 -- tests/test_models_signals.py | 2 +- 6 files changed, 11 insertions(+), 90 deletions(-) delete mode 100644 email_notifications.py diff --git a/README.md b/README.md index 294a753..a914848 100644 --- a/README.md +++ b/README.md @@ -164,16 +164,15 @@ flask run $env:FLASK_APP = "app.py" $env:FLASK_ENV = "development" $env:FLASK_DEBUG = "1" -(below is optional) -$env:DB_ADMIN_PASS = "YOUR_ADMIN_PASSWORD" flask run ```` browse to "http://127.0.0.1:5000/" to see the dashboard. -you can also define an admin password during the initial creation of the database: +you can also define an admin password & passphrase during the initial creation of the database: ```` (below is optional) $env:DB_ADMIN_PASS = "YOUR_ADMIN_PASSWORD" +$env:WEBHOOK_PASSPHRAASE = "YOUR_PASSPHRASE" flask run ```` @@ -216,6 +215,12 @@ need a passphrase, by default it is set as 'webhook'; check config.ini: WEBHOOK_PASSPHRASE : webhook ``` +API looks for a WEBHOOK_PASSPHRASE environment variable first during signal creation/update: + +```python +PASSPHRASE = os.environ.get("WEBHOOK_PASSPHRASE", configs.get("SECRET", "WEBHOOK_PASSPHRASE")) +``` + ### default admin and user is created during database creation; check config.ini: diff --git a/demo.py b/demo.py index 533fc74..8791094 100644 --- a/demo.py +++ b/demo.py @@ -696,4 +696,3 @@ def init_scheduler(): ) scheduler.start() - # sched.shutdown() diff --git a/email_notifications.py b/email_notifications.py deleted file mode 100644 index c1101af..0000000 --- a/email_notifications.py +++ /dev/null @@ -1,81 +0,0 @@ -### EMAIL NOTIFICATIONS ### - -from flask_mail import Mail, Message -from app import app -from app import iftoday, timediff, configs -from models.signals import SignalModel - -# Passphrase is required to register webhooks (& to update account positions & PNL) -PASSPHRASE = configs.get("SECRET", "WEBHOOK_PASSPHRASE") - -# configuration of email -app.config["MAIL_SERVER"] = configs.get("EMAIL", "MAIL_SERVER") -app.config["MAIL_PORT"] = int(configs.get("EMAIL", "MAIL_PORT")) -app.config["MAIL_USERNAME"] = configs.get("EMAIL", "MAIL_USERNAME") -app.config["MAIL_PASSWORD"] = configs.get("EMAIL", "MAIL_PASSWORD") -app.config["MAIL_USE_TLS"] = configs.getboolean("EMAIL", "MAIL_USE_TLS") -app.config["MAIL_USE_SSL"] = configs.getboolean("EMAIL", "MAIL_USE_SSL") - -email_subject = configs.get("EMAIL", "MAIL_SUBJECT") -email_body = configs.get("EMAIL", "MAIL_BODY") -email_sender = configs.get("EMAIL", "MAIL_SENDER") -email_recipient = configs.get("EMAIL", "MAIL_RECIPIENT") - -mail = Mail(app) # instantiate the mail class - - -def get_waiting_time(signal_to_check): - - send_email = False - warned = False - print("*** checking to warn ***") - - if signal_to_check: - if iftoday(str(signal_to_check.timestamp)): - diff = timediff(str(signal_to_check.timestamp)) - - if signal_to_check.error_msg: - if "(warned)" in signal_to_check.error_msg: - warned = True - - if int(diff) > 2 and not warned: - print("*** SENDING EMAIL ****") - send_email = True - - else: - print("*** nothing to warn ***") - - return send_email - - -def warning_email_context(): - - with app.app_context(): # being executed through a new thread outside the app context and the Mail object need this - try: - - signal_to_check = SignalModel.check_timestamp() - - send_email = get_waiting_time(signal_to_check) - - if send_email: - msg = Message( - email_subject, sender=email_sender, recipients=[email_recipient] - ) - msg.body = email_body - mail.send(msg) - print("*** warning email sent...***") - - if signal_to_check.error_msg: - signal_to_check.error_msg = signal_to_check.error_msg + "(warned)" - else: - signal_to_check.error_msg = "(warned)" - - signal_to_check.update(signal_to_check.rowid) - - else: - - print("*** no email ***") - - except Exception as e: - print("*** Email Notification Error ***") - print(e) diff --git a/models/signals.py b/models/signals.py index 2cc1b8f..6ac463d 100644 --- a/models/signals.py +++ b/models/signals.py @@ -1,3 +1,4 @@ +import os import re from typing import Dict, List, Union # for type hinting from db import db @@ -14,7 +15,7 @@ SignalJSON = Dict[str, Union[str, float, int]] # custom type hint # Passphrase is required to register webhooks (& to update account positions & PNL) -PASSPHRASE = configs.get("SECRET", "WEBHOOK_PASSPHRASE") +PASSPHRASE = os.environ.get("WEBHOOK_PASSPHRASE", configs.get("SECRET", "WEBHOOK_PASSPHRASE")) class SignalModel(db.Model): diff --git a/notify.py b/notify.py index c5d0c6f..f69a3be 100644 --- a/notify.py +++ b/notify.py @@ -7,9 +7,6 @@ from demo import iftoday, timediff from models.signals import SignalModel -# Passphrase is required to register webhooks (& to update account positions & PNL) -PASSPHRASE = configs.get("SECRET", "WEBHOOK_PASSPHRASE") - # configuration of email app.config["MAIL_SERVER"] = configs.get("EMAIL", "MAIL_SERVER") app.config["MAIL_PORT"] = int(configs.get("EMAIL", "MAIL_PORT")) diff --git a/tests/test_models_signals.py b/tests/test_models_signals.py index be1aced..efab20f 100644 --- a/tests/test_models_signals.py +++ b/tests/test_models_signals.py @@ -147,7 +147,7 @@ def test_passphrase_wrong(self): phrase = "wrong" self.assertTrue(SignalModel.passphrase_wrong(phrase)) - default_phrase = configs.get("SECRET", "WEBHOOK_PASSPHRASE") + default_phrase = os.environ.get("WEBHOOK_PASSPHRASE", configs.get("SECRET", "WEBHOOK_PASSPHRASE")) self.assertFalse(SignalModel.passphrase_wrong(default_phrase)) def test_insert_signal(self):