2020-11-17 14:16:44 +01:00
|
|
|
from sqlalchemy import or_
|
|
|
|
|
|
|
|
from database import SessionLocal, engine, models
|
|
|
|
from database.models import Users
|
|
|
|
|
|
|
|
db = SessionLocal()
|
|
|
|
|
|
|
|
|
|
|
|
def search_database(id):
|
|
|
|
return db.query(Users).filter(
|
|
|
|
or_(Users.correo_institucional == id, Users.numero_de_documento == id)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def insert_data(data):
|
|
|
|
item = Users(**data)
|
|
|
|
db.add(item)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(item)
|
|
|
|
|
|
|
|
|
|
|
|
def create_database(data):
|
|
|
|
models.Base.metadata.create_all(bind=engine)
|
|
|
|
existing_row = search_database(data["correo_institucional"])
|
|
|
|
if existing_row:
|
|
|
|
return
|
|
|
|
insert_data(data)
|
2020-11-25 11:06:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def save_attribute(attribute, data):
|
|
|
|
key = eval("Users." + attribute)
|
|
|
|
db.query(Users).filter(
|
|
|
|
or_(Users.correo_institucional == id, Users.numero_de_documento == id)
|
|
|
|
).update({key: data})
|
|
|
|
|
|
|
|
|
|
|
|
def verify_code(id, code):
|
|
|
|
db_record = search_database(id=id)
|
|
|
|
valid_code = code == db_record
|
|
|
|
if valid_code:
|
|
|
|
return True
|
|
|
|
return False
|