Skip to content
Snippets Groups Projects
Verified Commit cf06c3e7 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Provide option to generate PDFFile object before PDF generation

(cherry picked from commit 663a9df2)
parent b0c58567
No related branches found
No related tags found
No related merge requests found
Pipeline #96818 canceled
......@@ -20,6 +20,8 @@ Changed
* OIDC scope "profile" now exposes the avatar instead of the official photo
* [Dev] Provide function to generate PDF files from fully-rendered templates.
* [Dev] Accept pre-created file object for PDF generation to define
the redirect URL in advance.
Fixed
~~~~~
......
# Generated by Django 3.2.16 on 2022-11-03 11:36
from django.db import migrations, models
import django.utils.timezone
import oauth2_provider.generators
import oauth2_provider.models
class Migration(migrations.Migration):
dependencies = [
('core', '0041_update_gender_choices'),
]
operations = [
migrations.AlterField(
model_name='pdffile',
name='html_file',
field=models.FileField(blank=True, null=True, upload_to='pdfs/', verbose_name='Generated HTML file'),
),
]
......@@ -1236,7 +1236,9 @@ class PDFFile(ExtensibleModel):
expires_at = models.DateTimeField(
verbose_name=_("File expires at"), default=_get_default_expiration
)
html_file = models.FileField(upload_to="pdfs/", verbose_name=_("Generated HTML file"))
html_file = models.FileField(
upload_to="pdfs/", verbose_name=_("Generated HTML file"), blank=True, null=True
)
file = models.FileField(
upload_to="pdfs/", blank=True, null=True, verbose_name=_("Generated PDF file")
)
......
......@@ -88,10 +88,16 @@ def process_context_for_pdf(context: Optional[dict] = None, request: Optional[Ht
def generate_pdf_from_html(
html: str, request: Optional[HttpRequest] = None
html: str, request: Optional[HttpRequest] = None, file_object: Optional[PDFFile] = None
) -> Tuple[PDFFile, AsyncResult]:
"""Start a PDF generation task and return the matching file object and Celery result."""
file_object = PDFFile.objects.create(html_file=ContentFile(html.encode(), name="source.html"))
html_file = ContentFile(html.encode(), name="source.html")
# In some cases, the file object is already created (to get a redirect URL for the PDF)
if not file_object:
file_object = PDFFile.objects.create()
file_object.html_file = html_file
file_object.save()
# As this method may be run in background and there is no request available,
# we have to use a predefined URL from settings then
......@@ -110,6 +116,7 @@ def generate_pdf_from_template(
context: Optional[dict] = None,
request: Optional[HttpRequest] = None,
render_method: Optional[Callable] = None,
file_object: Optional[PDFFile] = None,
) -> Tuple[PDFFile, AsyncResult]:
"""Start a PDF generation task and return the matching file object and Celery result."""
processed_context = process_context_for_pdf(context, request)
......@@ -119,7 +126,7 @@ def generate_pdf_from_template(
else:
html_template = render_to_string(template_name, processed_context, request)
return generate_pdf_from_html(html_template, request)
return generate_pdf_from_html(html_template, request, file_object=file_object)
def render_pdf(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment