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

Add convert html to judgment #2048

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 21 additions & 1 deletion peachjam/models/judgment.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from tempfile import NamedTemporaryFile

from countries_plus.models import Country
from django.contrib.contenttypes.fields import GenericRelation
from django.core.files import File
from django.db import models
from django.db.models import Max
from django.template.defaultfilters import date as format_date
from django.urls import reverse
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from django.utils.translation import override as lang_override
from docpipe.soffice import soffice_convert

from peachjam.models import CoreDocument, Label, Locality
from peachjam.models import CoreDocument, Label, Locality, SourceFile


class Attorney(models.Model):
Expand Down Expand Up @@ -441,6 +445,22 @@ def pre_save(self):
self.assign_title()
super().pre_save()

def convert_html_to_pdf(self):
with NamedTemporaryFile(suffix=".html") as html_file:
html_file.write(self.content_html.encode("utf-8"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you do something like html_file.write(self.prepare_content_html_for_pdf()) so that subclasses can override the HTML that is used to generate the PDF, such as by adding a logo at the top.

Also, I think this can safely all be done on the CoreDocument object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done

html_file.flush()
html_file.seek(0)

pdf, _ = soffice_convert(html_file, "html", "pdf")
filename = slugify(self.case_name)
SourceFile.objects.update_or_create(
document=self,
defaults={
"file": File(pdf, name=f"{filename}.pdf"),
"mimetype": "application/pdf",
},
)


class CaseNumber(models.Model):
string_override = models.CharField(
Expand Down
13 changes: 13 additions & 0 deletions peachjam/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ def convert_source_file_to_pdf(source_file_id):
log.info("Conversion to PDF done")


@background(queue="peachjam", remove_existing_tasks=True)
def convert_html_to_pdf(judgment_id):
from peachjam.models import Judgment
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work for any file, not just judgment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


judgment = Judgment.objects.filter(id=judgment_id).first()
logger.info(f"Creating PDF from HTML for judgment {judgment_id}")
if not judgment:
logger.warning("Judgment not found")
return
judgment.convert_html_to_pdf()
logger.info("Done")


@background(queue="peachjam", remove_existing_tasks=True)
def rank_works():
from peachjam.graph.ranker import GraphRanker
Expand Down
Loading