|
1 | | -from fastapi import FastAPI, UploadFile, File |
2 | | -from fastapi.responses import FileResponse |
3 | | -from models.Request import ReportCreateRequest |
4 | | -from helpers.render import RenderClass |
5 | | -from helpers.docx2pdf import Converter |
| 1 | +from fastapi import FastAPI |
6 | 2 | import uvicorn |
7 | | -import os, base64 |
8 | | -import csv, time |
9 | | -from pdf2image import convert_from_path |
| 3 | +from api import api |
10 | 4 |
|
11 | | - |
12 | | -app = FastAPI() |
13 | | - |
14 | | -@app.post("/pdf",summary="Creates a pdf report.", tags=["Report"]) |
15 | | -def CreatePDFReport(body: ReportCreateRequest): |
16 | | - timestamp = time.time() |
17 | | - RenderClass.render(body, timestamp) |
18 | | - return FileResponse(Converter.docx2pdf(timestamp)) |
19 | | - |
20 | | -@app.post("/csv",summary="Creates a csv report.", tags=["Report"]) |
21 | | -def CreatePDFReport(body: ReportCreateRequest): |
22 | | - filename = time.time() |
23 | | - with open("./reports/%d.csv" % filename, 'w', encoding='UTF8', newline='') as f: |
24 | | - writer = csv.writer(f, delimiter=body.Seperator) |
25 | | - row = ["sep=" + body.Seperator] |
26 | | - |
27 | | - writer.writerow(row) |
28 | | - writer.writerow([body.Header]) |
29 | | - writer.writerow(body.ReadableColumns) |
30 | | - for data in body.Data: |
31 | | - row = [] |
32 | | - for column in body.Columns: |
33 | | - row.append(data[column]) |
34 | | - writer.writerow(row) |
35 | | - |
36 | | - return FileResponse("./reports/%d.csv" % filename) |
37 | | - |
38 | | -# Lists all templates |
39 | | -@app.get("/templates",summary="Lists all templates.", tags=["Template"]) |
40 | | -def ListTemplates(): |
41 | | - base_path = os.getcwd() + "/templates/" |
42 | | - result = [] |
43 | | - |
44 | | - for file in [f for f in os.listdir(base_path)]: |
45 | | - res = dict() |
46 | | - file_stats = os.stat(base_path+ file) |
47 | | - if os.path.splitext(file)[1] == ".docx": |
48 | | - res["name"] = file |
49 | | - res["size"] = file_stats.st_size |
50 | | - result.append(res) |
51 | | - |
52 | | - return result |
53 | | - |
54 | | -# Get single template |
55 | | -@app.get("/templates/{name}",summary="Get single template.", tags=["Template"]) |
56 | | -def ListTemplates(name: str): |
57 | | - base_path = os.getcwd() + "/templates/" + name |
58 | | - file_stats = os.stat(base_path) |
59 | | - res = dict() |
60 | | - res["name"] = name |
61 | | - res["size"] = file_stats.st_size |
62 | | - |
63 | | - return res |
64 | | - |
65 | | -# Get template's preview |
66 | | -@app.get("/templates/preview/{name}",summary="Get template's preview.", tags=["Template"]) |
67 | | -def TemplatePreview(name: str): |
68 | | - base_path = os.getcwd() + "/templates/" + name |
69 | | - pdf_path = Converter.docx2preview(base_path) |
70 | | - pdf_path = pdf_path.replace("docx", "pdf") |
71 | | - |
72 | | - pages = convert_from_path(pdf_path) |
73 | | - name = name.replace(".docx", ".png") |
74 | | - pages[0].save(name, 'png') |
75 | | - with open(name, "rb") as image_file: |
76 | | - encoded_string = base64.b64encode(image_file.read()) |
77 | | - response = dict() |
78 | | - response["encoded"] = encoded_string |
79 | | - return response |
80 | | - |
81 | | -# Delete template |
82 | | -@app.delete("/templates/{name}",summary="Deletes a template.", tags=["Template"]) |
83 | | -def ListTemplates(name: str): |
84 | | - os.remove(os.getcwd() + "/templates/" + name) |
85 | | - return "Item deleted successfully." |
86 | | - |
87 | | -# Upload new template |
88 | | -@app.post("/templates", summary="Uploads new template.", tags=["Template"]) |
89 | | -def SaveTemplate(file: UploadFile = File(...)): |
90 | | - try: |
91 | | - contents = file.file.read() |
92 | | - name = file.filename.replace(" ", "_") |
93 | | - name = file.filename.replace("-", "_") |
94 | | - |
95 | | - with open(name, 'wb') as f: |
96 | | - f.write(contents) |
97 | | - except Exception: |
98 | | - return {"message": "There was an error uploading the file"} |
99 | | - finally: |
100 | | - file.file.close() |
101 | | - os.rename(name, "./templates/" + name) |
102 | | - |
103 | | - return {"message": f"Successfully uploaded {name}"} |
| 5 | +app = FastAPI( |
| 6 | + title="Report Engine" |
| 7 | +) |
| 8 | +app.include_router(api.api_router) |
104 | 9 |
|
105 | 10 | def serve(): |
106 | 11 | """Serve the web application.""" |
|
0 commit comments