Document notifier and crud modules

This commit is contained in:
coolneng 2020-11-25 13:44:07 +01:00
parent 23a6e29ff9
commit 2d6597988c
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
2 changed files with 34 additions and 8 deletions

View File

@ -1,18 +1,25 @@
from email.message import EmailMessage from email.message import EmailMessage
from secrets import token_hex from secrets import token_hex
from smtplib import SMTP_SSL from smtplib import SMTP_SSL
from typing import Tuple
from constants import DOMAIN, PASSWORD, USERNAME from constants import DOMAIN, PASSWORD, USERNAME
from database.crud import save_attribute from database.crud import save_attribute
def initialize_smtp(domain, username, password): def initialize_smtp(domain, username, password) -> SMTP_SSL:
"""
Creates the SMTP connection to the mail server
"""
server = SMTP_SSL(domain) server = SMTP_SSL(domain)
server.login(username, password) server.login(username, password)
return server return server
def format_message(sender, recipient): def format_message(sender, recipient) -> Tuple[EmailMessage, str]:
"""
Creates the email message and formats it accordingly
"""
code = token_hex(4) code = token_hex(4)
body = "La clave es {}. Envíele esta clave al bot.".format() body = "La clave es {}. Envíele esta clave al bot.".format()
message = EmailMessage() message = EmailMessage()
@ -23,7 +30,10 @@ def format_message(sender, recipient):
return message, code return message, code
def send_mail(recipient): def send_mail(recipient) -> None:
"""
Sends an email to the specified recipient
"""
server = initialize_smtp(domain=DOMAIN, username=USERNAME, password=PASSWORD) server = initialize_smtp(domain=DOMAIN, username=USERNAME, password=PASSWORD)
message, code = format_message(sender=USERNAME, recipient=recipient) message, code = format_message(sender=USERNAME, recipient=recipient)
save_attribute(attribute="codigo", data=code) save_attribute(attribute="codigo", data=code)

View File

@ -1,4 +1,5 @@
from sqlalchemy import or_ from sqlalchemy import or_
from sqlalchemy.orm import Query
from database import SessionLocal, engine, models from database import SessionLocal, engine, models
from database.models import Users from database.models import Users
@ -6,20 +7,29 @@ from database.models import Users
db = SessionLocal() db = SessionLocal()
def search_database(id): def search_database(id) -> Query:
"""
Returns the user associated with the id argument
"""
return db.query(Users).filter( return db.query(Users).filter(
or_(Users.correo_institucional == id, Users.numero_de_documento == id) or_(Users.correo_institucional == id, Users.numero_de_documento == id)
) )
def insert_data(data): def insert_data(data) -> None:
"""
Inserts a new user into the database
"""
item = Users(**data) item = Users(**data)
db.add(item) db.add(item)
db.commit() db.commit()
db.refresh(item) db.refresh(item)
def create_database(data): def create_database(data) -> None:
"""
Creates the SQL database from scratch and populates it
"""
models.Base.metadata.create_all(bind=engine) models.Base.metadata.create_all(bind=engine)
existing_row = search_database(data["correo_institucional"]) existing_row = search_database(data["correo_institucional"])
if existing_row: if existing_row:
@ -27,14 +37,20 @@ def create_database(data):
insert_data(data) insert_data(data)
def save_attribute(attribute, data): def save_attribute(attribute, data) -> None:
"""
Updates the attribute value with the content of data
"""
key = eval("Users." + attribute) key = eval("Users." + attribute)
db.query(Users).filter( db.query(Users).filter(
or_(Users.correo_institucional == id, Users.numero_de_documento == id) or_(Users.correo_institucional == id, Users.numero_de_documento == id)
).update({key: data}) ).update({key: data})
def verify_code(id, code): def verify_code(id, code) -> bool:
"""
Verifies that two-factor authentification code matches the database record
"""
db_record = search_database(id=id) db_record = search_database(id=id)
valid_code = code == db_record valid_code = code == db_record
if valid_code: if valid_code: