From 2b9923e3c4bc8b1774747472d1839f37d54643d7 Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 26 Nov 2020 01:58:43 +0100 Subject: [PATCH] Fix user search query --- app/notifier.py | 4 ++-- database/crud.py | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/notifier.py b/app/notifier.py index 7128107..6dda6f5 100644 --- a/app/notifier.py +++ b/app/notifier.py @@ -21,7 +21,7 @@ def format_message(sender, recipient) -> Tuple[EmailMessage, str]: Creates the email message and formats it accordingly """ code = token_hex(4) - body = "La clave es {}. Envíele esta clave al bot.".format() + body = f"La clave es {code}. Envíele esta clave al bot." message = EmailMessage() message["From"] = sender message["To"] = recipient @@ -36,6 +36,6 @@ def send_mail(recipient) -> None: """ server = initialize_smtp(domain=DOMAIN, username=USERNAME, password=PASSWORD) message, code = format_message(sender=USERNAME, recipient=recipient) - save_attribute(attribute="codigo", data=code) + save_attribute(id=recipient, attribute="codigo", data=code) server.send_message(msg=message) server.close() diff --git a/database/crud.py b/database/crud.py index e75877b..019c9de 100644 --- a/database/crud.py +++ b/database/crud.py @@ -1,5 +1,4 @@ from sqlalchemy import or_ -from sqlalchemy.orm import Query from database import SessionLocal, engine, models from database.models import User @@ -7,12 +6,14 @@ from database.models import User db = SessionLocal() -def search_database(id) -> Query: +def search_database(id) -> User: """ Returns the user associated with the id argument """ - return db.query(User).filter( - or_(User.correo_institucional == id, User.numero_de_documento == id) + return ( + db.query(User) + .filter(or_(User.correo_institucional == id, User.numero_de_documento == id)) + .first() ) @@ -37,7 +38,7 @@ def create_database(data) -> None: insert_data(data) -def save_attribute(attribute, data) -> None: +def save_attribute(id, attribute, data) -> None: """ Updates the attribute value with the content of data """ @@ -45,6 +46,7 @@ def save_attribute(attribute, data) -> None: db.query(User).filter( or_(User.correo_institucional == id, User.numero_de_documento == id) ).update({key: data}) + db.commit() def verify_code(id, code) -> bool: @@ -52,7 +54,7 @@ def verify_code(id, code) -> bool: Verifies that two-factor authentification code matches the database record """ db_record = search_database(id=id) - valid_code = code == db_record + valid_code = code == db_record.codigo if valid_code: return True return False