Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Move to pandoc for rendering sponsorship contracts (#2343)" #2374

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ jobs:
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Install platform dependencies
run: |
sudo apt -y update
sudo apt -y install --no-install-recommends \
texlive-latex-base \
texlive-latex-recommended \
texlive-plain-generic \
lmodern
- name: Install pandoc
run: |
wget https://github.com/jgm/pandoc/releases/download/2.17.1.1/pandoc-2.17.1.1-1-amd64.deb
sudo dpkg -i pandoc-2.17.1.1-1-amd64.deb
- uses: actions/setup-python@v2
with:
python-version: 3.9.16
Expand Down
Empty file removed Aptfile
Empty file.
42 changes: 2 additions & 40 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,47 +1,9 @@
FROM python:3.9-bookworm
FROM python:3.9-bullseye
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1

# By default, Docker has special steps to avoid keeping APT caches in the layers, which
# is good, but in our case, we're going to mount a special cache volume (kept between
# builds), so we WANT the cache to persist.
RUN set -eux; \
rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache;

# Install System level build requirements, this is done before
# everything else because these are rarely ever going to change.
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
set -x \
&& apt-get update \
&& apt-get install --no-install-recommends -y \
pandoc \
texlive-latex-base \
texlive-latex-recommended \
texlive-fonts-recommended \
texlive-plain-generic \
lmodern

RUN case $(uname -m) in \
"x86_64") ARCH=amd64 ;; \
"aarch64") ARCH=arm64 ;; \
esac \
&& wget --quiet https://github.com/jgm/pandoc/releases/download/2.17.1.1/pandoc-2.17.1.1-1-${ARCH}.deb \
&& dpkg -i pandoc-2.17.1.1-1-${ARCH}.deb

RUN mkdir /code
WORKDIR /code

COPY dev-requirements.txt /code/
COPY base-requirements.txt /code/

RUN pip --no-cache-dir --disable-pip-version-check install --upgrade pip setuptools wheel

RUN --mount=type=cache,target=/root/.cache/pip \
set -x \
&& pip --disable-pip-version-check \
install \
-r dev-requirements.txt

RUN pip install -r dev-requirements.txt
COPY . /code/
7 changes: 4 additions & 3 deletions base-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ django-filter==2.4.0
django-ordered-model==3.4.3
django-widget-tweaks==1.4.8
django-countries==7.2.1
xhtml2pdf==0.2.5
django-easy-pdf3==0.1.2
num2words==0.5.10
django-polymorphic==3.0.0
sorl-thumbnail==12.7.0
docxtpl==0.12.0
reportlab==3.6.6
django-extensions==3.1.4
django-import-export==2.7.1

pypandoc==1.12
panflute==2.3.0
1 change: 1 addition & 0 deletions pydotorg/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
'ordered_model',
'widget_tweaks',
'django_countries',
'easy_pdf',
'sorl.thumbnail',

'banners',
Expand Down
89 changes: 0 additions & 89 deletions sponsors/contracts.py

This file was deleted.

Empty file.
90 changes: 0 additions & 90 deletions sponsors/pandoc_filters/pagebreak.py

This file was deleted.

78 changes: 78 additions & 0 deletions sponsors/pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
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()
Binary file removed sponsors/reference.docx
Binary file not shown.
39 changes: 0 additions & 39 deletions sponsors/tests/test_contracts.py

This file was deleted.

Loading
Loading