From d2eea3865a130f9b5e0f2011e2b901d69a5d3ab2 Mon Sep 17 00:00:00 2001 From: Stanislav Roslavtsev Date: Thu, 27 Jul 2023 18:14:34 +0300 Subject: [PATCH 1/8] Exceptions fix + Pv2 --- file_converter/routes/base.py | 4 ++-- file_converter/routes/file.py | 6 +++--- file_converter/settings.py | 9 +++------ tests/test_routes/test_converter.py | 2 +- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/file_converter/routes/base.py b/file_converter/routes/base.py index 250ec17..b837375 100644 --- a/file_converter/routes/base.py +++ b/file_converter/routes/base.py @@ -48,8 +48,8 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) - app.add_middleware(LimitUploadSize, max_upload_size=settings.MAX_SIZE) -@app.exception_handler(aiohttp.client_exceptions.ClientConnectorError) -async def not_found_error(request: Request, exc: aiohttp.client_exceptions.ClientConnectorError): +@app.exception_handler(aiohttp.ClientConnectorError) +async def not_found_error(request: Request, exc: aiohttp.ClientConnectorError): raise HTTPException(404, f"request failed: {exc} ") diff --git a/file_converter/routes/file.py b/file_converter/routes/file.py index a11520b..f5419ce 100644 --- a/file_converter/routes/file.py +++ b/file_converter/routes/file.py @@ -33,14 +33,14 @@ async def process( except UnsupportedToExt: raise HTTPException( - status_code=415, detail=f'Files are allowed to be converted only to {", ".join(settings.CONVERT_TYPES)}' + status_code=415, detail=f'Files are allowed to be converted only to {", ".join(str(settings.CONVERT_TYPES))}' ) except ConvertError: raise HTTPException(status_code=400, detail='Posted file is corrupted') except ForbiddenExt: - raise HTTPException(status_code=415, detail=f'Only {", ".join(settings.EXTENTIONS)} files are allowed.') + raise HTTPException(status_code=415, detail=f'Only {", ".join(str(settings.EXTENTIONS))} files are allowed.') root_path = settings.ROOT_PATH.removesuffix('/') - return {"status": "Success", "file_url": f'{root_path}/{settings.STATIC_FOLDER}/{result}'} # Отдает URL на файл + return {"status": "Success", "file_url": f'{root_path}/{str(settings.STATIC_FOLDER)}/{result}'} # Отдает URL на файл diff --git a/file_converter/settings.py b/file_converter/settings.py index 1400e9d..56d0d3b 100644 --- a/file_converter/settings.py +++ b/file_converter/settings.py @@ -2,7 +2,8 @@ from functools import lru_cache from typing import List -from pydantic import BaseSettings, DirectoryPath +from pydantic import DirectoryPath, ConfigDict +from pydantic_settings import BaseSettings class Settings(BaseSettings): @@ -18,11 +19,7 @@ class Settings(BaseSettings): MAX_SIZE: int = 5000000 # Максимальный размер файла в байтах STATIC_FOLDER: DirectoryPath | None = "static" - class Config: - """Pydantic BaseSettings config""" - - case_sensitive = True - env_file = ".env" + model_config = ConfigDict(case_sensitive=True, env_file=".env", extra="ignore") @lru_cache diff --git a/tests/test_routes/test_converter.py b/tests/test_routes/test_converter.py index f4e6f61..5f74499 100644 --- a/tests/test_routes/test_converter.py +++ b/tests/test_routes/test_converter.py @@ -87,4 +87,4 @@ def test_post_broken_ext(client): def test_extensions(client): req = client.get("/extensions") assert req.status_code == 200 - assert req.json() == {"in": settings.CONVERT_TYPES, "out": settings.EXTENTIONS} + assert req.json() == {"in": str(settings.CONVERT_TYPES), "out": str(settings.EXTENTIONS)} From a21b07f4ecfb326782403051606b936c8056e482 Mon Sep 17 00:00:00 2001 From: Stanislav Roslavtsev Date: Thu, 27 Jul 2023 18:15:51 +0300 Subject: [PATCH 2/8] Black + isort --- file_converter/routes/file.py | 8 ++++++-- file_converter/settings.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/file_converter/routes/file.py b/file_converter/routes/file.py index f5419ce..49fd503 100644 --- a/file_converter/routes/file.py +++ b/file_converter/routes/file.py @@ -33,7 +33,8 @@ async def process( except UnsupportedToExt: raise HTTPException( - status_code=415, detail=f'Files are allowed to be converted only to {", ".join(str(settings.CONVERT_TYPES))}' + status_code=415, + detail=f'Files are allowed to be converted only to {", ".join(str(settings.CONVERT_TYPES))}', ) except ConvertError: @@ -43,4 +44,7 @@ async def process( raise HTTPException(status_code=415, detail=f'Only {", ".join(str(settings.EXTENTIONS))} files are allowed.') root_path = settings.ROOT_PATH.removesuffix('/') - return {"status": "Success", "file_url": f'{root_path}/{str(settings.STATIC_FOLDER)}/{result}'} # Отдает URL на файл + return { + "status": "Success", + "file_url": f'{root_path}/{str(settings.STATIC_FOLDER)}/{result}', + } # Отдает URL на файл diff --git a/file_converter/settings.py b/file_converter/settings.py index 56d0d3b..f650996 100644 --- a/file_converter/settings.py +++ b/file_converter/settings.py @@ -2,7 +2,7 @@ from functools import lru_cache from typing import List -from pydantic import DirectoryPath, ConfigDict +from pydantic import ConfigDict, DirectoryPath from pydantic_settings import BaseSettings From af817745ae7b98631b3f247c25cd2e6d44ca3b36 Mon Sep 17 00:00:00 2001 From: Stanislav Roslavtsev Date: Thu, 27 Jul 2023 18:39:00 +0300 Subject: [PATCH 3/8] Fix test --- file_converter/routes/base.py | 6 +++--- file_converter/routes/file.py | 2 +- tests/test_routes/test_converter.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/file_converter/routes/base.py b/file_converter/routes/base.py index b837375..bdc5fcb 100644 --- a/file_converter/routes/base.py +++ b/file_converter/routes/base.py @@ -21,7 +21,7 @@ description='Серверная часть сервиса конвертации и хранения пользовательских документов', version=__version__, # Отключаем нелокальную документацию - root_path=settings.ROOT_PATH if __version__ != 'dev' else '/', + root_path=str(settings.ROOT_PATH) if __version__ != 'dev' else '/', docs_url=None if __version__ != 'dev' else '/docs', redoc_url=None, ) @@ -40,12 +40,12 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) - if 'content-length' not in request.headers: return Response(status_code=status.HTTP_411_LENGTH_REQUIRED) content_length = int(request.headers['content-length']) - if content_length > self.max_upload_size: + if content_length > int(self.max_upload_size): return Response(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE) return await call_next(request) -app.add_middleware(LimitUploadSize, max_upload_size=settings.MAX_SIZE) +app.add_middleware(LimitUploadSize, max_upload_size=str(settings.MAX_SIZE)) @app.exception_handler(aiohttp.ClientConnectorError) diff --git a/file_converter/routes/file.py b/file_converter/routes/file.py index 49fd503..b31738d 100644 --- a/file_converter/routes/file.py +++ b/file_converter/routes/file.py @@ -43,7 +43,7 @@ async def process( except ForbiddenExt: raise HTTPException(status_code=415, detail=f'Only {", ".join(str(settings.EXTENTIONS))} files are allowed.') - root_path = settings.ROOT_PATH.removesuffix('/') + root_path = str(settings.ROOT_PATH).removesuffix('/') return { "status": "Success", "file_url": f'{root_path}/{str(settings.STATIC_FOLDER)}/{result}', diff --git a/tests/test_routes/test_converter.py b/tests/test_routes/test_converter.py index 5f74499..f4e6f61 100644 --- a/tests/test_routes/test_converter.py +++ b/tests/test_routes/test_converter.py @@ -87,4 +87,4 @@ def test_post_broken_ext(client): def test_extensions(client): req = client.get("/extensions") assert req.status_code == 200 - assert req.json() == {"in": str(settings.CONVERT_TYPES), "out": str(settings.EXTENTIONS)} + assert req.json() == {"in": settings.CONVERT_TYPES, "out": settings.EXTENTIONS} From c3f781bbf3c47673ba3fc43e2169e8a70de00275 Mon Sep 17 00:00:00 2001 From: Stanislav Roslavtsev Date: Thu, 27 Jul 2023 18:58:13 +0300 Subject: [PATCH 4/8] Update requirements.txt --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 22f252c..14af476 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ fastapi -pydantic[dotenv] +pydantic +pydantic_settings uvicorn gunicorn aiofiles From e56c6fbcde1031a92d23f9422a5ffac6c2436634 Mon Sep 17 00:00:00 2001 From: Stanislav Roslavtsev Date: Mon, 31 Jul 2023 21:14:33 +0300 Subject: [PATCH 5/8] Fixing --- file_converter/routes/base.py | 2 +- file_converter/routes/file.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/file_converter/routes/base.py b/file_converter/routes/base.py index bdc5fcb..0630ea6 100644 --- a/file_converter/routes/base.py +++ b/file_converter/routes/base.py @@ -40,7 +40,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) - if 'content-length' not in request.headers: return Response(status_code=status.HTTP_411_LENGTH_REQUIRED) content_length = int(request.headers['content-length']) - if content_length > int(self.max_upload_size): + if content_length > self.max_upload_size: return Response(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE) return await call_next(request) diff --git a/file_converter/routes/file.py b/file_converter/routes/file.py index b31738d..e24095e 100644 --- a/file_converter/routes/file.py +++ b/file_converter/routes/file.py @@ -34,14 +34,14 @@ async def process( except UnsupportedToExt: raise HTTPException( status_code=415, - detail=f'Files are allowed to be converted only to {", ".join(str(settings.CONVERT_TYPES))}', + detail=f'Files are allowed to be converted only to {", ".join(settings.CONVERT_TYPES)}', ) except ConvertError: raise HTTPException(status_code=400, detail='Posted file is corrupted') except ForbiddenExt: - raise HTTPException(status_code=415, detail=f'Only {", ".join(str(settings.EXTENTIONS))} files are allowed.') + raise HTTPException(status_code=415, detail=f'Only {", ".join(settings.EXTENTIONS)} files are allowed.') root_path = str(settings.ROOT_PATH).removesuffix('/') return { From bc444d8a486ad3be77017ffec8c83ccab27e1d22 Mon Sep 17 00:00:00 2001 From: Stanislav Roslavtsev Date: Mon, 31 Jul 2023 21:14:47 +0300 Subject: [PATCH 6/8] Update file.py --- file_converter/routes/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file_converter/routes/file.py b/file_converter/routes/file.py index e24095e..cf1e4a8 100644 --- a/file_converter/routes/file.py +++ b/file_converter/routes/file.py @@ -34,7 +34,7 @@ async def process( except UnsupportedToExt: raise HTTPException( status_code=415, - detail=f'Files are allowed to be converted only to {", ".join(settings.CONVERT_TYPES)}', + detail=f'Files are allowed to be converted only to {", ".join(str(settings.CONVERT_TYPES))}', ) except ConvertError: From 8e9e888d84dd838ce570ffbdcf571d7cecf776d2 Mon Sep 17 00:00:00 2001 From: Stanislav Roslavtsev Date: Tue, 1 Aug 2023 17:18:05 +0300 Subject: [PATCH 7/8] Fix --- file_converter/routes/base.py | 2 +- file_converter/routes/file.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/file_converter/routes/base.py b/file_converter/routes/base.py index 0630ea6..6a1060a 100644 --- a/file_converter/routes/base.py +++ b/file_converter/routes/base.py @@ -21,7 +21,7 @@ description='Серверная часть сервиса конвертации и хранения пользовательских документов', version=__version__, # Отключаем нелокальную документацию - root_path=str(settings.ROOT_PATH) if __version__ != 'dev' else '/', + root_path=settings.ROOT_PATH if __version__ != 'dev' else '/', docs_url=None if __version__ != 'dev' else '/docs', redoc_url=None, ) diff --git a/file_converter/routes/file.py b/file_converter/routes/file.py index cf1e4a8..d8b65c4 100644 --- a/file_converter/routes/file.py +++ b/file_converter/routes/file.py @@ -43,7 +43,7 @@ async def process( except ForbiddenExt: raise HTTPException(status_code=415, detail=f'Only {", ".join(settings.EXTENTIONS)} files are allowed.') - root_path = str(settings.ROOT_PATH).removesuffix('/') + root_path = settings.ROOT_PATH.removesuffix('/') return { "status": "Success", "file_url": f'{root_path}/{str(settings.STATIC_FOLDER)}/{result}', From cf2b60cf03dfda68ef7a6bbe201ccfd9edb64651 Mon Sep 17 00:00:00 2001 From: Stanislav Roslavtsev Date: Thu, 3 Aug 2023 12:05:41 +0300 Subject: [PATCH 8/8] Fix (again) --- file_converter/routes/base.py | 2 +- file_converter/routes/file.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/file_converter/routes/base.py b/file_converter/routes/base.py index 6a1060a..b837375 100644 --- a/file_converter/routes/base.py +++ b/file_converter/routes/base.py @@ -45,7 +45,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) - return await call_next(request) -app.add_middleware(LimitUploadSize, max_upload_size=str(settings.MAX_SIZE)) +app.add_middleware(LimitUploadSize, max_upload_size=settings.MAX_SIZE) @app.exception_handler(aiohttp.ClientConnectorError) diff --git a/file_converter/routes/file.py b/file_converter/routes/file.py index d8b65c4..ce3d40c 100644 --- a/file_converter/routes/file.py +++ b/file_converter/routes/file.py @@ -34,7 +34,7 @@ async def process( except UnsupportedToExt: raise HTTPException( status_code=415, - detail=f'Files are allowed to be converted only to {", ".join(str(settings.CONVERT_TYPES))}', + detail=f'Files are allowed to be converted only to {", ".join(settings.CONVERT_TYPES)}', ) except ConvertError: