diff --git a/.travis.yml b/.travis.yml index bad0ac9..c1e969b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,4 @@ python: - "3.7" install: "pip install -r requirements.txt" # command to run tests -script: pylint **/*.py \ No newline at end of file +script: pylint **/*.py diff --git a/requirements.txt b/requirements.txt index 5ee0a20..e06b3b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ + colorama==0.4.1 paho-mqtt==1.4.0 requests==2.22.0 @@ -7,3 +8,4 @@ python-dotenv==0.10.3 pylint==2.4.2 simple-chalk==0.1.0 pre-commit==1.18.3 + diff --git a/tmessage/auth.py b/tmessage/auth.py index d1a49c7..494e2a2 100644 --- a/tmessage/auth.py +++ b/tmessage/auth.py @@ -5,7 +5,9 @@ import dotenv as env env.load_dotenv() -API_BASE_URL = os.environ.get("TMESSAGE_API_URL") or "https://peaceful-waters-15584.herokuapp.com" +API_BASE_URL_A = os.environ.get("TMESSAGE_API_URL") +API_BASE_URL_B = "https://peaceful-waters-15584.herokuapp.com" +API_BASE_URL = API_BASE_URL_B or API_BASE_URL_A API_USER_URL = f'{API_BASE_URL}/api/user' diff --git a/tmessage/cli.py b/tmessage/cli.py index f6ebd14..26e0f61 100644 --- a/tmessage/cli.py +++ b/tmessage/cli.py @@ -5,7 +5,7 @@ from colorama import init, deinit, Fore, Back, Style from simple_chalk import chalk import tmessage.auth as auth # auth.py -from tmessage.db import store_messages # db.py +from tmessage.db import database # db.py from tmessage.utils import get_formatted_message # Initialize colorama @@ -53,7 +53,7 @@ def on_message(client, userdata, message): print(user_details, msg) _, _, message = current_msg.partition("] ") if IS_STORE: - store_messages(user, message) + database(user, message, False) def main(): @@ -62,6 +62,7 @@ def main(): if auth.check_existed(CURRENT_USER): password = getpass(f"User {CURRENT_USER} found\nEnter password: ") payload = auth.authenticate(CURRENT_USER, password) + new = False else: print(f"Welcome {CURRENT_USER} to tmessage!\nPlease register...") displayed_name = input("Enter your name used for display: ") @@ -74,6 +75,7 @@ def main(): payload = auth.register( CURRENT_USER, displayed_name, password, password_confirm ) + new = True print("User Authorized") user_name = payload["user_name"] displayed_name = payload["displayed_name"] @@ -89,7 +91,7 @@ def main(): if raw_msg != "": MQTT_CLIENT.publish(MQTT_TOPIC, pub_msg) if IS_STORE: - store_messages(CURRENT_USER, formatted_msg) + database(CURRENT_USER, formatted_msg, new) else: print(Back.WHITE + Fore.RED + "Can't send empty message", end="\n") except KeyboardInterrupt: diff --git a/tmessage/db.py b/tmessage/db.py index f89901e..968c28f 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -4,25 +4,22 @@ from peewee import CharField, DateTimeField, Model, SqliteDatabase -MESSAGES_DB = SqliteDatabase('message_store.sqlite') +def database(user, raw_msg, new): + """user for sender, raw_msg for message, new if the user is new""" + MESSAGES_DB = SqliteDatabase(f'{user}.db')# pylint: disable=invalid-name -class Message(Model): - """Message table - keeps track of message sent and received""" - sender = CharField() - message = CharField() - timestamp = DateTimeField() + class Message(Model): + """Message table - keeps track of message sent and received""" + sender = CharField() + message = CharField() + timestamp = DateTimeField() - class Meta: # pylint: disable=missing-class-docstring,too-few-public-methods - database = MESSAGES_DB + class Meta: # pylint: disable=missing-class-docstring,too-few-public-methods + database = MESSAGES_DB -@MESSAGES_DB -def store_messages(user, raw_msg): - """Store a message sent by the indicated user in the database""" + if new == True: # pylint: disable=singleton-comparison + MESSAGES_DB.create_tables([Message]) time = datetime.now() - Message.create(sender=user, message=raw_msg, timestamp=time) - - -MESSAGES_DB.create_tables([Message])