Utils and better handling
This commit is contained in:
parent
1eafc1a0e1
commit
8d2de1766b
6 changed files with 36 additions and 12 deletions
support_formatter/routes
|
@ -6,7 +6,6 @@ from flask.views import MethodView
|
|||
from ..app import Application
|
||||
from ..config import APISettings
|
||||
from ..models.interface import ApiVersionGet, HealthGet, HealthStatus, OpenAPIGet
|
||||
|
||||
from . import interface_routes as blp
|
||||
|
||||
APP = Application.get_instance()
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
import os
|
||||
import pathlib
|
||||
import uuid
|
||||
from typing import List
|
||||
|
||||
import marshmallow as ma
|
||||
from flask_smorest.fields import Upload
|
||||
from werkzeug.datastructures import FileStorage
|
||||
|
||||
import uuid
|
||||
|
||||
from ..app import Application
|
||||
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
|
||||
|
||||
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():
|
||||
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"
|
||||
file.save(filepath)
|
||||
|
||||
if os.stat(filepath).st_size < 1 or not allowed_file(file.filename):
|
||||
filepath.unlink()
|
||||
filepath.unlink(missing_ok=True)
|
||||
continue
|
||||
|
||||
filepaths.append(filepath)
|
||||
|
||||
if len(filepaths) == 0:
|
||||
return { "status": 406, "message": "No files provided" }, 406
|
||||
return generate_error(406, message="No files provided")
|
||||
|
||||
try:
|
||||
for f in filepaths:
|
||||
result = process_csv(f)
|
||||
returndata = returndata | result
|
||||
f.unlink()
|
||||
f.unlink(missing_ok=True)
|
||||
except FileTypeInvalidError:
|
||||
for f in filepaths:
|
||||
f.unlink()
|
||||
return { "status": 500, "message": "Error whilst parsing uploaded file - please contact Firq on Fate/Sacc Order" }, 406
|
||||
f.unlink(missing_ok=True)
|
||||
return generate_error(500, message="Error whilst parsing uploaded file - please contact Firq on Fate/Sacc Order")
|
||||
|
||||
return returndata
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue