Fix user search query

This commit is contained in:
coolneng 2020-11-26 01:58:43 +01:00
parent 23d7bc0f7a
commit 2b9923e3c4
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
2 changed files with 10 additions and 8 deletions

View File

@ -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()

View File

@ -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