From b58905dec979fee789b78bb7266289660531efc4 Mon Sep 17 00:00:00 2001 From: signebedi Date: Mon, 25 Mar 2024 15:00:02 -0500 Subject: [PATCH] Modified: api_key is now a foreign key (#68) --- libreforms_fastapi/utils/document_database.py | 20 +++++++++++++++---- libreforms_fastapi/utils/sqlalchemy_models.py | 3 +-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/libreforms_fastapi/utils/document_database.py b/libreforms_fastapi/utils/document_database.py index e8a6865..1037791 100644 --- a/libreforms_fastapi/utils/document_database.py +++ b/libreforms_fastapi/utils/document_database.py @@ -163,8 +163,6 @@ def __init__(self, form_name, document_id): message = f"The document with ID '{document_id}' collection '{form_name}' is not deleted and cannot be restored." super().__init__(message) - - class InsufficientPermissions(Exception): """Exception raised when attempting to access a document that user lacks permissions for.""" def __init__(self, form_name, document_id, username): @@ -205,8 +203,22 @@ def get_document_database( if use_mongodb: if not mongodb_uri or mongodb_uri=="": raise Exception("Please pass a value MongoDB URI") - return ManageMongoDB(form_names_callable, timezone, db_path, use_logger, env) - return ManageTinyDB(form_names_callable, timezone, db_path, use_logger, env) + return ManageMongoDB( + form_names_callable=form_names_callable, + timezone=timezone, + db_path=db_path, + use_logger=use_logger, + env=env, + mongodb_uri=mongodb_uri, + ) + # Default to a TinyDB database + return ManageTinyDB( + form_names_callable=form_names_callable, + timezone=timezone, + db_path=db_path, + use_logger=use_logger, + env=env, + ) class ManageDocumentDB(ABC): diff --git a/libreforms_fastapi/utils/sqlalchemy_models.py b/libreforms_fastapi/utils/sqlalchemy_models.py index 62f8ede..7426fbc 100644 --- a/libreforms_fastapi/utils/sqlalchemy_models.py +++ b/libreforms_fastapi/utils/sqlalchemy_models.py @@ -51,8 +51,7 @@ class User(Base): locked_until = Column(DateTime, nullable=True, default=tz_aware_datetime) last_password_change = Column(DateTime, nullable=True, default=tz_aware_datetime) failed_login_attempts = Column(Integer, default=0) - # api_key_id = Column(Integer, ForeignKey('signing.id'), nullable=True) - api_key = Column(String(1000), nullable=True, unique=True) + api_key = Column(String(1000), ForeignKey('signing.signature'), nullable=True, unique=True) # This opt out, if true, will exclude this user's ID and IP from the statistics # gathered from their usage, see https://github.com/signebedi/gita-api/issues/59. opt_out = Column(Boolean, nullable=False, default=False)