diff --git a/app.py b/app.py index c2e06e5..28e5d4e 100644 --- a/app.py +++ b/app.py @@ -17,9 +17,11 @@ from collections import namedtuple from cryptography.fernet import Fernet +load_dotenv() + import model.inference as inference from model import request_function -import storage.azure_storage_api as azure_storage_api +from datastore import azure_storage_api import storage.datastore_storage_api as datastore @@ -74,7 +76,6 @@ class ImageWarning(APIWarnings): class MaxContentLengthWarning(APIWarnings): pass -load_dotenv() connection_string_regex = r"^DefaultEndpointsProtocol=https?;.*;FileEndpoint=https://[a-zA-Z0-9]+\.file\.core\.windows\.net/;$" pipeline_version_regex = r"\d.\d.\d" @@ -564,9 +565,8 @@ def validate_and_upload_data(email, picture_set, data): raise EmailNotSendError("the user email is not provided") if not picture_set: raise EmptyPictureSetError("no picture set provided") - cursor = app.config["DB"].cursor() - user_id = datastore.is_user_registered(cursor, email) - picture_id = datastore.upload_picture_set(cursor, user_id=user_id, **data) + user_id = datastore.validate_user(email) + picture_id = datastore.upload_picture_set(user_id=user_id, **data) return picture_id async def fetch_json(repo_URL, key, file_path): diff --git a/storage/datastore_storage_api.py b/storage/datastore_storage_api.py index a9415b3..0f7f776 100644 --- a/storage/datastore_storage_api.py +++ b/storage/datastore_storage_api.py @@ -3,32 +3,55 @@ This module provide an absraction to the nachet-datastore interface. """ import datastore -from datastore import db +import datastore.bin.deployment_mass_import + +import datastore.bin.upload_picture_set +import datastore.db.queries.seed as seed_queries +# import datastore.db.queries.user as user_queries +# import datastore.db.queries.picture as picture_queries +from datastore import db, user + class DatastoreError(Exception): pass + class SeedNotFoundError(DatastoreError): pass -def get_connection(): - return db.connect_db() -def get_cursor(connection): +def get_cursor(): + db.connect_db() return db.cursor() + def get_all_seeds_names() -> list: """ Return all seeds name register in the Datastore. """ try: - return db.queries.seed.get_all_seeds_names(get_cursor) + return seed_queries.get_all_seeds_names(get_cursor()) except Exception as error: # TODO modify Exception for more specific exception raise SeedNotFoundError(error.args[0]) + def get_seeds(expression: str) -> list: """ Return a list of all seed that contains the expression """ - return list(filter(lambda x: expression in x, get_all_seeds_names(get_cursor))) \ No newline at end of file + return list(filter(lambda x: expression in x, get_all_seeds_names(get_cursor()))) + + +def validate_user(email: str) -> datastore.User: + """ + Return True if user is valid, False otherwise + """ + + cursor = get_cursor() + if user.is_user_registered(cursor, email): + return datastore.get_User(email, cursor) + + +def upload_picture_set(**kwargs): + return datastore.bin.upload_picture_set.upload_picture_set(get_cursor(), **kwargs)