Skip to content

Commit 8209dff

Browse files
authored
Revert "Move to pandoc for rendering sponsorship contracts (#2343)" (#2374)
This reverts commit a4990b7.
1 parent af64f12 commit 8209dff

20 files changed

+483
-605
lines changed

.github/workflows/ci.yml

-12
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@ jobs:
1717
steps:
1818
- name: Check out repository
1919
uses: actions/checkout@v2
20-
- name: Install platform dependencies
21-
run: |
22-
sudo apt -y update
23-
sudo apt -y install --no-install-recommends \
24-
texlive-latex-base \
25-
texlive-latex-recommended \
26-
texlive-plain-generic \
27-
lmodern
28-
- name: Install pandoc
29-
run: |
30-
wget https://github.com/jgm/pandoc/releases/download/2.17.1.1/pandoc-2.17.1.1-1-amd64.deb
31-
sudo dpkg -i pandoc-2.17.1.1-1-amd64.deb
3220
- uses: actions/setup-python@v2
3321
with:
3422
python-version: 3.9.16

Aptfile

Whitespace-only changes.

Dockerfile

+2-40
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,9 @@
1-
FROM python:3.9-bookworm
1+
FROM python:3.9-bullseye
22
ENV PYTHONUNBUFFERED=1
33
ENV PYTHONDONTWRITEBYTECODE=1
4-
5-
# By default, Docker has special steps to avoid keeping APT caches in the layers, which
6-
# is good, but in our case, we're going to mount a special cache volume (kept between
7-
# builds), so we WANT the cache to persist.
8-
RUN set -eux; \
9-
rm -f /etc/apt/apt.conf.d/docker-clean; \
10-
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache;
11-
12-
# Install System level build requirements, this is done before
13-
# everything else because these are rarely ever going to change.
14-
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
15-
--mount=type=cache,target=/var/lib/apt,sharing=locked \
16-
set -x \
17-
&& apt-get update \
18-
&& apt-get install --no-install-recommends -y \
19-
pandoc \
20-
texlive-latex-base \
21-
texlive-latex-recommended \
22-
texlive-fonts-recommended \
23-
texlive-plain-generic \
24-
lmodern
25-
26-
RUN case $(uname -m) in \
27-
"x86_64") ARCH=amd64 ;; \
28-
"aarch64") ARCH=arm64 ;; \
29-
esac \
30-
&& wget --quiet https://github.com/jgm/pandoc/releases/download/2.17.1.1/pandoc-2.17.1.1-1-${ARCH}.deb \
31-
&& dpkg -i pandoc-2.17.1.1-1-${ARCH}.deb
32-
334
RUN mkdir /code
345
WORKDIR /code
35-
366
COPY dev-requirements.txt /code/
377
COPY base-requirements.txt /code/
38-
39-
RUN pip --no-cache-dir --disable-pip-version-check install --upgrade pip setuptools wheel
40-
41-
RUN --mount=type=cache,target=/root/.cache/pip \
42-
set -x \
43-
&& pip --disable-pip-version-check \
44-
install \
45-
-r dev-requirements.txt
46-
8+
RUN pip install -r dev-requirements.txt
479
COPY . /code/

base-requirements.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ django-filter==2.4.0
4444
django-ordered-model==3.4.3
4545
django-widget-tweaks==1.4.8
4646
django-countries==7.2.1
47+
xhtml2pdf==0.2.5
48+
django-easy-pdf3==0.1.2
4749
num2words==0.5.10
4850
django-polymorphic==3.0.0
4951
sorl-thumbnail==12.7.0
52+
docxtpl==0.12.0
53+
reportlab==3.6.6
5054
django-extensions==3.1.4
5155
django-import-export==2.7.1
52-
53-
pypandoc==1.12
54-
panflute==2.3.0

pydotorg/settings/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
'ordered_model',
174174
'widget_tweaks',
175175
'django_countries',
176+
'easy_pdf',
176177
'sorl.thumbnail',
177178

178179
'banners',

sponsors/contracts.py

-89
This file was deleted.

sponsors/pandoc_filters/__init__.py

Whitespace-only changes.

sponsors/pandoc_filters/pagebreak.py

-90
This file was deleted.

sponsors/pdf.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
This module is a wrapper around django-easy-pdf so we can reuse code
3+
"""
4+
import io
5+
import os
6+
from django.conf import settings
7+
from django.http import HttpResponse
8+
from django.utils.dateformat import format
9+
10+
from docxtpl import DocxTemplate
11+
from easy_pdf.rendering import render_to_pdf_response, render_to_pdf
12+
13+
from markupfield_helpers.helpers import render_md
14+
from django.utils.html import mark_safe
15+
16+
17+
def _clean_split(text, separator='\n'):
18+
return [
19+
t.replace('-', '').strip()
20+
for t in text.split('\n')
21+
if t.replace('-', '').strip()
22+
]
23+
24+
25+
def _contract_context(contract, **context):
26+
start_date = contract.sponsorship.start_date
27+
context.update({
28+
"contract": contract,
29+
"start_date": start_date,
30+
"start_day_english_suffix": format(start_date, "S"),
31+
"sponsor": contract.sponsorship.sponsor,
32+
"sponsorship": contract.sponsorship,
33+
"benefits": _clean_split(contract.benefits_list.raw),
34+
"legal_clauses": _clean_split(contract.legal_clauses.raw),
35+
"renewal": contract.sponsorship.renewal,
36+
})
37+
previous_effective = contract.sponsorship.previous_effective_date
38+
context["previous_effective"] = previous_effective if previous_effective else "UNKNOWN"
39+
context["previous_effective_english_suffix"] = format(previous_effective, "S") if previous_effective else None
40+
return context
41+
42+
43+
def render_contract_to_pdf_response(request, contract, **context):
44+
template = "sponsors/admin/preview-contract.html"
45+
context = _contract_context(contract, **context)
46+
return render_to_pdf_response(request, template, context)
47+
48+
49+
def render_contract_to_pdf_file(contract, **context):
50+
template = "sponsors/admin/preview-contract.html"
51+
context = _contract_context(contract, **context)
52+
return render_to_pdf(template, context)
53+
54+
55+
def _gen_docx_contract(output, contract, **context):
56+
context = _contract_context(contract, **context)
57+
renewal = context["renewal"]
58+
if renewal:
59+
template = os.path.join(settings.TEMPLATES_DIR, "sponsors", "admin", "renewal-contract-template.docx")
60+
else:
61+
template = os.path.join(settings.TEMPLATES_DIR, "sponsors", "admin", "contract-template.docx")
62+
doc = DocxTemplate(template)
63+
doc.render(context)
64+
doc.save(output)
65+
return output
66+
67+
68+
def render_contract_to_docx_response(request, contract, **context):
69+
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
70+
response['Content-Disposition'] = 'attachment; filename=contract.docx'
71+
return _gen_docx_contract(output=response, contract=contract, **context)
72+
73+
74+
def render_contract_to_docx_file(contract, **context):
75+
fp = io.BytesIO()
76+
fp = _gen_docx_contract(output=fp, contract=contract, **context)
77+
fp.seek(0)
78+
return fp.read()

sponsors/reference.docx

-12.3 KB
Binary file not shown.

sponsors/tests/test_contracts.py

-39
This file was deleted.

0 commit comments

Comments
 (0)