Skip to content

Commit

Permalink
stable point
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-smith committed Dec 7, 2024
1 parent f7a8934 commit 941ce7f
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 33 deletions.
50 changes: 28 additions & 22 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import io
import base64

from docxtpl import DocxTemplate

from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie
Expand Down Expand Up @@ -1663,28 +1665,6 @@ def org_tree(self, request):

return Response(tree)

@action(detail=True, methods=["get"])
def exec_report(self, request, pk):
document = Document()
document.add_heading("Sales Performance Report", 0)
document.add_heading(f"ID: {pk}", 1)

document.add_paragraph(
"Report generated on: {}".format(now().strftime("%Y-%m-%d %H:%M:%S"))
)

buffer_doc = io.BytesIO()
document.save(buffer_doc)
buffer_doc.seek(0)

response = HttpResponse(
buffer_doc.getvalue(),
content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)
response["Content-Disposition"] = "attachment; filename=sales_report.docx"

return response

@action(detail=False, methods=["get"])
def ids(self, request):
my_map = dict()
Expand Down Expand Up @@ -2175,6 +2155,32 @@ def compliance_assessment_csv(self, request, pk):
{"error": "Permission denied"}, status=status.HTTP_403_FORBIDDEN
)

@action(detail=True, methods=["get"])
def word_report(self, request, pk):
document = Document()
document.add_heading("Sales Performance Report", 0)
document.add_heading(f"ID: {pk}", 1)

document.add_paragraph(
"Report generated on: {}".format(now().strftime("%Y-%m-%d %H:%M:%S"))
)

document.add_page_break()

document.add_paragraph("ok then.")

buffer_doc = io.BytesIO()
document.save(buffer_doc)
buffer_doc.seek(0)

response = HttpResponse(
buffer_doc.getvalue(),
content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)
response["Content-Disposition"] = "attachment; filename=sales_report.docx"

return response

@action(detail=True, name="Get action plan PDF")
def action_plan_pdf(self, request, pk):
(object_ids_view, _, _) = RoleAssignment.get_accessible_object_ids(
Expand Down
77 changes: 76 additions & 1 deletion backend/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 backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ humanize = "^4.11.0"
huey = "^2.5.2"
seaborn = "^0.13.2"
python-docx = "^1.1.2"
docxtpl = "^0.19.0"

[tool.poetry.group.dev.dependencies]
pytest-django = "4.8.0"
Expand Down
1 change: 1 addition & 0 deletions frontend/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
"plan": "Plan",
"asPDF": "as PDF",
"asCSV": "as CSV",
"asWord": "as Word",
"draft": "Draft",
"riskMatrixView": "Risk matrix view",
"currentInMatrixView": "Current",
Expand Down
1 change: 1 addition & 0 deletions frontend/messages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
"plan": "Plan",
"asPDF": "en PDF",
"asCSV": "en CSV",
"asWord": "en Word",
"draft": "Brouillon",
"riskMatrixView": "Vue matricielle",
"currentInMatrixView": "Courante",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,3 @@
>
</div>
{/if}
{#if data.model.name == 'folder'}
<div class="card w-full my-12 p-4 bg-white">
<div class="text-lg font-bold">Extra</div>
<a href={`${data.data.id}/exec-report`} class="text-lg">Generate exec report</a>
</div>
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { BASE_API_URL } from '$lib/utils/constants';
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const GET: RequestHandler = async ({ fetch, params }) => {
const URLModel = 'folders';
const endpoint = `${BASE_API_URL}/${URLModel}/${params.id}/exec_report/`;
const URLModel = 'compliance-assessments';
const endpoint = `${BASE_API_URL}/${URLModel}/${params.id}/word_report/`;

const res = await fetch(endpoint);
if (!res.ok) {
error(400, 'Error fetching the report file');
error(400, 'Error fetching the Word file');
}

const fileName = `report-${new Date().toISOString()}.docx`;
const fileName = `audit-${params.id}-${new Date().toISOString()}.docx`;

return new Response(await res.blob(), {
headers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@
href="/compliance-assessments/{data.compliance_assessment.id}/export/csv"
class="block px-4 py-2 text-sm text-gray-800 hover:bg-gray-200">... {m.asCSV()}</a
>
<a
href="/compliance-assessments/{data.compliance_assessment.id}/export/word"
class="block px-4 py-2 text-sm text-gray-800 hover:bg-gray-200">... {m.asWord()}</a
>
{/if}
<a
href="/compliance-assessments/{data.compliance_assessment.id}/export"
Expand Down

0 comments on commit 941ce7f

Please sign in to comment.