From e4f23fc6d35eb5178250405a6c43627abbdc65c8 Mon Sep 17 00:00:00 2001 From: Eduard0803 Date: Wed, 6 Dec 2023 14:40:43 -0300 Subject: [PATCH] feat: report-user documento route --- gestao/web/api/document/__init__.py | 3 + gestao/web/api/document/utils.py | 92 +++++++++++++++++++++++++++++ gestao/web/api/document/views.py | 21 +++++++ gestao/web/api/router.py | 7 ++- poetry.lock | 29 ++++++++- pyproject.toml | 1 + 6 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 gestao/web/api/document/__init__.py create mode 100644 gestao/web/api/document/utils.py create mode 100644 gestao/web/api/document/views.py diff --git a/gestao/web/api/document/__init__.py b/gestao/web/api/document/__init__.py new file mode 100644 index 0000000..fc07eea --- /dev/null +++ b/gestao/web/api/document/__init__.py @@ -0,0 +1,3 @@ +from gestao.web.api.document.views import router + +__all__ = ["router"] diff --git a/gestao/web/api/document/utils.py b/gestao/web/api/document/utils.py new file mode 100644 index 0000000..0930c15 --- /dev/null +++ b/gestao/web/api/document/utils.py @@ -0,0 +1,92 @@ +import io + +from openpyxl import Workbook + + +def generate_report_users_file(all_users_list: list, filter_list: list): + template_data = { + "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["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 diff --git a/gestao/web/api/document/views.py b/gestao/web/api/document/views.py new file mode 100644 index 0000000..21834d0 --- /dev/null +++ b/gestao/web/api/document/views.py @@ -0,0 +1,21 @@ +from typing import List + +from fastapi import APIRouter +from fastapi.responses import StreamingResponse + +from gestao.db.models.user import User +from gestao.web.api.document.utils import generate_report_users_file + +router = APIRouter() + + +@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) + return StreamingResponse( + file_stream, + media_type=( + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ), + ) diff --git a/gestao/web/api/router.py b/gestao/web/api/router.py index c337209..cc49232 100644 --- a/gestao/web/api/router.py +++ b/gestao/web/api/router.py @@ -1,9 +1,14 @@ 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"], +) diff --git a/poetry.lock b/poetry.lock index 9a9a033..b1a496e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -395,6 +395,18 @@ files = [ {file = "eradicate-2.3.0.tar.gz", hash = "sha256:06df115be3b87d0fc1c483db22a2ebb12bcf40585722810d809cc770f5031c37"}, ] +[[package]] +name = "et-xmlfile" +version = "1.1.0" +description = "An implementation of lxml.xmlfile for the standard library" +category = "main" +optional = false +python-versions = ">=3.6" +files = [ + {file = "et_xmlfile-1.1.0-py3-none-any.whl", hash = "sha256:a2ba85d1d6a74ef63837eed693bcb89c3f752169b0e3e7ae5b16ca5e1b3deada"}, + {file = "et_xmlfile-1.1.0.tar.gz", hash = "sha256:8eb9e2bc2f8c97e37a2dc85a09ecdcdec9d8a396530a6d5a33b30b9a92da0c5c"}, +] + [[package]] name = "exceptiongroup" version = "1.1.3" @@ -1281,6 +1293,21 @@ files = [ [package.dependencies] setuptools = "*" +[[package]] +name = "openpyxl" +version = "3.1.2" +description = "A Python library to read/write Excel 2010 xlsx/xlsm files" +category = "main" +optional = false +python-versions = ">=3.6" +files = [ + {file = "openpyxl-3.1.2-py2.py3-none-any.whl", hash = "sha256:f91456ead12ab3c6c2e9491cf33ba6d08357d802192379bb482f1033ade496f5"}, + {file = "openpyxl-3.1.2.tar.gz", hash = "sha256:a6f5977418eff3b2d5500d54d9db50c8277a368436f4e4f8ddb1be3422870184"}, +] + +[package.dependencies] +et-xmlfile = "*" + [[package]] name = "ormar" version = "0.12.2" @@ -2462,4 +2489,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "26177479c37809d8aa3b48777f92dfc45e226edc832fcfcff41de7a38cddf066" +content-hash = "32bf1930017602d0f5a553a0bd5b2e8b7405248eccca95bd5b1c44bb2304640e" diff --git a/pyproject.toml b/pyproject.toml index b8b29ec..5c27983 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ psycopg2-binary = "^2.9.6" httptools = "^0.6.0" pytest = "^7.4.3" faker = "^20.1.0" +openpyxl = "^3.1.2" [tool.poetry.dev-dependencies]