From ecf0b847369e5cdd3c47368ad8aa1a6a9300415e Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 15:58:23 +0530 Subject: [PATCH 01/43] Update db.py --- tmessage/db.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index f89901e..59098e3 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -1,28 +1,32 @@ """Handles the connection to the SQLite database as well as DB interaction""" from datetime import datetime +from flask import Flask +from flask_sqlalchemy import SQLAlchemy -from peewee import CharField, DateTimeField, Model, SqliteDatabase +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///main.db' +app.config['SQLALCHEMY_BINDS'] = { 'we' : 'sqlite:///we.db'} +db = SQLAlchemy(app) +User = '' +class MessageDatabase(db.Model): + __bind_key__ = User + sender = db.Column(db.String(120), primary_key=True, nullable=False) + message = db.Column(db.String(120), nullable=False) + timestamp = db.Column(db.DateTime) -MESSAGES_DB = SqliteDatabase('message_store.sqlite') + def __repr__(self): + return f"MessageDatabase('{self.sender}','{self.message}','{self.timestamp}')" -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 - - -@MESSAGES_DB -def store_messages(user, raw_msg): +def store_messages(user, raw_msg, new): + User = user """Store a message sent by the indicated user in the database""" - time = datetime.now() - Message.create(sender=user, message=raw_msg, timestamp=time) - - -MESSAGES_DB.create_tables([Message]) + if new == 'yes': + app.config['SQLALCHEMY_BINDS'][User] = f'sqlite:///{User}.db' + time = datetime.now() + db.create_all() + x = MessageDatabase(sender=User, message=raw_msg, timestamp=time) + db.session.add(x) + db.session.commit() From 2b4fc33878505a3488fdb0b7bee360a9cdb380cf Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 16:01:16 +0530 Subject: [PATCH 02/43] Update db.py In this edit, we can now create new databases for the users who have recently registered, and the messages sent by the users will be stored in their respective databases. I have used SQLAlchemy instead of peewee. --- tmessage/db.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index 59098e3..62a6b1d 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -27,6 +27,6 @@ def store_messages(user, raw_msg, new): app.config['SQLALCHEMY_BINDS'][User] = f'sqlite:///{User}.db' time = datetime.now() db.create_all() - x = MessageDatabase(sender=User, message=raw_msg, timestamp=time) - db.session.add(x) + Data = MessageDatabase(sender=User, message=raw_msg, timestamp=time) + db.session.add(Data) db.session.commit() From c01ddfeee583529f7ba2bf2b4502e1a3cc62e862 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 16:05:31 +0530 Subject: [PATCH 03/43] 'new' variable to create databases for new users Just added a variable 'new' in line 65 and line 78. And a new argument named 'new'. --- tmessage/cli.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tmessage/cli.py b/tmessage/cli.py index f6ebd14..f669d91 100644 --- a/tmessage/cli.py +++ b/tmessage/cli.py @@ -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 = 'yes' 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 = 'no' 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) + store_messages(CURRENT_USER, formatted_msg, new) else: print(Back.WHITE + Fore.RED + "Can't send empty message", end="\n") except KeyboardInterrupt: From 65b1fc62e52b8cb20ceb583ff67ed931ad948e86 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 16:24:20 +0530 Subject: [PATCH 04/43] Update db.py --- tmessage/db.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tmessage/db.py b/tmessage/db.py index 62a6b1d..533c234 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -1,4 +1,6 @@ """Handles the connection to the SQLite database as well as DB interaction""" +pip install flask +pip install flask-sqlalchemy from datetime import datetime from flask import Flask from flask_sqlalchemy import SQLAlchemy From 7f2e80b3fff97702ffd280b6963a59b1a607c918 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 16:49:34 +0530 Subject: [PATCH 05/43] Update db.py --- tmessage/db.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index 533c234..62a6b1d 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -1,6 +1,4 @@ """Handles the connection to the SQLite database as well as DB interaction""" -pip install flask -pip install flask-sqlalchemy from datetime import datetime from flask import Flask from flask_sqlalchemy import SQLAlchemy From e486493298a6826e0b3b685137cca077fef93ccc Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 20:48:31 +0530 Subject: [PATCH 06/43] Update db.py --- tmessage/db.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tmessage/db.py b/tmessage/db.py index 62a6b1d..75c3a5f 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -3,12 +3,15 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy + app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///main.db' -app.config['SQLALCHEMY_BINDS'] = { 'we' : 'sqlite:///we.db'} +app.config['SQLALCHEMY_BINDS'] = {} db = SQLAlchemy(app) User = '' + + class MessageDatabase(db.Model): __bind_key__ = User sender = db.Column(db.String(120), primary_key=True, nullable=False) From 909f3a51507dba2957b18e7cb2b1732bffef066e Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 21:07:05 +0530 Subject: [PATCH 07/43] Update requirements.txt --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index 5ee0a20..f474eaf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,5 @@ python-dotenv==0.10.3 pylint==2.4.2 simple-chalk==0.1.0 pre-commit==1.18.3 +flask==1.1.1 +Flask-SQLAlchemy==2.4.1 From 68f1dd5e6d4658d6faf6094791369a1c689ead6f Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 21:43:57 +0530 Subject: [PATCH 08/43] Update db.py --- tmessage/db.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index 75c3a5f..765b1c8 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -4,32 +4,32 @@ from flask_sqlalchemy import SQLAlchemy -app = Flask(__name__) -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///main.db' -app.config['SQLALCHEMY_BINDS'] = {} -db = SQLAlchemy(app) +APP = Flask(__name__) +APP.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///main.db' +APP.config['SQLALCHEMY_BINDS'] = {} +DB = SQLAlchemy(APP) -User = '' +USER = '' -class MessageDatabase(db.Model): - __bind_key__ = User - sender = db.Column(db.String(120), primary_key=True, nullable=False) - message = db.Column(db.String(120), nullable=False) - timestamp = db.Column(db.DateTime) +class Message(DB.Model): + __bind_key__ = USER + sender = DB.Column(DB.String(120), primary_key=True, nullable=False) + message = DB.Column(DB.String(120), nullable=False) + timestamp = DB.Column(DB.DateTime) def __repr__(self): - return f"MessageDatabase('{self.sender}','{self.message}','{self.timestamp}')" + return f"Message('{self.sender}','{self.message}','{self.timestamp}')" def store_messages(user, raw_msg, new): - User = user + USER = user """Store a message sent by the indicated user in the database""" if new == 'yes': - app.config['SQLALCHEMY_BINDS'][User] = f'sqlite:///{User}.db' + APP.config['SQLALCHEMY_BINDS'][USER] = f'sqlite:///{USER}.db' time = datetime.now() - db.create_all() - Data = MessageDatabase(sender=User, message=raw_msg, timestamp=time) - db.session.add(Data) - db.session.commit() + DB.create_all() + data = Message(sender=USER, message=raw_msg, timestamp=time) + DB.session.add(data) + DB.session.commit() From 98e7b292d4b2fe72a04082cfd1b739548b94af29 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 21:54:33 +0530 Subject: [PATCH 09/43] Update db.py --- tmessage/db.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index 765b1c8..76d9813 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -9,11 +9,10 @@ APP.config['SQLALCHEMY_BINDS'] = {} DB = SQLAlchemy(APP) -USER = '' - class Message(DB.Model): - __bind_key__ = USER + def fun(user): + __bind_key__ = user sender = DB.Column(DB.String(120), primary_key=True, nullable=False) message = DB.Column(DB.String(120), nullable=False) timestamp = DB.Column(DB.DateTime) @@ -23,13 +22,13 @@ def __repr__(self): def store_messages(user, raw_msg, new): - USER = user """Store a message sent by the indicated user in the database""" - if new == 'yes': - APP.config['SQLALCHEMY_BINDS'][USER] = f'sqlite:///{USER}.db' + APP.config['SQLALCHEMY_BINDS'][user] = f'sqlite:///{user}.db' + + Message.fun(user) time = datetime.now() DB.create_all() - data = Message(sender=USER, message=raw_msg, timestamp=time) + data = Message(sender=user, message=raw_msg, timestamp=time) DB.session.add(data) DB.session.commit() From 40528e10007d1369a40f46f204655c3e0034bc5b Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 22:01:07 +0530 Subject: [PATCH 10/43] Update cli.py --- tmessage/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tmessage/cli.py b/tmessage/cli.py index f669d91..0082bce 100644 --- a/tmessage/cli.py +++ b/tmessage/cli.py @@ -59,6 +59,7 @@ def on_message(client, userdata, message): def main(): """ Register a new User or Authenticates the already registered User to send message """ try: + new = '' if auth.check_existed(CURRENT_USER): password = getpass(f"User {CURRENT_USER} found\nEnter password: ") payload = auth.authenticate(CURRENT_USER, password) From 33dc6e2ccd63a6b6fc41ff639593966d6ba9cb70 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 22:14:09 +0530 Subject: [PATCH 11/43] Update db.py --- tmessage/db.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index 76d9813..69aefae 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -11,8 +11,6 @@ class Message(DB.Model): - def fun(user): - __bind_key__ = user sender = DB.Column(DB.String(120), primary_key=True, nullable=False) message = DB.Column(DB.String(120), nullable=False) timestamp = DB.Column(DB.DateTime) @@ -21,12 +19,12 @@ def __repr__(self): return f"Message('{self.sender}','{self.message}','{self.timestamp}')" -def store_messages(user, raw_msg, new): - """Store a message sent by the indicated user in the database""" - if new == 'yes': - APP.config['SQLALCHEMY_BINDS'][user] = f'sqlite:///{user}.db' - - Message.fun(user) +def new_database(user): + APP.config['SQLALCHEMY_BINDS'][user] = f'sqlite:///{user}.db' + + +def store_messages(user, raw_msg): + Message.__bind_key__ = user time = datetime.now() DB.create_all() data = Message(sender=user, message=raw_msg, timestamp=time) From 65a4a1e675eb35d2a04324c5371b2f1029178e96 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 22:15:54 +0530 Subject: [PATCH 12/43] Update cli.py --- tmessage/cli.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tmessage/cli.py b/tmessage/cli.py index 0082bce..f5b6a77 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 store_messages, new_database # db.py from tmessage.utils import get_formatted_message # Initialize colorama @@ -59,11 +59,9 @@ def on_message(client, userdata, message): def main(): """ Register a new User or Authenticates the already registered User to send message """ try: - new = '' if auth.check_existed(CURRENT_USER): password = getpass(f"User {CURRENT_USER} found\nEnter password: ") payload = auth.authenticate(CURRENT_USER, password) - new = 'yes' else: print(f"Welcome {CURRENT_USER} to tmessage!\nPlease register...") displayed_name = input("Enter your name used for display: ") @@ -76,7 +74,7 @@ def main(): payload = auth.register( CURRENT_USER, displayed_name, password, password_confirm ) - new = 'no' + new_database(CURRENT_USER) print("User Authorized") user_name = payload["user_name"] displayed_name = payload["displayed_name"] @@ -92,7 +90,7 @@ def main(): if raw_msg != "": MQTT_CLIENT.publish(MQTT_TOPIC, pub_msg) if IS_STORE: - store_messages(CURRENT_USER, formatted_msg, new) + store_messages(CURRENT_USER, formatted_msg) else: print(Back.WHITE + Fore.RED + "Can't send empty message", end="\n") except KeyboardInterrupt: From cf9cbed38f581dfef66ae65b30ec04c35bba4f87 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Sun, 27 Oct 2019 22:30:43 +0530 Subject: [PATCH 13/43] Update requirements.txt --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index f474eaf..8915c3a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,5 @@ simple-chalk==0.1.0 pre-commit==1.18.3 flask==1.1.1 Flask-SQLAlchemy==2.4.1 +pylint-flask==0.6 +pylint-flask-sqlalchemy==0.1.0 From 0c9c00c3dd04befcdfac4391a61671f71fb27fab Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:08:27 +0530 Subject: [PATCH 14/43] Update db.py --- tmessage/db.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index 69aefae..934fcca 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -11,19 +11,24 @@ class Message(DB.Model): + """ Database where messages will be stored """ sender = DB.Column(DB.String(120), primary_key=True, nullable=False) message = DB.Column(DB.String(120), nullable=False) timestamp = DB.Column(DB.DateTime) + def add(self): + return 1+1 def __repr__(self): return f"Message('{self.sender}','{self.message}','{self.timestamp}')" def new_database(user): + """ creates new database for new users """ APP.config['SQLALCHEMY_BINDS'][user] = f'sqlite:///{user}.db' - - + + def store_messages(user, raw_msg): + """ function that stores messages from the particular user """ Message.__bind_key__ = user time = datetime.now() DB.create_all() From 009cf89cd1bc10b947a1bf5ea3ec23b00a954d7d Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:11:48 +0530 Subject: [PATCH 15/43] Update db.py --- tmessage/db.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tmessage/db.py b/tmessage/db.py index 934fcca..7105783 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -17,7 +17,8 @@ class Message(DB.Model): timestamp = DB.Column(DB.DateTime) def add(self): - return 1+1 + """ To few methods""" + print(self.sender) def __repr__(self): return f"Message('{self.sender}','{self.message}','{self.timestamp}')" From e67459b5e3ec2f2d379751b80d33b8c764873cfb Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:13:15 +0530 Subject: [PATCH 16/43] Update requirements.txt --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 8915c3a..f40843c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,3 @@ pre-commit==1.18.3 flask==1.1.1 Flask-SQLAlchemy==2.4.1 pylint-flask==0.6 -pylint-flask-sqlalchemy==0.1.0 From 0166b7c95d4d68d33e2fd9906700d34eabeb3586 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:15:43 +0530 Subject: [PATCH 17/43] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index f40843c..2d0a8c8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ requests==2.22.0 peewee==3.11.2 PyJWT==1.7.1 python-dotenv==0.10.3 -pylint==2.4.2 +flake8==3.7.8 simple-chalk==0.1.0 pre-commit==1.18.3 flask==1.1.1 From 7c468e9b728a35efdde185ca4aea93e6f3d891e8 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:20:51 +0530 Subject: [PATCH 18/43] Update requirements.txt --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 2d0a8c8..d395473 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,6 @@ requests==2.22.0 peewee==3.11.2 PyJWT==1.7.1 python-dotenv==0.10.3 -flake8==3.7.8 simple-chalk==0.1.0 pre-commit==1.18.3 flask==1.1.1 From c39ab5c0c930871275d6817b13c307835f0aca9e Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:28:56 +0530 Subject: [PATCH 19/43] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bad0ac9..28e8c2b 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-flask **/*.py From b901b9550938b64e5cdf92909e145da39a71ab46 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:34:04 +0530 Subject: [PATCH 20/43] Update .travis.yml --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 28e8c2b..1418b7f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,5 +2,6 @@ language: python python: - "3.7" install: "pip install -r requirements.txt" +install: '"python.linting.pylintArgs": ["--load-plugins", "pylint_flask"]' # command to run tests -script: pylint-flask **/*.py +script: pylint **/*.py From 3e0abc6494d2321c77b199b5f180e6557b83b186 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:35:33 +0530 Subject: [PATCH 21/43] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1418b7f..e9f937d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,6 @@ language: python python: - "3.7" install: "pip install -r requirements.txt" -install: '"python.linting.pylintArgs": ["--load-plugins", "pylint_flask"]' +script: "python.linting.pylintArgs": ["--load-plugins", "pylint_flask"] # command to run tests script: pylint **/*.py From 72b58f66613b570d2e2739231bf85f891acb9230 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:43:48 +0530 Subject: [PATCH 22/43] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e9f937d..87b71b4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,6 @@ language: python python: - "3.7" install: "pip install -r requirements.txt" -script: "python.linting.pylintArgs": ["--load-plugins", "pylint_flask"] +command: "python.linting.pylintArgs": ["--load-plugins", "pylint_flask"] # command to run tests script: pylint **/*.py From 92efb109570e2791ec8dd8ff5abe7f045236eaa8 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:44:55 +0530 Subject: [PATCH 23/43] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 87b71b4..361dfca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,6 @@ language: python python: - "3.7" install: "pip install -r requirements.txt" -command: "python.linting.pylintArgs": ["--load-plugins", "pylint_flask"] +command: "python.linting.pylintArgs": ["pylint_flask"] # command to run tests script: pylint **/*.py From cc36051aaa13b03627612474b2a5c5db22190cc8 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:46:15 +0530 Subject: [PATCH 24/43] Update .travis.yml --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 361dfca..c1e969b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,5 @@ language: python python: - "3.7" install: "pip install -r requirements.txt" -command: "python.linting.pylintArgs": ["pylint_flask"] # command to run tests script: pylint **/*.py From b884ce8f383f1453e7d4cc2655e539cfd00ae3c7 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:49:09 +0530 Subject: [PATCH 25/43] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c1e969b..5024cd4 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 +script: flake8 **/*.py From 69317848abe66d619201e1eb78f664efce40a030 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:51:36 +0530 Subject: [PATCH 26/43] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index d395473..3472e0d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ pre-commit==1.18.3 flask==1.1.1 Flask-SQLAlchemy==2.4.1 pylint-flask==0.6 +flake8=3.7.8 From b7311b9ed5478be1dbe28a72465f656f2e061334 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 00:52:47 +0530 Subject: [PATCH 27/43] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 3472e0d..85eea7b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,4 @@ pre-commit==1.18.3 flask==1.1.1 Flask-SQLAlchemy==2.4.1 pylint-flask==0.6 -flake8=3.7.8 +flake8==3.7.8 From ce5bea91e0c0e47b2a514c260ee60294e74361ac Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 01:03:08 +0530 Subject: [PATCH 28/43] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5024cd4..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: flake8 **/*.py +script: pylint **/*.py From 5616731b45228cb3a6e3d702314839809283cebd Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 01:09:30 +0530 Subject: [PATCH 29/43] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c1e969b..5024cd4 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 +script: flake8 **/*.py From 2f55181365c7d70d1de59b7bd5af839f86dcdf82 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 01:57:48 +0530 Subject: [PATCH 30/43] Update db.py --- tmessage/db.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tmessage/db.py b/tmessage/db.py index 7105783..0b7dc78 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -19,6 +19,7 @@ class Message(DB.Model): def add(self): """ To few methods""" print(self.sender) + def __repr__(self): return f"Message('{self.sender}','{self.message}','{self.timestamp}')" From 4406ae48a019045e7643636d1ee9f9ac6d1b1ccc Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 02:01:46 +0530 Subject: [PATCH 31/43] Update auth.py --- tmessage/auth.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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' From b1bc6a15c1652c593365f627f27da4de12eb6333 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 02:12:05 +0530 Subject: [PATCH 32/43] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5024cd4..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: flake8 **/*.py +script: pylint **/*.py From d622585a07bdfc61e42f384275f2337b19c7120e Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 02:13:58 +0530 Subject: [PATCH 33/43] Update db.py --- tmessage/db.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tmessage/db.py b/tmessage/db.py index 0b7dc78..38668da 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -11,6 +11,7 @@ class Message(DB.Model): + # pylint: disable=all """ Database where messages will be stored """ sender = DB.Column(DB.String(120), primary_key=True, nullable=False) message = DB.Column(DB.String(120), nullable=False) @@ -30,6 +31,7 @@ def new_database(user): def store_messages(user, raw_msg): + # pylint: disable=all """ function that stores messages from the particular user """ Message.__bind_key__ = user time = datetime.now() From 0701dfb6a2c319ddb863693ebbfdf5638f571fd4 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 02:18:01 +0530 Subject: [PATCH 34/43] Update requirements.txt --- requirements.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index 85eea7b..578a22c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,10 @@ colorama==0.4.1 paho-mqtt==1.4.0 requests==2.22.0 -peewee==3.11.2 PyJWT==1.7.1 python-dotenv==0.10.3 simple-chalk==0.1.0 pre-commit==1.18.3 flask==1.1.1 Flask-SQLAlchemy==2.4.1 -pylint-flask==0.6 -flake8==3.7.8 + From 366b3e14be7c422aebdeae62c8427abe69a1947f Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 02:19:22 +0530 Subject: [PATCH 35/43] Update db.py --- tmessage/db.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index 38668da..e64f415 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -16,10 +16,7 @@ class Message(DB.Model): sender = DB.Column(DB.String(120), primary_key=True, nullable=False) message = DB.Column(DB.String(120), nullable=False) timestamp = DB.Column(DB.DateTime) - - def add(self): - """ To few methods""" - print(self.sender) + def __repr__(self): return f"Message('{self.sender}','{self.message}','{self.timestamp}')" From 8935f7008983cda2d110794980e1ad1dc2f506e4 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 02:22:43 +0530 Subject: [PATCH 36/43] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 578a22c..4590d45 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,5 @@ simple-chalk==0.1.0 pre-commit==1.18.3 flask==1.1.1 Flask-SQLAlchemy==2.4.1 +pylint==2.4.2 From 14c401c79312d9c403f66418a4a6904e5e05ae3f Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 17:08:34 +0530 Subject: [PATCH 37/43] Update requirements.txt --- requirements.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index 4590d45..e06b3b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,11 @@ + colorama==0.4.1 paho-mqtt==1.4.0 requests==2.22.0 +peewee==3.11.2 PyJWT==1.7.1 python-dotenv==0.10.3 +pylint==2.4.2 simple-chalk==0.1.0 pre-commit==1.18.3 -flask==1.1.1 -Flask-SQLAlchemy==2.4.1 -pylint==2.4.2 From 83939978fbecb910037c0d3a3624778a6a613030 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 17:10:11 +0530 Subject: [PATCH 38/43] Update db.py --- tmessage/db.py | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index e64f415..f9103f4 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -1,38 +1,26 @@ """Handles the connection to the SQLite database as well as DB interaction""" from datetime import datetime -from flask import Flask -from flask_sqlalchemy import SQLAlchemy +from peewee import CharField, DateTimeField, Model, SqliteDatabase -APP = Flask(__name__) -APP.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///main.db' -APP.config['SQLALCHEMY_BINDS'] = {} -DB = SQLAlchemy(APP) +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') -class Message(DB.Model): - # pylint: disable=all - """ Database where messages will be stored """ - sender = DB.Column(DB.String(120), primary_key=True, nullable=False) - message = DB.Column(DB.String(120), nullable=False) - timestamp = DB.Column(DB.DateTime) - - - def __repr__(self): - return f"Message('{self.sender}','{self.message}','{self.timestamp}')" + class Message(Model): + """Message table - keeps track of message sent and received""" + sender = CharField() + message = CharField() + timestamp = DateTimeField() -def new_database(user): - """ creates new database for new users """ - APP.config['SQLALCHEMY_BINDS'][user] = f'sqlite:///{user}.db' + class Meta: # pylint: disable=missing-class-docstring,too-few-public-methods + database = MESSAGES_DB -def store_messages(user, raw_msg): - # pylint: disable=all - """ function that stores messages from the particular user """ - Message.__bind_key__ = user + if new == True: + MESSAGES_DB.create_tables([Message]) + MESSAGES_DB.connect() time = datetime.now() - DB.create_all() - data = Message(sender=user, message=raw_msg, timestamp=time) - DB.session.add(data) - DB.session.commit() + Message.create(sender=user, message=raw_msg, timestamp=time) From badbe2af2c56e24466aef37114f61d5940d8f73b Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 17:11:14 +0530 Subject: [PATCH 39/43] Update cli.py --- tmessage/cli.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tmessage/cli.py b/tmessage/cli.py index f5b6a77..443163d 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, new_database # db.py +from tmessage.db import database # db.py from tmessage.utils import get_formatted_message # Initialize colorama @@ -36,6 +36,7 @@ MQTT_CLIENT = mqtt.Client() CURRENT_USER = ARGS.user +new = False def on_message(client, userdata, message): @@ -53,7 +54,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, new) def main(): @@ -74,7 +75,7 @@ def main(): payload = auth.register( CURRENT_USER, displayed_name, password, password_confirm ) - new_database(CURRENT_USER) + new = True print("User Authorized") user_name = payload["user_name"] displayed_name = payload["displayed_name"] @@ -90,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: From e113d4ead39f3d061cec1853ccf95ef847fad57b Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 17:17:41 +0530 Subject: [PATCH 40/43] Update db.py --- tmessage/db.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tmessage/db.py b/tmessage/db.py index f9103f4..20cbefa 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -6,7 +6,7 @@ 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') + MESSAGES_DB = SqliteDatabase(f'{user}.db')# pylint: disable=invalid_name class Message(Model): @@ -19,8 +19,7 @@ class Meta: # pylint: disable=missing-class-docstring,too-few-public-methods database = MESSAGES_DB - if new == True: + if new = True: MESSAGES_DB.create_tables([Message]) - MESSAGES_DB.connect() time = datetime.now() Message.create(sender=user, message=raw_msg, timestamp=time) From 468663571d5ed35c9d0b46bb7a3989a52eb2c506 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 17:21:19 +0530 Subject: [PATCH 41/43] Update db.py --- tmessage/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tmessage/db.py b/tmessage/db.py index 20cbefa..827eacf 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -19,7 +19,7 @@ class Meta: # pylint: disable=missing-class-docstring,too-few-public-methods database = MESSAGES_DB - if new = True: + if new == True: # pylint: disable=singleton-comparison MESSAGES_DB.create_tables([Message]) time = datetime.now() Message.create(sender=user, message=raw_msg, timestamp=time) From 9b3692d807fb887cd646a4413095367289b09f88 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Mon, 28 Oct 2019 17:23:45 +0530 Subject: [PATCH 42/43] Update db.py --- tmessage/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tmessage/db.py b/tmessage/db.py index 827eacf..968c28f 100644 --- a/tmessage/db.py +++ b/tmessage/db.py @@ -6,7 +6,7 @@ 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 + MESSAGES_DB = SqliteDatabase(f'{user}.db')# pylint: disable=invalid-name class Message(Model): From 30960f9a4d390165d86841c39e57b0eb5664f4e1 Mon Sep 17 00:00:00 2001 From: HashirGJ8842 <56411583+HashirGJ8842@users.noreply.github.com> Date: Tue, 29 Oct 2019 13:18:25 +0530 Subject: [PATCH 43/43] Update cli.py --- tmessage/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tmessage/cli.py b/tmessage/cli.py index 443163d..26e0f61 100644 --- a/tmessage/cli.py +++ b/tmessage/cli.py @@ -36,7 +36,6 @@ MQTT_CLIENT = mqtt.Client() CURRENT_USER = ARGS.user -new = False def on_message(client, userdata, message): @@ -54,7 +53,7 @@ def on_message(client, userdata, message): print(user_details, msg) _, _, message = current_msg.partition("] ") if IS_STORE: - database(user, message, new) + database(user, message, False) def main(): @@ -63,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: ")