-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from fga-eps-mds/development
Development
- Loading branch information
Showing
12 changed files
with
398 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from io import BytesIO | ||
from urllib import parse | ||
|
||
import pytest | ||
from fastapi import FastAPI | ||
from httpx import AsyncClient | ||
from starlette import status | ||
|
||
from gestao.web.api.document.utils import generate_report_users_file | ||
|
||
|
||
def test_generate_report_users_file_correct() -> None: | ||
file_stream = generate_report_users_file([], []) | ||
assert isinstance(file_stream, BytesIO) | ||
|
||
|
||
def test_generate_report_users_file_incorrect() -> None: | ||
try: | ||
file_stream = generate_report_users_file(1, 2) | ||
except Exception as e: | ||
assert isinstance(e, TypeError) | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_get_report_users_NoFilter( | ||
client: AsyncClient, fastapi_app: FastAPI | ||
) -> None: | ||
url = fastapi_app.url_path_for("get_report_users") | ||
response = await client.get(url) | ||
assert response.status_code == 200 | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_get_report_users_Filter( | ||
client: AsyncClient, fastapi_app: FastAPI | ||
) -> None: | ||
url = fastapi_app.url_path_for("get_report_users") | ||
filter = ["fullName", "registration", "bloodType"] | ||
url += "?" + parse.urlencode({"filter_list": filter}) | ||
response = await client.get(url) | ||
assert response.status_code == 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from gestao.web.api.document.views import router | ||
|
||
__all__ = ["router"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import io | ||
from datetime import datetime | ||
|
||
from docx import Document | ||
from openpyxl import Workbook | ||
from python_docx_replace import docx_replace | ||
|
||
from gestao.db.models.user import User | ||
|
||
|
||
def generate_affiliation_file(user: User): | ||
input_path = "./files/affiliation_template.docx" | ||
now = datetime.now() | ||
month_switch = { | ||
1: "janeiro", | ||
2: "fevereiro", | ||
3: "março", | ||
4: "abril", | ||
5: "maio", | ||
6: "junho", | ||
7: "julho", | ||
8: "agosto", | ||
9: "setembro", | ||
10: "outubro", | ||
11: "novembro", | ||
12: "dezembro", | ||
} | ||
date_string = f"{now.day} de {month_switch.get(now.month)} de {now.year}" | ||
doc = Document(input_path) | ||
docx_replace( | ||
doc, | ||
**{ | ||
"cpf": user.cpf, | ||
"name": user.fullName, | ||
"date": date_string, | ||
}, | ||
) | ||
|
||
file_stream = io.BytesIO() | ||
doc.save(file_stream) | ||
file_stream.seek(0) | ||
|
||
return file_stream | ||
|
||
|
||
def generate_report_users_file(all_users_list: list, filter_list: list): | ||
template_data = { | ||
"index": [], | ||
"fullName": [], | ||
"warName": [], | ||
"registration": [], | ||
"rg": [], | ||
"cpf": [], | ||
"placeOfBirth": [], | ||
"ufNatural": [], | ||
"civilState": [], | ||
"cep": [], | ||
"address": [], | ||
"number": [], | ||
"neighborhood": [], | ||
"city": [], | ||
"complement": [], | ||
"uf": [], | ||
"email": [], | ||
"cellphone": [], | ||
"phone": [], | ||
"gender": [], | ||
"motherName": [], | ||
"fatherName": [], | ||
"scolarity": [], | ||
"religion": [], | ||
"bloodType": [], | ||
"actualWorkSituation": [], | ||
"admissionDate": [], | ||
"jobRole": [], | ||
"bodyOfLaw": [], | ||
"lotation": [], | ||
"workPost": [], | ||
} | ||
|
||
for user in all_users_list: | ||
template_data["index"].append(all_users_list.index(user) + 1) | ||
template_data["fullName"].append(user.fullName) | ||
template_data["warName"].append(user.warName) | ||
template_data["registration"].append(user.registration) | ||
template_data["rg"].append(user.rg) | ||
template_data["cpf"].append(user.cpf) | ||
template_data["placeOfBirth"].append(user.placeOfBirth) | ||
template_data["ufNatural"].append(user.ufNatural) | ||
template_data["civilState"].append(user.civilState) | ||
template_data["cep"].append(user.cep) | ||
template_data["address"].append(user.address) | ||
template_data["number"].append(user.number) | ||
template_data["neighborhood"].append(user.neighborhood) | ||
template_data["city"].append(user.city) | ||
template_data["complement"].append(user.complement) | ||
template_data["uf"].append(user.uf) | ||
template_data["email"].append(user.email) | ||
template_data["cellphone"].append(user.cellphone) | ||
template_data["phone"].append(user.phone) | ||
template_data["gender"].append(user.gender) | ||
template_data["motherName"].append(user.motherName) | ||
template_data["fatherName"].append(user.fatherName) | ||
template_data["scolarity"].append(user.scolarity) | ||
template_data["religion"].append(user.religion) | ||
template_data["bloodType"].append(user.bloodType) | ||
template_data["actualWorkSituation"].append(user.actualWorkSituation) | ||
template_data["admissionDate"].append(user.admissionDate) | ||
template_data["jobRole"].append(user.jobRole) | ||
template_data["bodyOfLaw"].append(user.bodyOfLaw) | ||
template_data["lotation"].append(user.lotation) | ||
template_data["workPost"].append(user.workPost) | ||
|
||
if len(filter_list) == 0: | ||
users_data = template_data | ||
else: | ||
users_data = { | ||
key: value for key, value in template_data.items() if key in filter_list | ||
} | ||
|
||
wb = Workbook() | ||
active_spreadsheet = wb.active | ||
|
||
columns = list(users_data.keys()) | ||
active_spreadsheet.append(columns) | ||
|
||
for line in zip(*users_data.values()): | ||
active_spreadsheet.append(line) | ||
|
||
file_stream = io.BytesIO() | ||
wb.save(file_stream) | ||
file_stream.seek(0) | ||
|
||
return file_stream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import logging | ||
from typing import List | ||
|
||
from fastapi import APIRouter, HTTPException | ||
from fastapi.responses import StreamingResponse | ||
|
||
from gestao.db.models.user import User | ||
from gestao.web.api.document.utils import ( | ||
generate_affiliation_file, | ||
generate_report_users_file, | ||
) | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/affiliation/{user_id}") | ||
async def get_user_affiliation(user_id: str) -> None: | ||
try: | ||
user = await User.objects.get(id=user_id) | ||
file_stream = generate_affiliation_file(user) | ||
file_name = "affiliation-doc.docx" | ||
return StreamingResponse( | ||
file_stream, | ||
media_type=( | ||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" | ||
), | ||
headers={ | ||
"Content-Disposition": f"attachment; filename={file_name}", | ||
}, | ||
) | ||
except Exception: | ||
logging.error("Error occurred while get user", exc_info=True) | ||
raise HTTPException(status_code=404, detail="User not found") | ||
|
||
|
||
@router.get("/report-users/") | ||
async def get_report_users(filter_list: List[str] = []): | ||
users_list = await User.objects.all() | ||
file_stream = generate_report_users_file(list(users_list), filter_list) | ||
file_name = "report-users.xlsx" | ||
return StreamingResponse( | ||
file_stream, | ||
media_type=( | ||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | ||
), | ||
headers={ | ||
"Content-Disposition": f"attachment; filename={file_name}", | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
from fastapi.routing import APIRouter | ||
|
||
from gestao.web.api import echo, login, monitoring, user | ||
from gestao.web.api import document, echo, login, monitoring, user | ||
|
||
api_router = APIRouter() | ||
api_router.include_router(monitoring.router) | ||
api_router.include_router(echo.router, prefix="/echo", tags=["echo"]) | ||
api_router.include_router(user.router, prefix="/users", tags=["users"]) | ||
api_router.include_router(login.router, prefix="/login", tags=["login"]) | ||
api_router.include_router(document.router, prefix="/documents", tags=["documents"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.