60 lines
2 KiB
Python
60 lines
2 KiB
Python
from django.http import HttpResponse
|
|
from drf_spectacular.types import OpenApiTypes
|
|
from drf_spectacular.utils import OpenApiResponse, extend_schema
|
|
from rest_framework import status
|
|
from rest_framework.views import APIView
|
|
|
|
from apps.reports.services.data_sources import ReportDataSources
|
|
from apps.reports.services.excel import build_report_workbook, build_report_zip
|
|
from apps.reports.services.report_builder import (
|
|
TICKETS_COLUMNS,
|
|
USERS_COLUMNS,
|
|
build_ticket_rows,
|
|
build_user_rows,
|
|
)
|
|
|
|
|
|
class GenerateReportView(APIView):
|
|
authentication_classes = []
|
|
permission_classes = []
|
|
|
|
@extend_schema(
|
|
tags=["Reports"],
|
|
summary="Generate CRM reports",
|
|
description=(
|
|
"Fetches application tickets and related user profiles, then returns a "
|
|
"ZIP archive containing `users-report.xlsx` and `tickets-report.xlsx`."
|
|
),
|
|
request=None,
|
|
responses={
|
|
(200, "application/zip"): OpenApiResponse(
|
|
response=OpenApiTypes.BINARY,
|
|
description="ZIP archive with users and tickets Excel reports.",
|
|
),
|
|
},
|
|
)
|
|
def post(self, request):
|
|
data_sources = ReportDataSources()
|
|
tickets = data_sources.fetch_application_tickets()
|
|
users = data_sources.fetch_users_for_tickets(tickets)
|
|
|
|
files = {
|
|
"users-report.xlsx": build_report_workbook(
|
|
title="Users Report",
|
|
columns=USERS_COLUMNS,
|
|
rows=build_user_rows(users),
|
|
),
|
|
"tickets-report.xlsx": build_report_workbook(
|
|
title="Tickets Report",
|
|
columns=TICKETS_COLUMNS,
|
|
rows=build_ticket_rows(tickets),
|
|
),
|
|
}
|
|
|
|
response = HttpResponse(
|
|
build_report_zip(files),
|
|
content_type="application/zip",
|
|
status=status.HTTP_200_OK,
|
|
)
|
|
response["Content-Disposition"] = 'attachment; filename="crm-reports.zip"'
|
|
return response
|