Skip to content

Commit b6cf5d3

Browse files
committed
added test&postman requests
1 parent 445c124 commit b6cf5d3

File tree

8 files changed

+150
-133
lines changed

8 files changed

+150
-133
lines changed

app/api/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
from core.startup import on_startup, on_shutdown
44
from core.users import current_active_user
55
from models.user import User
6-
from app.services.convertor_router import file_router
6+
from services.convert_router import convert_router
77

88
from .user_router import api_router
99

1010

1111
app = FastAPI()
1212
app.include_router(api_router)
13-
app.include_router(file_router, prefix="/files", tags=["files"])
13+
app.include_router(convert_router, prefix="/files", tags=["files"])
1414

1515

1616
@app.get("/authenticated-route")

app/services/convert_router.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import os
2+
import shutil
3+
4+
from fastapi import APIRouter, Depends, Form, File, UploadFile, HTTPException
5+
from fastapi.responses import FileResponse
6+
7+
from core.config import file_storage_settings as settings
8+
from core.users import current_active_user
9+
from models.user import User
10+
from schemas.schemas import ImageProcessingOptions
11+
12+
from .csv_to_xlsx_convertor import convert_csv_xlsx
13+
from .docx_to_pdf_convertor import write_docx, write_pdf
14+
from .image_convertor import process_image
15+
16+
17+
convert_router = APIRouter()
18+
UPLOAD_DIR = settings.MEDIA_DIR
19+
os.makedirs(UPLOAD_DIR, exist_ok=True)
20+
21+
22+
@convert_router.post("/convert/image")
23+
async def convert_image(
24+
file: UploadFile = File(...),
25+
resize: str = Form(None),
26+
convert_to: str = Form(None),
27+
grayscale: bool = Form(None),
28+
flip: str = Form(None),
29+
user: User = Depends(current_active_user)
30+
):
31+
options = ImageProcessingOptions(
32+
resize=resize,
33+
convert_to=convert_to,
34+
grayscale=grayscale,
35+
flip=flip
36+
)
37+
38+
user_folder = f"{UPLOAD_DIR}/{user.id}"
39+
os.makedirs(user_folder, exist_ok=True)
40+
41+
file_location = os.path.join(user_folder, file.filename)
42+
43+
with open(file_location, "wb") as buffers:
44+
shutil.copyfileobj(file.file, buffers)
45+
46+
processed_file_location = process_image(file_location, options)
47+
return {
48+
"filename": os.path.basename(processed_file_location),
49+
"location": processed_file_location
50+
}
51+
52+
53+
@convert_router.post("/convert/csv-xlsx")
54+
async def convert_csv_xlsx_file(
55+
file: UploadFile = File(...),
56+
user: User = Depends(current_active_user)
57+
):
58+
user_folder = os.path.join(str(UPLOAD_DIR), str(user.id))
59+
os.makedirs(user_folder, exist_ok=True)
60+
61+
file_location = os.path.join(user_folder, file.filename)
62+
63+
64+
with open(file_location, "wb") as buffers:
65+
shutil.copyfileobj(file.file, buffers)
66+
67+
filename = convert_csv_xlsx(file_location)
68+
69+
return {"filename": filename}
70+
71+
72+
@convert_router.post("/convert/pdf-docs")
73+
async def convert_pdf_word_file(
74+
file: UploadFile = File(...),
75+
user: User = Depends(current_active_user)
76+
):
77+
user_folder = f"{UPLOAD_DIR}/{user.id}"
78+
os.makedirs(user_folder, exist_ok=True)
79+
80+
file_location = os.path.join(user_folder, file.filename)
81+
file_docs = file_location.split(".")[0] + ".docs"
82+
processed_file_location = write_docx(
83+
file_word=file_docs, file_pdf=file_location
84+
)
85+
return {
86+
"filename": os.path.basename(processed_file_location),
87+
"location": processed_file_location
88+
}
89+
90+
91+
@convert_router.post("/convert/docs-pdf")
92+
async def convert_pdf_word_file(
93+
file: UploadFile = File(...),
94+
user: User = Depends(current_active_user)
95+
):
96+
user_folder = f"{UPLOAD_DIR}/{user.id}"
97+
os.makedirs(user_folder, exist_ok=True)
98+
99+
file_location = os.path.join(user_folder, file.filename)
100+
file_pdf = file_location.split(".")[0] + ".pdf"
101+
processed_file_location = write_pdf(
102+
file_pdf=file_pdf, file_word=file_location
103+
)
104+
return {
105+
"filename": os.path.basename(processed_file_location),
106+
"location": processed_file_location
107+
}
108+
109+
110+
@convert_router.get("/download/{filename}", response_class=FileResponse)
111+
async def download_file(filename: str, user: User = Depends(current_active_user)):
112+
user_folder = os.path.join(str(UPLOAD_DIR), str(user.id))
113+
file_location = os.path.join(user_folder, filename)
114+
if not os.path.exists(file_location):
115+
raise HTTPException(status_code=404, detail="File not found")
116+
return {
117+
"message": [
118+
user_folder,
119+
file_location
120+
]
121+
}
122+
return FileResponse(file_location)

app/services/convertor_router.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

app/services/csv_to_xlsx_convertor.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@ def convert_csv_xlsx(filename: FilePath) -> None:
2121
if re.findall(_csv_filetype, filename):
2222
file_xlsx = re.sub(_csv_filetype, ".xlsx", filename)
2323
pd.read_csv(filename).to_excel(file_xlsx, index=False)
24+
return file_xlsx
2425

2526
elif re.findall(_xlsx_filetype, filename):
2627
file_csv = re.sub(_xlsx_filetype, ".csv", filename)
2728
pd.read_excel(filename).to_csv(file_csv, index=False)
29+
return file_csv
2830

2931
else:
3032
raise ValueError("This file type is not supported.\n")
31-
32-
33-
if __name__ == "__main__":
34-
file_csv = ".\\data\\2024-04.csv"
35-
convert_csv_xlsx(file_csv)
36-
37-
file_xlsx = ".\\data\\2024-04_1.xlsx"
38-
convert_csv_xlsx(file_xlsx)
39-
40-
print("Convert is successful.")

app/services/docx_to_pdf_convertor.py

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
"""Домашнє завдання 11
2-
3-
11.2 Зробити конвертор для docx або doc у pdf та навпаки
4-
"""
5-
61
from typing import Union
72

83
from docx import Document
94
from fpdf import FPDF
105
from PyPDF2 import PdfReader
116

127

13-
def _read_docx(filename: Union[str, bytes]) -> str:
8+
def _read_docx(file_docs: Union[str, bytes]) -> str:
149
"""Reads DOCX file and return only text content.
1510
1611
Args:
@@ -19,24 +14,28 @@ def _read_docx(filename: Union[str, bytes]) -> str:
1914
Returns:
2015
str: file content
2116
"""
22-
document = Document(filename)
17+
document = Document(file_docs)
2318
return "\n".join([line.text for line in document.paragraphs])
2419

2520

26-
def write_docx(filename: Union[str, bytes], content: str) -> None:
21+
def write_docx(
22+
file_docs: Union[str, bytes],
23+
file_pdf: Union[str, bytes]
24+
) -> None:
2725
"""Writes textual content to a DOCX file.
2826
2927
Args:
30-
filename (Union[str, bytes]): filename or file path
31-
content (str): textual content to write to a DOCX file
28+
file_docs (Union[str, bytes]): filename or file path
29+
file_pdf (Union[str, bytes]): filename or file path
3230
"""
31+
content=_read_pdf(file_pdf)
3332
document = Document()
3433
for line in content.split("\n"):
3534
document.add_paragraph(line)
36-
document.save(filename)
35+
document.save(file_docs)
3736

3837

39-
def _read_pdf(filename: Union[str, bytes]) -> str:
38+
def _read_pdf(file_pdf: Union[str, bytes]) -> str:
4039
"""Reads DOCX file and return only text content.
4140
4241
Args:
@@ -45,36 +44,25 @@ def _read_pdf(filename: Union[str, bytes]) -> str:
4544
Returns:
4645
str: file content
4746
"""
48-
reader = PdfReader(stream=filename)
47+
reader = PdfReader(stream=file_pdf)
4948
return "\n".join(
5049
[reader.pages[i].extract_text() for i in range(0, len(reader.pages))]
5150
)
5251

5352

54-
def write_pdf(filename: Union[str, bytes], content: str) -> None:
53+
def write_pdf(
54+
file_pdf: Union[str, bytes],
55+
file_word: Union[str, bytes]
56+
) -> None:
5557
"""Writes textual content to a PDF file.
5658
5759
Args:
58-
filename (Union[str, bytes]): filename or file path
59-
content (str): textual content to write to a PDF file
60+
file_pdf (Union[str, bytes]): filename or file path
61+
file_word (Union[str, bytes]): filename or file path
6062
"""
63+
content = _read_docx(file_word)
6164
pdf = FPDF()
6265
pdf.set_font("Arial", size=12)
6366
pdf.add_page()
6467
pdf.write(5, content)
65-
pdf.output(filename)
66-
67-
68-
if __name__ == "__main__":
69-
# Let's set the path to the files for the presentation
70-
file_docs = ".\\data\\data.docx"
71-
file_pdf = ".\\data\\data.pdf"
72-
_file_to_docs = ".\\data\\to_docx.docx"
73-
74-
# Convert DOCX to PDF
75-
write_pdf(filename=file_pdf, content=_read_docx(file_docs))
76-
print("Convert DOCX to PDF is successfully!")
77-
78-
# Convert PDF to DOCX
79-
write_docx(filename=_file_to_docs, content=_read_pdf(file_pdf))
80-
print("Convert PDF to DOCX is successfully!")
68+
pdf.output(file_pdf)

tests/media/hello_world.xlsx

8.24 KB
Binary file not shown.

tests/test_app.py renamed to tests/test_auth_user.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,6 @@ async def test_delete_another_user_data(
165165
assert not user, "User was not deleted from the database"
166166

167167

168-
async def test_upload_file(async_client, get_token):
169-
with open("tests/media/python.png", "rb") as f:
170-
files = {"file": ("python.png", f, "image/png")}
171-
data = {"resize": "500x500"}
172-
173-
headers = {"Authorization": f"Bearer {get_token}"}
174-
image_upload_response = await async_client.post(
175-
"/files/upload/", files=files, data=data, headers=headers
176-
)
177-
178-
assert image_upload_response.status_code == status.HTTP_200_OK, (
179-
"Upload file failed: " + image_upload_response.text
180-
)
181-
182-
183168
async def test_logout(async_client, get_token):
184169
headers = {
185170
"Authorization": f"Bearer {get_token}",

tests/test_converts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from fastapi import status
2+
from sqlalchemy import select
3+
4+
from tests.conftest import async_session_maker

0 commit comments

Comments
 (0)