diff --git a/api/api/db/logic/account.py b/api/api/db/logic/account.py index 2efb0af..21a73c8 100644 --- a/api/api/db/logic/account.py +++ b/api/api/db/logic/account.py @@ -76,19 +76,10 @@ async def get_user_by_login(connection: AsyncConnection, login: str) -> Optional query = select(account_table).where(account_table.c.login == login) user_db_cursor = await connection.execute(query) - user_db = user_db_cursor.one_or_none() - - if not user_db: + user_data = user_db_cursor.mappings().one_or_none() + if not user_data: return None - user_data = { - column.name: ( - getattr(user_db, column.name).name - if isinstance(getattr(user_db, column.name), Enum) - else getattr(user_db, column.name) - ) - for column in account_table.columns - } return User.model_validate(user_data) diff --git a/api/api/db/logic/keyring.py b/api/api/db/logic/keyring.py index ad3de1b..f4dd70d 100644 --- a/api/api/db/logic/keyring.py +++ b/api/api/db/logic/keyring.py @@ -18,20 +18,11 @@ async def get_key_by_id(connection: AsyncConnection, key_id: str) -> Optional[Ac query = select(account_keyring_table).where(account_keyring_table.c.key_id == key_id) user_db_cursor = await connection.execute(query) - user_db = user_db_cursor.one_or_none() - if not user_db: + user_data = user_db_cursor.mappings().one_or_none() + if not user_data: return None - user_data = { - column.name: ( - getattr(user_db, column.name).name - if isinstance(getattr(user_db, column.name), Enum) - else getattr(user_db, column.name) - ) - for column in account_keyring_table.columns - } - return AccountKeyring.model_validate(user_data) diff --git a/api/api/db/logic/listevents.py b/api/api/db/logic/listevents.py index c318195..e28589b 100644 --- a/api/api/db/logic/listevents.py +++ b/api/api/db/logic/listevents.py @@ -117,19 +117,13 @@ async def get_listevents_by_name(connection: AsyncConnection, name: str) -> Opti query = select(list_events_table).where(list_events_table.c.name == name) listevents_db_cursor = await connection.execute(query) - listevents_db = listevents_db_cursor.one_or_none() - if not listevents_db: + listevents_data = listevents_db_cursor.mappings().one_or_none() + if not listevents_data: return None - listevents_data = { - column.name: ( - getattr(listevents_db, column.name).name - if isinstance(getattr(listevents_db, column.name), Enum) - else getattr(listevents_db, column.name) - ) - for column in list_events_table.columns - } + + return ListEvent.model_validate(listevents_data) @@ -141,20 +135,11 @@ async def get_listevents_by_id(connection: AsyncConnection, id: int) -> Optional query = select(list_events_table).where(list_events_table.c.id == id) listevents_db_cursor = await connection.execute(query) - listevents_db = listevents_db_cursor.one_or_none() - if not listevents_db: + listevents_data = listevents_db_cursor.mappings().one_or_none() + if not listevents_data: return None - listevents_data = { - column.name: ( - getattr(listevents_db, column.name).name - if isinstance(getattr(listevents_db, column.name), Enum) - else getattr(listevents_db, column.name) - ) - for column in list_events_table.columns - } - return ListEvent.model_validate(listevents_data) diff --git a/api/api/db/logic/processschema.py b/api/api/db/logic/processschema.py index 5f34392..f048275 100644 --- a/api/api/db/logic/processschema.py +++ b/api/api/db/logic/processschema.py @@ -13,11 +13,11 @@ from api.schemas.process.process_schema import ProcessSchema from api.schemas.endpoints.process_schema import all_process_schema_adapter, AllProcessSchemaResponse -async def get_processschema_page_by_creator_id( +async def get_process_schema_page_by_creator_id( connection: AsyncConnection, creator_id: int, page: int, limit: int ) -> Optional[AllProcessSchemaResponse]: """ - Получает список событий заданного создателя по значениям page и limit и creator_id. + Получает список схем процессов по значениям page и limit и creator_id. """ first_schema = page * limit - limit @@ -62,9 +62,9 @@ async def get_processschema_page_by_creator_id( ) -async def get_processschema_page(connection: AsyncConnection, page, limit) -> Optional[AllProcessSchemaResponse]: +async def get_process_schema_page(connection: AsyncConnection, page, limit) -> Optional[AllProcessSchemaResponse]: """ - Получает список событий заданного создателя по значениям page и limit. + Получает список схем процессов по значениям page и limit. """ first_schema = page * limit - (limit) @@ -107,53 +107,36 @@ async def get_processschema_page(connection: AsyncConnection, page, limit) -> Op -async def get_processschema_by_title(connection: AsyncConnection, title: str) -> Optional[ProcessSchema]: +async def get_process_schema_by_title(connection: AsyncConnection, title: str) -> Optional[ProcessSchema]: """ Получает process schema по title. """ query = select(process_schema_table).where(process_schema_table.c.title == title) processschema_db_cursor = await connection.execute(query) - processschema_db = processschema_db_cursor.one_or_none() - if not processschema_db: + processschema_data = processschema_db_cursor.mappings().one_or_none() + if not processschema_data: return None - processschema_data = { - column.name: ( - getattr(processschema_db, column.name).name - if isinstance(getattr(processschema_db, column.name), Enum) - else getattr(processschema_db, column.name) - ) - for column in process_schema_table.columns - } - return ProcessSchema.model_validate(processschema_data) -async def get_processschema_by_id(connection: AsyncConnection, id: int) -> Optional[ProcessSchema]: +async def get_process_schema_by_id(connection: AsyncConnection, id: int) -> Optional[ProcessSchema]: """ Получает processschema по id. """ query = select(process_schema_table).where(process_schema_table.c.id == id) processschema_db_cursor = await connection.execute(query) - processschema_db = processschema_db_cursor.one_or_none() - if not processschema_db: + + processschema_data = processschema_db_cursor.mappings().one_or_none() + if not processschema_data: return None - processschema_data = { - column.name: ( - getattr(processschema_db, column.name).name - if isinstance(getattr(processschema_db, column.name), Enum) - else getattr(processschema_db, column.name) - ) - for column in process_schema_table.columns - } - return ProcessSchema.model_validate(processschema_data) -async def update_processschema_by_id(connection: AsyncConnection, update_values, processschema): +async def update_process_schema_by_id(connection: AsyncConnection, update_values, processschema): """ Вносит изменеия в нужное поле таблицы process_schema_table. """ @@ -163,7 +146,7 @@ async def update_processschema_by_id(connection: AsyncConnection, update_values, await connection.commit() -async def create_processschema(connection: AsyncConnection, processschema: ProcessSchema, creator_id: int) -> Optional[ProcessSchema]: +async def create_process_schema(connection: AsyncConnection, processschema: ProcessSchema, creator_id: int) -> Optional[ProcessSchema]: """ Создает нове поле в таблице process_schema_table. """ diff --git a/api/api/endpoints/account.py b/api/api/endpoints/account.py index 5ce0da4..daa0d93 100644 --- a/api/api/endpoints/account.py +++ b/api/api/endpoints/account.py @@ -98,14 +98,13 @@ async def update_account( if user_update.password is not None: await update_password_key(connection, user.id, user_update.password) - update_values = update_user_data_changes(user_update, user) - if update_values is None: + updated_values = user_update.model_dump(by_alias=True, exclude_none=True) + + if not updated_values: return user - user_update_data = UserUpdate.model_validate({**user.model_dump(), **update_values}) - - await update_user_by_id(connection, update_values, user) + await update_user_by_id(connection, updated_values, user) user = await get_user_by_id(connection, user_id) @@ -126,12 +125,12 @@ async def delete_account( user_update = UserUpdate(status=AccountStatus.DELETED.value) - update_values = update_user_data_changes(user_update, user) + updated_values = user_update.model_dump(by_alias=True, exclude_none=True) - if update_values is None: + if not updated_values: return user - await update_user_by_id(connection, update_values, user) + await update_user_by_id(connection, updated_values, user) user = await get_user_by_id(connection, user_id) diff --git a/api/api/endpoints/keyring.py b/api/api/endpoints/keyring.py index 3f09f61..20990c8 100644 --- a/api/api/endpoints/keyring.py +++ b/api/api/endpoints/keyring.py @@ -87,14 +87,12 @@ async def update_keyring( if keyring is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="keyring not found") - update_values = update_key_data_changes(keyring_update, keyring) + updated_values = keyring_update.model_dump(by_alias=True, exclude_none=True) - if update_values is None: + if not updated_values: return keyring - keyring_update_data = AccountKeyring.model_validate({**keyring.model_dump(), **update_values}) - - await update_key_by_id(connection, update_values, keyring) + await update_key_by_id(connection, updated_values, keyring) keyring = await get_key_by_id(connection, key_id) @@ -116,12 +114,12 @@ async def delete_keyring( keyring_update = AccountKeyringUpdate(status=KeyStatus.DELETED.value) - update_values = update_key_data_changes(keyring_update, keyring) + updated_values = keyring_update.model_dump(by_alias=True, exclude_none=True) - if update_values is None: + if not updated_values: return keyring - await update_key_by_id(connection, update_values, keyring) + await update_key_by_id(connection, updated_values, keyring) keyring = await get_key_by_id(connection, key_id) diff --git a/api/api/endpoints/listevents.py b/api/api/endpoints/listevents.py index d93274f..914d37d 100644 --- a/api/api/endpoints/listevents.py +++ b/api/api/endpoints/listevents.py @@ -54,12 +54,12 @@ async def get_all_list_events( authorize_user, page_flag = await db_user_role_validation_for_listevents_and_processschema(connection, current_user) if page_flag: - list_eventspage = await get_listevents_page(connection, page, limit) + list_events_page = await get_listevents_page(connection, page, limit) - if list_eventspage is None: + if list_events_page is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="List events not found") - return list_eventspage + return list_events_page else: list_events_page = await get_listevents_page_by_creator_id(connection, authorize_user.id, page, limit) @@ -126,14 +126,12 @@ async def update_list_events( connection, current_user, listevents_validation.creator_id ) - update_values = update_listevents_data_changes(listevents_update, listevents_validation) + updated_values = listevents_update.model_dump(by_alias=True, exclude_none=True) - if update_values is None: + if not updated_values: return listevents_validation - listevents_update_data = ListEvent.model_validate({**listevents_validation.model_dump(), **update_values}) - - await update_listevents_by_id(connection, update_values, listevents_validation) + await update_listevents_by_id(connection, updated_values, listevents_validation) listevents = await get_listevents_by_id(connection, listevents_id) @@ -157,12 +155,12 @@ async def delete_list_events( listevents_update = ListEventUpdate(status=EventStatus.DELETED.value) - update_values = update_listevents_data_changes(listevents_update, listevents_validation) + updated_values = listevents_update.model_dump(by_alias=True, exclude_none=True) - if update_values is None: + if not updated_values: return listevents_validation - await update_listevents_by_id(connection, update_values, listevents_validation) + await update_listevents_by_id(connection, updated_values, listevents_validation) listevents = await get_listevents_by_id(connection, listevents_id) diff --git a/api/api/endpoints/processschema.py b/api/api/endpoints/processschema.py index e0e67f8..5e3401c 100644 --- a/api/api/endpoints/processschema.py +++ b/api/api/endpoints/processschema.py @@ -12,12 +12,12 @@ from api.db.connection.session import get_connection_dep from api.db.logic.account import get_user_by_login from api.db.logic.processschema import ( - get_processschema_by_title, - create_processschema, - get_processschema_by_id, - update_processschema_by_id, - get_processschema_page_by_creator_id, - get_processschema_page + get_process_schema_by_title, + create_process_schema, + get_process_schema_by_id, + update_process_schema_by_id, + get_process_schema_page_by_creator_id, + get_process_schema_page ) from api.schemas.process.process_schema import ProcessSchema @@ -42,8 +42,6 @@ api_router = APIRouter( tags=["process schema"], ) - - @api_router.get("", dependencies=[Depends(bearer_schema)], response_model=AllProcessSchemaResponse) async def get_all_process_schema( page: int = 1, @@ -54,14 +52,14 @@ async def get_all_process_schema( authorize_user, page_flag = await db_user_role_validation_for_listevents_and_processschema(connection, current_user) if page_flag: - process_schemapage = await get_processschema_page(connection, page, limit) + process_schema_page = await get_process_schema_page(connection, page, limit) - if process_schemapage is None: + if process_schema_page is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Process schema not found") - return process_schemapage + return process_schema_page else: - process_schema_page = await get_processschema_page_by_creator_id(connection, authorize_user.id, page, limit) + process_schema_page = await get_process_schema_page_by_creator_id(connection, authorize_user.id, page, limit) if process_schema_page is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Process schema not found") @@ -75,7 +73,7 @@ async def get_process_schema( connection: AsyncConnection = Depends(get_connection_dep), current_user=Depends(get_current_user), ): - processschema_validation = await get_processschema_by_id(connection, processschema_id) + processschema_validation = await get_process_schema_by_id(connection, processschema_id) if processschema_validation is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Process schema not found") @@ -90,17 +88,17 @@ async def get_process_schema( return processschema_validation @api_router.post("", dependencies=[Depends(bearer_schema)], response_model=ProcessSchema) -async def create_process_schema( +async def create_processschema( processschema: ProcessSchemaUpdate, connection: AsyncConnection = Depends(get_connection_dep), current_user=Depends(get_current_user), ): user_validation = await get_user_by_login(connection, current_user) - processschema_validation = await get_processschema_by_title(connection, processschema.title) + processschema_validation = await get_process_schema_by_title(connection, processschema.title) if processschema_validation is None: - await create_processschema(connection, processschema, user_validation.id) - processschema_new = await get_processschema_by_title(connection, processschema.title) + await create_process_schema(connection, processschema, user_validation.id) + processschema_new = await get_process_schema_by_title(connection, processschema.title) return processschema_new else: @@ -116,7 +114,7 @@ async def update_process_schema( connection: AsyncConnection = Depends(get_connection_dep), current_user=Depends(get_current_user), ): - processschema_validation = await get_processschema_by_id(connection, processschema_id) + processschema_validation = await get_process_schema_by_id(connection, processschema_id) if processschema_validation is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Process schema not found") @@ -125,16 +123,14 @@ async def update_process_schema( connection, current_user, processschema_validation.creator_id ) - update_values = update_processschema_data_changes(processschema_update, processschema_validation) + updated_values = processschema_update.model_dump(by_alias=True, exclude_none=True) - if update_values is None: + if not updated_values: return processschema_validation - processschema_update_data = ProcessSchema.model_validate({**processschema_validation.model_dump(), **update_values}) + await update_process_schema_by_id(connection, updated_values, processschema_validation) - await update_processschema_by_id(connection, update_values, processschema_validation) - - processschema = await get_processschema_by_id(connection, processschema_id) + processschema = await get_process_schema_by_id(connection, processschema_id) return processschema @@ -144,7 +140,7 @@ async def delete_process_schema( connection: AsyncConnection = Depends(get_connection_dep), current_user=Depends(get_current_user), ): - processschema_validation = await get_processschema_by_id(connection, processschema_id) + processschema_validation = await get_process_schema_by_id(connection, processschema_id) if processschema_validation is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Process schema not found") @@ -155,13 +151,13 @@ async def delete_process_schema( processschema_update = ProcessSchemaUpdate(status=ProcessStatus.DELETED.value) - update_values = update_processschema_data_changes(processschema_update, processschema_validation) + updated_values = processschema_update.model_dump(by_alias=True, exclude_none=True) - if update_values is None: + if not updated_values: return processschema_validation - await update_processschema_by_id(connection, update_values, processschema_validation) + await update_process_schema_by_id(connection, updated_values, processschema_validation) - processschema = await get_processschema_by_id(connection, processschema_id) + processschema = await get_process_schema_by_id(connection, processschema_id) return processschema diff --git a/api/api/endpoints/profile.py b/api/api/endpoints/profile.py index 43ecf7c..407e627 100644 --- a/api/api/endpoints/profile.py +++ b/api/api/endpoints/profile.py @@ -38,7 +38,7 @@ async def get_profile( @api_router.put("", dependencies=[Depends(bearer_schema)], response_model=User) async def update_profile( - user_updata: UserUpdate, + user_update: UserUpdate, connection: AsyncConnection = Depends(get_connection_dep), current_user=Depends(get_current_user), ): @@ -46,15 +46,16 @@ async def update_profile( if user is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found") - if user_updata.role == None and user_updata.login == None: - update_values = update_user_data_changes(user_updata, user) + if user_update.role is None and user_update.login is None: - if update_values is None: + updated_values = user_update.model_dump(by_alias=True, exclude_none=True) + + if updated_values is None: return user - await update_user_by_id(connection, update_values, user) + await update_user_by_id(connection, updated_values, user) - user = await get_user_by_id(connection, user.id) + user = await get_user_by_id(connection, user.id) return user else: