From 496dcbcb5882eb2742c51903590b75993d779532 Mon Sep 17 00:00:00 2001 From: Sam <78538841+spwoodcock@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:54:46 +0700 Subject: [PATCH] refactor: bytesio .seek(0) & .read() methods with .getvalue() (#1001) * refactor: replace bytesio .seek(0) with .getvalue() * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/backend/app/central/central_schemas.py | 1 - src/backend/app/main.py | 2 +- src/backend/app/projects/project_routes.py | 33 ++++++++-------------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/backend/app/central/central_schemas.py b/src/backend/app/central/central_schemas.py index 9e84694ffd..d4157ac229 100644 --- a/src/backend/app/central/central_schemas.py +++ b/src/backend/app/central/central_schemas.py @@ -17,7 +17,6 @@ # from enum import Enum -from loguru import logger as log from pydantic import BaseModel diff --git a/src/backend/app/main.py b/src/backend/app/main.py index e144344c7b..a8e93ea0a2 100644 --- a/src/backend/app/main.py +++ b/src/backend/app/main.py @@ -19,8 +19,8 @@ import logging import sys -from typing import Optional from contextlib import asynccontextmanager +from typing import Optional import sentry_sdk from fastapi import FastAPI, Request diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index 8166753702..b9b922b864 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -368,8 +368,7 @@ async def upload_multi_project_boundary( "Uploading project boundary multipolygon for " f"project ID: {project_id}" ) # read entire file - await project_geojson.seek(0) - content = await project_geojson.read() + content = await project_geojson.getvalue() boundary = json.loads(content) # Validatiing Coordinate Reference System @@ -414,8 +413,7 @@ async def task_split( """ # read entire file - await project_geojson.seek(0) - content = await project_geojson.read() + content = await project_geojson.getvalue() boundary = json.loads(content) # Validatiing Coordinate Reference System @@ -454,8 +452,7 @@ async def upload_project_boundary( raise HTTPException(status_code=400, detail="Provide a valid .geojson file") # read entire file - await upload.seek(0) - content = await upload.read() + content = await upload.getvalue() boundary = json.loads(content) # Validatiing Coordinate Reference System @@ -493,8 +490,7 @@ async def edit_project_boundary( raise HTTPException(status_code=400, detail="Provide a valid .geojson file") # read entire file - await upload.seek(0) - content = await upload.read() + content = await upload.getvalue() boundary = json.loads(content) # Validatiing Coordinate Reference System @@ -532,8 +528,7 @@ async def validate_form( if file_ext not in allowed_extensions: raise HTTPException(status_code=400, detail="Provide a valid .xls file") - await form.seek(0) - contents = await form.read() + contents = await form.getvalue() return await central_crud.test_form_validity(contents, file_ext[1:]) @@ -547,8 +542,7 @@ async def generate_files( data_extracts: Optional[UploadFile] = File(None), db: Session = Depends(database.get_db), ): - """ - Generate additional content for the project to function. + """Generate additional content for the project to function. QR codes, @@ -590,8 +584,7 @@ async def generate_files( if file_ext not in allowed_extensions: raise HTTPException(status_code=400, detail="Provide a valid .xls file") xform_title = file_name[0] - await xls_form_upload.seek(0) - custom_xls_form = await xls_form_upload.read() + custom_xls_form = await xls_form_upload.getvalue() project.form_xls = custom_xls_form @@ -602,8 +595,7 @@ async def generate_files( raise HTTPException( status_code=400, detail="Provide a valid .yaml config file" ) - await xls_form_config_file.seek(0) - config_file_contents = await xls_form_config_file.read() + config_file_contents = await xls_form_config_file.getvalue() project.form_config_file = config_file_contents db.commit() @@ -655,8 +647,7 @@ async def get_data_extracts( ): try: # read entire file - await aoi.seek(0) - aoi_content = await aoi.read() + aoi_content = await aoi.getvalue() boundary = json.loads(aoi_content) # Validatiing Coordinate Reference System @@ -819,8 +810,7 @@ async def preview_tasks( raise HTTPException(status_code=400, detail="Provide a valid .geojson file") # read entire file - await project_geojson.seek(0) - content = await project_geojson.read() + content = await project_geojson.getvalue() boundary = json.loads(content) # Validatiing Coordinate Reference System @@ -1187,8 +1177,7 @@ async def generate_files_janakpur( if file_ext not in allowed_extensions: raise HTTPException(status_code=400, detail="Provide a valid .xls file") xform_title = file_name[0] - await form.seek(0) - contents = await form.read() + contents = await form.getvalue() project.form_xls = contents db.commit()