Utils and better handling
Some checks failed
/ backend-pylint (push) Successful in 1m30s
/ build-artifacts (push) Successful in 16s
/ publish-artifacts (push) Successful in 1m7s
/ release (push) Failing after 22m48s
/ build-and-push-container (push) Successful in 3m14s

This commit is contained in:
Firq 2024-10-04 15:03:44 +02:00
parent 1eafc1a0e1
commit 8d2de1766b
Signed by: Firq
GPG key ID: 3ACC61C8CEC83C20
6 changed files with 36 additions and 12 deletions

View file

@ -1,6 +1,6 @@
[project] [project]
name = "support_formatter" name = "support_formatter"
version = "0.2.0" version = "0.2.1"
requires-python = ">= 3.10" requires-python = ">= 3.10"
authors = [{name = "Firq", email = "firelp42@gmail.com"}] authors = [{name = "Firq", email = "firelp42@gmail.com"}]
maintainers = [{name = "Firq", email = "firelp42@gmail.com"}] maintainers = [{name = "Firq", email = "firelp42@gmail.com"}]

View file

@ -1,6 +1,9 @@
import csv import csv
import pathlib import pathlib
from ..utils import convert_to_bool
class FileTypeInvalidError(ValueError): class FileTypeInvalidError(ValueError):
pass pass
@ -30,7 +33,7 @@ def process_csv(path: pathlib.Path):
entry.update({ entry.update({
"level": int(entry['level']), "level": int(entry['level']),
"np_level": int(entry['np_level']), "np_level": int(entry['np_level']),
"bce": bool(entry["bce"]) "bce": convert_to_bool(entry["bce"])
}) })
else: else:
entry.update({ entry.update({

View file

@ -1,6 +1,8 @@
from enum import Enum from enum import Enum
import marshmallow as ma import marshmallow as ma
class HealthStatus(Enum): class HealthStatus(Enum):
OK = 0 OK = 0
WARNING = 1 WARNING = 1

View file

@ -6,7 +6,6 @@ from flask.views import MethodView
from ..app import Application from ..app import Application
from ..config import APISettings from ..config import APISettings
from ..models.interface import ApiVersionGet, HealthGet, HealthStatus, OpenAPIGet from ..models.interface import ApiVersionGet, HealthGet, HealthStatus, OpenAPIGet
from . import interface_routes as blp from . import interface_routes as blp
APP = Application.get_instance() APP = Application.get_instance()

View file

@ -1,15 +1,16 @@
import os import os
import pathlib import pathlib
import uuid
from typing import List from typing import List
import marshmallow as ma import marshmallow as ma
from flask_smorest.fields import Upload from flask_smorest.fields import Upload
from werkzeug.datastructures import FileStorage from werkzeug.datastructures import FileStorage
import uuid
from ..app import Application from ..app import Application
from ..config import APISettings from ..config import APISettings
from ..logic.csv_processor import process_csv, FileTypeInvalidError from ..logic.csv_processor import FileTypeInvalidError, process_csv
from ..utils import generate_error
from . import formatter_routes as blp from . import formatter_routes as blp
APP = Application.get_instance() APP = Application.get_instance()
@ -38,28 +39,28 @@ def upload_file(form: dict[str, str], files: dict[str, FileStorage]):
for name, file in files.items(): for name, file in files.items():
if name not in ("servantdata", "cedata"): if name not in ("servantdata", "cedata"):
return { "status": 406, "message": "Invalid form sent" }, 406 return generate_error(406, message="Invalid form sent")
filepath = APISettings.FILE_SAVE_DIRECTORY / f"{uuid.uuid4()}.csv" filepath = APISettings.FILE_SAVE_DIRECTORY / f"{uuid.uuid4()}.csv"
file.save(filepath) file.save(filepath)
if os.stat(filepath).st_size < 1 or not allowed_file(file.filename): if os.stat(filepath).st_size < 1 or not allowed_file(file.filename):
filepath.unlink() filepath.unlink(missing_ok=True)
continue continue
filepaths.append(filepath) filepaths.append(filepath)
if len(filepaths) == 0: if len(filepaths) == 0:
return { "status": 406, "message": "No files provided" }, 406 return generate_error(406, message="No files provided")
try: try:
for f in filepaths: for f in filepaths:
result = process_csv(f) result = process_csv(f)
returndata = returndata | result returndata = returndata | result
f.unlink() f.unlink(missing_ok=True)
except FileTypeInvalidError: except FileTypeInvalidError:
for f in filepaths: for f in filepaths:
f.unlink() f.unlink(missing_ok=True)
return { "status": 500, "message": "Error whilst parsing uploaded file - please contact Firq on Fate/Sacc Order" }, 406 return generate_error(500, message="Error whilst parsing uploaded file - please contact Firq on Fate/Sacc Order")
return returndata return returndata

View file

@ -0,0 +1,19 @@
from typing import Any, Optional, Tuple, TypedDict
class ErrorReturn(TypedDict):
status: int
message: Optional[str]
details: Optional[dict[str, Any]]
def generate_error(status: int, message: Optional[str]=None, additional_data: Optional[dict[str, Any]]=None) -> Tuple[ErrorReturn, int]:
obj: ErrorReturn = { "status": status }
if message is not None:
obj |= { "message": message }
if additional_data is not None:
obj |= { "details": additional_data }
return obj, status
def convert_to_bool(val: str) -> bool:
return bool(val) if str(val).lower() not in ("null", "none") else False