diff --git a/sponsors/pdf.py b/sponsors/pdf.py deleted file mode 100644 index 9855beee3..000000000 --- a/sponsors/pdf.py +++ /dev/null @@ -1,78 +0,0 @@ -""" -This module is a wrapper around django-easy-pdf so we can reuse code -""" -import io -import os -from django.conf import settings -from django.http import HttpResponse -from django.utils.dateformat import format - -from docxtpl import DocxTemplate -from easy_pdf.rendering import render_to_pdf_response, render_to_pdf - -from markupfield_helpers.helpers import render_md -from django.utils.html import mark_safe - - -def _clean_split(text, separator='\n'): - return [ - t.replace('-', '').strip() - for t in text.split('\n') - if t.replace('-', '').strip() - ] - - -def _contract_context(contract, **context): - start_date = contract.sponsorship.start_date - context.update({ - "contract": contract, - "start_date": start_date, - "start_day_english_suffix": format(start_date, "S"), - "sponsor": contract.sponsorship.sponsor, - "sponsorship": contract.sponsorship, - "benefits": _clean_split(contract.benefits_list.raw), - "legal_clauses": _clean_split(contract.legal_clauses.raw), - "renewal": contract.sponsorship.renewal, - }) - previous_effective = contract.sponsorship.previous_effective_date - context["previous_effective"] = previous_effective if previous_effective else "UNKNOWN" - context["previous_effective_english_suffix"] = format(previous_effective, "S") if previous_effective else None - return context - - -def render_contract_to_pdf_response(request, contract, **context): - template = "sponsors/admin/preview-contract.html" - context = _contract_context(contract, **context) - return render_to_pdf_response(request, template, context) - - -def render_contract_to_pdf_file(contract, **context): - template = "sponsors/admin/preview-contract.html" - context = _contract_context(contract, **context) - return render_to_pdf(template, context) - - -def _gen_docx_contract(output, contract, **context): - context = _contract_context(contract, **context) - renewal = context["renewal"] - if renewal: - template = os.path.join(settings.TEMPLATES_DIR, "sponsors", "admin", "renewal-contract-template.docx") - else: - template = os.path.join(settings.TEMPLATES_DIR, "sponsors", "admin", "contract-template.docx") - doc = DocxTemplate(template) - doc.render(context) - doc.save(output) - return output - - -def render_contract_to_docx_response(request, contract, **context): - response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document') - response['Content-Disposition'] = 'attachment; filename=contract.docx' - return _gen_docx_contract(output=response, contract=contract, **context) - - -def render_contract_to_docx_file(contract, **context): - fp = io.BytesIO() - fp = _gen_docx_contract(output=fp, contract=contract, **context) - fp.seek(0) - return fp.read() diff --git a/sponsors/tests/test_pdf.py b/sponsors/tests/test_pdf.py deleted file mode 100644 index e4d140cf2..000000000 --- a/sponsors/tests/test_pdf.py +++ /dev/null @@ -1,113 +0,0 @@ -from datetime import date -from docxtpl import DocxTemplate -from markupfield_helpers.helpers import render_md -from model_bakery import baker -from pathlib import Path -from unittest.mock import patch, Mock - -from django.conf import settings -from django.http import HttpResponse, HttpRequest -from django.template.loader import render_to_string -from django.test import TestCase -from django.utils.html import mark_safe -from django.utils.dateformat import format - -from sponsors.pdf import render_contract_to_pdf_file, render_contract_to_pdf_response, render_contract_to_docx_response - - -class TestRenderContract(TestCase): - def setUp(self): - self.contract = baker.make_recipe("sponsors.tests.empty_contract", sponsorship__start_date=date.today()) - text = f"{self.contract.benefits_list.raw}\n\n**Legal Clauses**\n{self.contract.legal_clauses.raw}" - html = render_md(text) - self.context = { - "contract": self.contract, - "start_date": self.contract.sponsorship.start_date, - "start_day_english_suffix": format(self.contract.sponsorship.start_date, "S"), - "sponsor": self.contract.sponsorship.sponsor, - "sponsorship": self.contract.sponsorship, - "benefits": [], - "legal_clauses": [], - "renewal": None, - "previous_effective": "UNKNOWN", - "previous_effective_english_suffix": None, - } - self.template = "sponsors/admin/preview-contract.html" - - # PDF unit tests - @patch("sponsors.pdf.render_to_pdf") - def test_render_pdf_using_django_easy_pdf(self, mock_render): - mock_render.return_value = "pdf content" - - content = render_contract_to_pdf_file(self.contract) - - self.assertEqual(content, "pdf content") - mock_render.assert_called_once_with(self.template, self.context) - - @patch("sponsors.pdf.render_to_pdf_response") - def test_render_response_using_django_easy_pdf(self, mock_render): - response = Mock(HttpResponse) - mock_render.return_value = response - - request = Mock(HttpRequest) - content = render_contract_to_pdf_response(request, self.contract) - - self.assertEqual(content, response) - mock_render.assert_called_once_with(request, self.template, self.context) - - # DOCX unit test - @patch("sponsors.pdf.DocxTemplate") - def test_render_response_with_docx_attachment(self, MockDocxTemplate): - template = Path(settings.TEMPLATES_DIR) / "sponsors" / "admin" / "contract-template.docx" - self.assertTrue(template.exists()) - mocked_doc = Mock(DocxTemplate) - MockDocxTemplate.return_value = mocked_doc - - request = Mock(HttpRequest) - response = render_contract_to_docx_response(request, self.contract) - - MockDocxTemplate.assert_called_once_with(str(template.resolve())) - mocked_doc.render.assert_called_once_with(self.context) - mocked_doc.save.assert_called_once_with(response) - self.assertEqual(response.get("Content-Disposition"), "attachment; filename=contract.docx") - self.assertEqual( - response.get("Content-Type"), - "application/vnd.openxmlformats-officedocument.wordprocessingml.document" - ) - - @patch("sponsors.pdf.DocxTemplate") - def test_render_response_with_docx_attachment__renewal(self, MockDocxTemplate): - renewal_contract = baker.make_recipe("sponsors.tests.empty_contract", sponsorship__start_date=date.today(), - sponsorship__renewal=True) - text = f"{renewal_contract.benefits_list.raw}\n\n**Legal Clauses**\n{renewal_contract.legal_clauses.raw}" - html = render_md(text) - renewal_context = { - "contract": renewal_contract, - "start_date": renewal_contract.sponsorship.start_date, - "start_day_english_suffix": format(self.contract.sponsorship.start_date, "S"), - "sponsor": renewal_contract.sponsorship.sponsor, - "sponsorship": renewal_contract.sponsorship, - "benefits": [], - "legal_clauses": [], - "renewal": True, - "previous_effective": "UNKNOWN", - "previous_effective_english_suffix": None, - } - renewal_template = "sponsors/admin/preview-contract.html" - - template = Path(settings.TEMPLATES_DIR) / "sponsors" / "admin" / "renewal-contract-template.docx" - self.assertTrue(template.exists()) - mocked_doc = Mock(DocxTemplate) - MockDocxTemplate.return_value = mocked_doc - - request = Mock(HttpRequest) - response = render_contract_to_docx_response(request, renewal_contract) - - MockDocxTemplate.assert_called_once_with(str(template.resolve())) - mocked_doc.render.assert_called_once_with(renewal_context) - mocked_doc.save.assert_called_once_with(response) - self.assertEqual(response.get("Content-Disposition"), "attachment; filename=contract.docx") - self.assertEqual( - response.get("Content-Type"), - "application/vnd.openxmlformats-officedocument.wordprocessingml.document" - )