Skip to content

Commit a28d3a3

Browse files
fixed type errors and broken reset button
1 parent cff40aa commit a28d3a3

7 files changed

Lines changed: 57 additions & 35 deletions

File tree

overload_web/application/commands/file_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class LoadAllWorkflowFiles:
8989
def execute(
9090
workflow_id: str, storage: ports.FileStorage, repo: ports.SqlRepositoryProtocol
9191
) -> list[files.VendorFile]:
92-
file_list = repo.list(workflow_id)
92+
file_list = repo.list_by_id(workflow_id)
9393
vendor_files = [
9494
files.VendorFile(
9595
file_name=i["filename"], content=storage.load(i["reference"])

overload_web/application/ports.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6-
from typing import (
7-
Any,
8-
BinaryIO,
9-
Iterator,
10-
Protocol,
11-
Sequence,
12-
TypeVar,
13-
runtime_checkable,
14-
)
6+
from typing import Any, Iterator, Protocol, Sequence, TypeVar, runtime_checkable
157

168
logger = logging.getLogger(__name__)
179

@@ -52,11 +44,34 @@ def get_bibs_by_id(
5244

5345
@runtime_checkable
5446
class FileStorage(Protocol):
55-
def load(self, reference: str) -> BinaryIO: ... # pragma: no branch
47+
def load(self, reference: str) -> bytes: ... # pragma: no branch
48+
49+
"""
50+
Load a file.
51+
52+
Args:
53+
reference: the path to the file
54+
55+
Returns:
56+
the content of the specified file as a `bytes` object
57+
"""
58+
5659
def save(
5760
self, id: str, filename: str, content: bytes
5861
) -> str: ... # pragma: no branch
5962

63+
"""
64+
Save a file to a location on storage.
65+
66+
Args:
67+
id: the workflow_id for the file.
68+
filename: the name of the file.
69+
content: the content of the file as a bytes object.
70+
71+
Returns:
72+
the path where the file was saved as a string
73+
"""
74+
6075

6176
@runtime_checkable
6277
class FileLoader(Protocol):
@@ -191,7 +206,13 @@ def list(
191206
self, offset: int | None = 0, limit: int | None = 0
192207
) -> Sequence[dict[str, Any]]: ... # pragma: no branch
193208

194-
"""List objects in a database."""
209+
"""List all objects in a database."""
210+
211+
def list_by_id(
212+
self, id: str | int
213+
) -> Sequence[dict[str, Any]]: ... # pragma: no branch
214+
215+
"""List all objects in a database filtering by a specific id."""
195216

196217
def save(self, obj: T) -> dict[str, Any]: ... # pragma: no branch
197218

overload_web/application/services/bib_processing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from collections import Counter, defaultdict
88
from typing import Any
99

10-
from overload_web.domain.models import bibs
10+
from overload_web.domain.models import bibs, reporting
1111

1212
logger = logging.getLogger(__name__)
1313

@@ -16,7 +16,7 @@ def create_full_records_report(
1616
analysis: list[bibs.MatchAnalysis],
1717
missing_barcodes: list[str],
1818
file_names: list[str],
19-
) -> dict[str, list[Any]]:
19+
) -> reporting.ProcessingStatistics:
2020
"""Generate statistics from a batch of processed full-level records"""
2121
stats = defaultdict(list)
2222
stats["file_names"].extend(file_names)
@@ -27,12 +27,12 @@ def create_full_records_report(
2727
out: dict[str, Any] = dict(stats)
2828
out["total_records"] = len(analysis)
2929
out["total_files"] = len(stats["file_names"])
30-
return out
30+
return reporting.ProcessingStatistics(**out)
3131

3232

3333
def create_order_records_report(
3434
analysis: list[bibs.MatchAnalysis], file_names: list[str]
35-
) -> dict[str, list[Any]]:
35+
) -> reporting.ProcessingStatistics:
3636
"""Generate statistics from a batch of processed order-level records"""
3737
stats = defaultdict(list)
3838
for rec in analysis:
@@ -42,7 +42,7 @@ def create_order_records_report(
4242
out["total_records"] = len(analysis)
4343
out["total_files"] = len(file_names)
4444
out["file_names"] = file_names
45-
return out
45+
return reporting.ProcessingStatistics(**out)
4646

4747

4848
def extract_barcodes(records: list[bibs.DomainBib]) -> list[str]:

overload_web/infrastructure/batch_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def save(self, obj: PVFBatch) -> dict[str, Any]:
133133
ProcessedFileModel.model_validate(i, from_attributes=True)
134134
for i in obj.files
135135
]
136-
valid_stats = PVFReportModel(**obj.report)
136+
valid_stats = PVFReportModel.model_validate(obj.report, from_attributes=True)
137137
valid_batch = PVFBatch(files=valid_files, report=valid_stats)
138138
self.session.add(valid_batch)
139139
self.session.commit()

overload_web/infrastructure/file_io.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import logging
1515
import os
1616
from pathlib import Path
17-
from typing import Any, BinaryIO, Sequence
17+
from typing import Any, Sequence
1818

1919
from file_retriever import Client, File
2020
from sqlmodel import Field, Session, SQLModel, select
@@ -36,8 +36,10 @@ def save(self, id: str, filename: str, content: bytes) -> str:
3636

3737
return str(path)
3838

39-
def load(self, reference: str) -> BinaryIO:
40-
return open(reference, "rb")
39+
def load(self, reference: str) -> bytes:
40+
with open(reference, "rb") as fh:
41+
file = fh.read()
42+
return file
4143

4244

4345
class LocalFileLoader:
@@ -181,21 +183,20 @@ def get(self, id: str | int) -> dict[str, Any] | None:
181183
file = self.session.get(IncomingFileModel, id)
182184
return file.model_dump() if file else None
183185

184-
def list(self, workflow_id: str | int) -> Sequence[dict[str, Any]]:
186+
def list_by_id(self, id: str | int) -> Sequence[dict[str, Any]]:
185187
"""
186188
Retrieve all `IncomingFileModel` objects in the database.
187189
188190
Args:
189-
workflow_id: the `workflow_id` whose files to retrieve.
191+
id: the `workflow_id` whose files to retrieve.
190192
191193
Returns:
192194
a sequence of `IncomingFileModel` objects.
193195
"""
194-
statement = select(IncomingFileModel).where(
195-
IncomingFileModel.workflow_id == workflow_id
196-
)
197-
results = self.session.exec(statement).all()
198-
return [i.model_dump() for i in results]
196+
statement = select(IncomingFileModel).where(IncomingFileModel.workflow_id == id)
197+
results = self.session.exec(statement)
198+
all_files = results.all()
199+
return [i.model_dump() for i in all_files]
199200

200201
def save(self, obj: IncomingFileModel) -> dict[str, Any]:
201202
"""

overload_web/presentation/routers/files.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async def select_ftp_file(
6262
UploadFileToWorkflow(storage=storage, repo=repository).execute(
6363
workflow_id=workflow_id, filename=remote_file, content=file_content.content
6464
)
65-
selected = repository.list(workflow_id)
65+
selected = repository.list_by_id(workflow_id)
6666
return request.app.state.templates.TemplateResponse(
6767
name="pvf_partials/selected_files.html",
6868
request=request,
@@ -82,7 +82,7 @@ async def upload_file(
8282
UploadFileToWorkflow(storage=storage, repo=repository).execute(
8383
workflow_id=workflow_id, filename=str(file.filename), content=file.file.read()
8484
)
85-
selected = repository.list(workflow_id)
85+
selected = repository.list_by_id(workflow_id)
8686
logger.info(f"Current file list: {selected}")
8787
return request.app.state.templates.TemplateResponse(
8888
name="pvf_partials/selected_files.html",
@@ -99,7 +99,7 @@ async def remove_file(
9999
workflow_id: str = Form(),
100100
):
101101
DeleteFileFromWorkflow.execute(id=file_id, repo=repository)
102-
selected = repository.list(workflow_id)
102+
selected = repository.list_by_id(workflow_id)
103103
logger.info(f"Current file list: {selected}")
104104
return request.app.state.templates.TemplateResponse(
105105
"pvf_partials/selected_files.html", {"request": request, "files": selected}

overload_web/templates/pvf_partials/pvf_results.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
<div class="pvf-button-section">
3838
<div class="row p-3">
3939
<div class="col">
40-
<button id="pvf-button"
41-
class="btn btn-nypl"
42-
href='{{ url_for("vendor_file_page") }}'
43-
type="submit">Reset form</button>
40+
<a id="pvf-button"
41+
class="btn btn-nypl"
42+
href='{{ url_for("vendor_file_page") }}'
43+
type="submit">Reset form</a>
4444
</div>
4545
</div>
4646
</div>

0 commit comments

Comments
 (0)