Skip to content

Commit

Permalink
feat: report-user documento route
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard0803 committed Dec 6, 2023
1 parent 3e0a060 commit e4f23fc
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 2 deletions.
3 changes: 3 additions & 0 deletions gestao/web/api/document/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from gestao.web.api.document.views import router

__all__ = ["router"]
92 changes: 92 additions & 0 deletions gestao/web/api/document/utils.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions gestao/web/api/document/views.py
Original file line number Diff line number Diff line change
@@ -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"
),
)
7 changes: 6 additions & 1 deletion gestao/web/api/router.py
Original file line number Diff line number Diff line change
@@ -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"],
)
29 changes: 28 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit e4f23fc

Please sign in to comment.