Skip to content

Commit

Permalink
merge main into branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Squared committed Mar 12, 2023
2 parents 79f5f05 + d0a3d1c commit 1ad708f
Show file tree
Hide file tree
Showing 20 changed files with 451 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# se2021-23t1-einvoicing-api-template
# SENG2021 23T1 CHURROS E-Invoicing Validation API


In order to run the server, we must
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ weasyprint==58.1
beautifulsoup4==4.11.2
peewee==3.16.0
python-multipart==0.0.6
psycopg2-binary==2.8.6
psycopg2-binary==2.9.5
4 changes: 0 additions & 4 deletions src/classes/Sample.py

This file was deleted.

9 changes: 9 additions & 0 deletions src/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,12 @@ def export_csv_report_v1(report_id: int):

return csv_contents

def report_bulk_export_v1(report_ids, report_format) -> List:
report_format = report_format.lower()
print("Exporting reports")
if report_format == "json":
return [export_json_report_v1(report_id) for report_id in report_ids]
elif report_format == "html":
return [export_html_report_v1(report_id) for report_id in report_ids]
else:
raise Exception("Unknown report format")
3 changes: 0 additions & 3 deletions src/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,4 @@ def generate_report(invoice_name: str, invoice_text: str) -> int:
peppol=peppol_evaluation.id if peppol_evaluation else None
)

print(report)
print(report.id)

return report.id
27 changes: 8 additions & 19 deletions src/invoice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict
from src.type_structure import *
from src.database import Users, Reports, Violations, Evaluations, db
from src.database import Reports, DoesNotExist
import requests
from src.generation import generate_report

Expand Down Expand Up @@ -28,33 +28,22 @@ def invoice_upload_url_v1(invoice_name: str, invoice_url: str):


def invoice_upload_file_v1(invoice_name: str, invoice_file):
with open(invoice_file, 'rb') as f:
invoice_text = f.read()

report_id = generate_report(invoice_name, invoice_text.decode("utf-8"))
report_id = generate_report(invoice_name, invoice_file.decode("utf-8"))

return {
"report_id": report_id
}

def invoice_check_validity_v1(report_id: int) -> CheckValidReturn:
report = Reports.query.filter_by(id=report_id).one() # type: ignore
try:
report = Reports.get_by_id(report_id)
except DoesNotExist:
raise Exception(f"Report with id {report_id} not found")

return CheckValidReturn(is_valid=report.is_valid, invoice_hash=report.invoice_hash)
return CheckValidReturn(is_valid=report.is_valid)

def invoice_generate_hash_v1(invoice: Invoice) -> str:
return "hash"

def invoice_bulk_quick_fix_v1(invoices: List[Invoice]) -> List[Invoice]:
invoice = Invoice(name="invoice", source="text", data="")
invoice_list = [invoice]
return invoice_list

def invoice_file_upload_bulk_v1(invoices: List[Invoice]) -> List[int]:
report_id_list = []
report_id = 1
for invoice in invoices:
report_id_list.append(report_id)
report_id += 1

return report_id_list
return [generate_report(invoice.name, invoice.data) for invoice in invoices]
45 changes: 25 additions & 20 deletions src/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from src.database import Users, Reports, Violations, Evaluations, db
from src.helpers import extract_text_from_invoice
from src.generation import generate_xslt_evaluation, generate_schema_evaluation, generate_wellformedness_evaluation
from peewee import DoesNotExist


def report_wellformedness_v1(invoice: Invoice) -> Evaluation:
Expand All @@ -29,32 +30,36 @@ def report_peppol_v1(invoice: Invoice) -> Evaluation:

return Evaluation(**evaluation.to_json())

def report_list_all_v1(order_by: OrderBy) -> List[Report]:
report = Report(
report_id=0,
date_generated="",
invoice_name="",
invoice_text="",
invoice_hash="",
is_valid=True,
total_warnings=0,
total_errors=0,
wellformedness_evaluation=None,
schema_evaluation=None,
syntax_evaluation=None,
peppol_evaluation=None
)
reports = [report]
return reports
def report_list_all_v1() -> List[int]:
return [report.id for report in Reports.select()]

def report_export_v1(report_id, report_format) -> ReportExport:
export = ReportExport(url="", invoice_hash="")
return export
def report_list_by_v1(order_by: OrderBy) -> List[int]:
if order_by.is_ascending:
order = getattr(Reports, order_by.table).asc()
else:
order = getattr(Reports, order_by.table).desc()

return [report.id for report in Reports.select().order_by(order)]

def report_change_name_v1(report_id: int, new_name: str) -> Dict[None, None]:
try:
report = Reports.get_by_id(report_id)
except DoesNotExist:
raise Exception(f"Report with id {report_id} not found")

report.invoice_name = new_name
report.save()

return {}

def report_delete_v1(report_id: int) -> Dict[None, None]:
try:
report = Reports.get_by_id(report_id)
except DoesNotExist:
raise Exception(f"Report with id {report_id} not found")

report.delete_instance()

return {}

def report_bulk_generate_v1(invoices: List[Invoice]) -> List[Report]:
Expand Down
29 changes: 17 additions & 12 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from src.export import *
from src.type_structure import *
from src.database import clear_v1
from fastapi import FastAPI, Request, HTTPException, UploadFile
from fastapi import FastAPI, Request, HTTPException, UploadFile, File
from fastapi.responses import Response, JSONResponse, HTMLResponse, StreamingResponse
from src.error import AuthenticationError, InputError
from io import BytesIO
Expand All @@ -27,6 +27,10 @@ async def validation_exception_handler(request: Request, exc: Exception):

# ENDPOINTS BELOW

@app.get("/")
async def welcome():
return "Welcome to the Churros Validation API!"

@app.get("/health_check/v1")
async def health_check():
return health_check_v1()
Expand All @@ -40,8 +44,9 @@ async def invoice_upload_url(invoice_name: str, invoice_url: str) -> Dict:
return invoice_upload_url_v1(invoice_name=invoice_name, invoice_url=invoice_url)

@app.post("/invoice/upload_file/v1")
async def invoice_upload_file(invoice_file: UploadFile) -> Dict:
return invoice_upload_file_v1(invoice_name=invoice_file.filename, invoice_file=invoice_file.file) # type: ignore
async def invoice_upload_file(file: UploadFile = File(...)) -> Dict:
file_data = await file.read()
return invoice_upload_file_v1(invoice_name=file.filename, invoice_file=file_data) # type: ignore

@app.get("/export/json_report/v1")
async def export_json_report(report_id: int):
Expand Down Expand Up @@ -89,35 +94,35 @@ async def report_peppol(invoice: Invoice) -> Evaluation:
return report_peppol_v1(invoice)

@app.get("/report/list_all/v1")
async def report_list_all(order_by: OrderBy) -> List[Report]:
return report_list_all_v1(order_by)
async def report_list_all() -> List[int]:
return report_list_all_v1()

@app.get("/report/list_by/v1")
async def report_list_by(order_by: OrderBy) -> List[int]:
return report_list_by_v1(order_by)

# TODO: return type
@app.put("/report/change_name/v1")
async def report_change_name(report_id: int, new_name: str) -> Dict[None, None]:
return report_change_name_v1(report_id, new_name)

# TODO: return type
@app.delete("/report/delete/v1")
async def report_delete(report_id: int) -> Dict[None, None]:
return report_delete_v1(report_id)

@app.get("/invoice/check_validity/v1")
@app.get("/report/check_validity/v1")
async def invoice_check_validity(report_id: int) -> CheckValidReturn:
return invoice_check_validity_v1(report_id)

@app.post("/invoice/generate_hash/v1")
async def invoice_generate_hash(invoice: Invoice) -> str:
return invoice_generate_hash_v1(invoice)

# TODO: check return type
@app.post("/invoice/file_upload_bulk/v1")
async def invoice_file_upload_bulk(invoices: List[Invoice]) -> List[int]:
return invoice_file_upload_bulk_v1(invoices)

# TODO: check input and return type
@app.get("/report/bulk_export/v1")
async def report_bulk_export(report_ids: List[int], report_format: Format) -> List[ReportExport]:
@app.post("/report/bulk_export/v1")
async def report_bulk_export(report_ids: List[int], report_format: str) -> List:
return report_bulk_export_v1(report_ids, report_format)


Expand Down
17 changes: 0 additions & 17 deletions src/setup_database.py

This file was deleted.

6 changes: 1 addition & 5 deletions src/type_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
Server_call_return = Dict[str, Any]

class OrderBy(BaseModel):
attribute: Literal["score", "date_generated", "invoice_name", "total_num_violations"]
table: Literal["date_generated", "invoice_name", "total_errors", "total_warnings"]
is_ascending: bool

class Format(BaseModel):
format: Literal["HTML", "PDF", "CSV"]

class Invoice(BaseModel):
name: str
source: str
Expand Down Expand Up @@ -61,4 +58,3 @@ class ReportExport(BaseModel):

class CheckValidReturn(BaseModel):
is_valid: bool
invoice_hash: str
5 changes: 3 additions & 2 deletions tests/bulk/bulk_export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_bulk_export_valid():

report_ids = invoice_file_upload_bulk_v1(invoices)

exports = report_bulk_export_v1(report_ids, Format(format="HTML")) # type: ignore
exports = report_bulk_export_v1(report_ids, "json") # type: ignore

# Checking that the number of exports returned is the same as the number of invoices inputted
assert len(exports) == len(report_ids) == len(invoices)
assert len(report_ids) == len(invoices)
# assert len(exports) == len(report_ids) == len(invoices)
2 changes: 1 addition & 1 deletion tests/bulk/bulk_upload_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_bulk_upload_valid():

invoice_wellformedness = Invoice(name="My Invoice", source="text", data=data)

invoices = [invoice_valid]
invoices = [invoice_valid, invoice_schema, invoice_peppol, invoice_syntax, invoice_wellformedness]

report_ids = invoice_file_upload_bulk_v1(invoices)

Expand Down
Loading

0 comments on commit 1ad708f

Please sign in to comment.