From 7421149ae5638d59b98051c7772cde920f81ba0f Mon Sep 17 00:00:00 2001 From: pablomariz Date: Thu, 21 May 2020 03:58:12 -0300 Subject: [PATCH] =?UTF-8?q?Corre=C3=A7=C3=B5es=20diversAs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database_manager.py | 11 ++-- model.py | 25 +++++----- rotas.py | 119 +++++++++++++++++++++++--------------------- 3 files changed, 79 insertions(+), 76 deletions(-) diff --git a/database_manager.py b/database_manager.py index f79fcb1..92af1d9 100644 --- a/database_manager.py +++ b/database_manager.py @@ -8,10 +8,9 @@ from mysql_manager import Gera_query class Database(): - def __init__(self, database = 1): + def __init__(self): self.gerador_de_query = Gera_query() self.connected = False - self.database = database def connect(self, database): @@ -57,11 +56,13 @@ def authenticate(self, database): config = configparser.ConfigParser() config.read('conf.cfg') - if self.database == 1: + if database == 1: config_bank = 'config_bank_app' else: config_bank = 'config_bank_api' + print(config_bank) + address_bank = config.get(config_bank, 'address_bank') name_bank = config.get(config_bank, 'name_bank') user_bank = config.get(config_bank, 'user_bank') @@ -132,7 +133,7 @@ def return_user_id(self, username): def return_salt(self, username): - query = f'select salt from usuarios where login = "{username}"' + query = f'select salt from usuarios where username = "{username}"' salt = self.commit_with_return(query)[0][0] @@ -140,7 +141,7 @@ def return_salt(self, username): def return_password(self, username, password): - query = f'select password from usuarios where login = "{username}" and password = "{password}"' + query = f'select password from usuarios where username = "{username}" and password = "{password}"' password = self.commit_with_return(query)[0][0] diff --git a/model.py b/model.py index dc4ff1b..a0ea5d7 100644 --- a/model.py +++ b/model.py @@ -30,7 +30,7 @@ def gera_token(self, data): user_id = self.database.return_user_id(username) token = str(user_type) - token += self.crypto(username, user_id) + token += self.crypto(username, str(user_id)) query = f"INSERT INTO `tokens` (`user`,`token`) VALUES ({int(user_id)}, '{token}');" self.database.commit_without_return(query, database = 2) @@ -82,20 +82,20 @@ def confirm_token(self, data): def active_token(self, token, user_id): try: query = f'select id from tokens where token = {token} and user = {user_id}' - token_id = self.database.commit_with_return(query, database = 2)[0][0] - - if len(id) > 0: + token_id = self.database.commit_with_return(query, database = 2)[0] + + if len(token_id) > 0: query = self.gera_query.alterar_dados_da_tabela('connections', ['active'], [1], where=True, - valor_where=token_id, coluna_verificacao='id') + valor_where=token_id[0], coluna_verificacao='id') self.database.commit_without_return(query, database = 2) else: - query = self.gera_query.inserir_na_tabela('connections', ['token'], [token_id]) + query = self.gera_query.inserir_na_tabela('connections', ['token'], [token_id[0]]) self.database.commit_without_return(query, database = 2) self.r = { 'Message' : { 'token' : token, - 'token_id' : token_id + 'token_id' : token_id[0] }, 'Status' : 200 } @@ -111,14 +111,14 @@ def active_token(self, token, user_id): def deactive_token(self, token): try: query = f'select id from tokens where token = {token}' - token_id = self.database.commit_with_return(query, database = 2)[0][0] + token_id = self.database.commit_with_return(query, database = 2)[0] - if len(id) > 0: + if len(token_id) > 0: query = self.gera_query.alterar_dados_da_tabela('connections', ['active'], [0], where=True, - valor_where=token_id, coluna_verificacao='id') + valor_where=token_id[0], coluna_verificacao='id') self.database.commit_without_return(query, database = 2) else: - query = self.gera_query.inserir_na_tabela('connections', ['token', 'active'], [token_id, 0]) + query = self.gera_query.inserir_na_tabela('connections', ['token', 'active'], [token_id[0], 0]) self.database.commit_without_return(query, database = 2) self.r = { @@ -223,7 +223,6 @@ def novo_veiculo(self, data): username = data['Username'] user_type = data['UserType'] license_plate = data['LicensePlate'] - username = data['Username'] user_id = self.database.return_user_id(username) columns = self.database.return_columns('carros') @@ -406,7 +405,7 @@ def autenticar_usuario(self, data): user_id = self.database.return_user_id(username) - if len(user_id) == 0: + if not user_id: raise Exception('Usuário não encontrado no banco de dados') salt = self.database.return_salt(username) diff --git a/rotas.py b/rotas.py index 094b516..a5b2dfa 100644 --- a/rotas.py +++ b/rotas.py @@ -27,15 +27,15 @@ def novo_veiculo(): raise Exception('Invalid Token') response = jsonify(backend.novo_veiculo(data)) - status = int(response['status']) except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status #adicionar novo usuario - FUNCIONANDO @@ -54,14 +54,15 @@ def novo_usuario(): raise Exception(e) response = jsonify(backend.novo_usuario(data)) - status = int(response['status']) + except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status #usuario solicita nova limpeza - FUNCIONANDO @@ -83,15 +84,15 @@ def nova_limpeza(): raise Exception('Invalid Token') response = jsonify(backend.nova_limpeza(data)) - status = int(response['status']) except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status #avalicao do cliente a respeito de UM carro(QRcode) - FUNCIONANDO @@ -113,15 +114,16 @@ def nova_avaliacao(): raise Exception('Invalid Token') response = jsonify(backend.nova_avaliacao(data)) - status = int(response['status']) - + + except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status @@ -143,15 +145,15 @@ def recusa_notificacao(): raise Exception('Invalid Token') response = jsonify(backend.recusa_notificacao(data)) - status = int(response['status']) except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status @@ -173,15 +175,15 @@ def grava_envio_notificao(): raise Exception('Invalid Token') response = jsonify(backend.grava_envio_notificao(data)) - status = int(response['status']) - + except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status @@ -202,43 +204,44 @@ def solicitar_limpeza(): if response['Message'] != 'OK': raise Exception('Invalid Token') - response = jsonify(backend.solicitar_limpeza(data)) - status = int(response['status']) - + response = backend.solicitar_limpeza(data) + except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) - return response, status + } + status = int(response['Status']) + return jsonify(response), status @app.route ('/autenticar_usuario', methods=['POST']) def autenticar_usuario(): try: - data = request.json() + data = request.get_json() if len(data) > 3: - e = jsonify({ - 'Message' : { - 'Error' : 'Request out of params' - }, - 'Status' : 401 - }) - raise Exception(e) + e = { + 'Message' : { + 'Error' : 'Request out of params' + }, + 'Status' : 401 + } + print(e) + raise Exception(e) - response = jsonify(backend.autenticar_usuario(data)) - status = int(response['status']) + response = backend.autenticar_usuario(data) except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) - return response, status + } + status = int(response['Status']) + return jsonify(response), status @app.route ('/buscar_notificacoes', methods=['POST']) @@ -259,15 +262,15 @@ def buscar_notificacoes(): raise Exception('Invalid Token') response = jsonify(backend.buscar_notificacoes(data)) - status = int(response['status']) except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status @@ -289,15 +292,15 @@ def buscar_limpezas_veiculo(): raise Exception('Invalid Token') response = jsonify(backend.buscar_limpezas_veiculo(data)) - status = int(response['status']) - + except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status @@ -319,15 +322,15 @@ def buscar_resumo_veiculo(): raise Exception('Invalid Token') response = jsonify(backend.buscar_resumo_veiculo(data)) - status = int(response['status']) except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status @@ -349,15 +352,15 @@ def buscar_ultima_limpeza_veiculo(): raise Exception('Invalid Token') response = jsonify(backend.buscar_ultima_limpeza_veiculo(data)) - status = int(response['status']) except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status @@ -379,15 +382,15 @@ def buscar_limpeza(): raise Exception('Invalid Token') response = jsonify(backend.buscar_limpeza(data)) - status = int(response['status']) except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status @@ -409,15 +412,15 @@ def realizar_logoff(): raise Exception('Invalid Token') response = jsonify(backend.realizar_logoff(data)) - status = int(response['status']) except Exception as e: - response = jsonify({ + response = { 'Message' : { 'Error' : str(e) }, 'Status' : 401 - }) + } + status = int(response.get_json()['Status']) return response, status