tenants2 icon indicating copy to clipboard operation
tenants2 copied to clipboard

Add a "get_norent_letter_pdfs" management command

Open toolness opened this issue 4 years ago • 0 comments

A user didn't receive a copy of their letter via email, so we had to manually email it to them. We don't actually have an easy way of getting the original PDF for a letter at the moment, so at the very least a management command to do this would be helpful.

The following is a quick command I wrote to do this; I didn't commit it to the repository because I'm not really sure if we'll need it again and I didn't want to spend time writing a test for it (and incurring the runtime of the test on every future CircleCI run).

from pathlib import Path
from django.core.management import BaseCommand

from users.models import JustfixUser
from norent.letter_sending import render_multilingual_letter


class Command(BaseCommand):
    help = "Output PDFs of NoRent letters sent by a user."

    def add_arguments(self, parser):
        parser.add_argument(
            'username',
            help='The username to retrieve NoRent PDFs for.'
        )

    def handle(self, *args, **options) -> None:
        user = JustfixUser.objects.get(username=options['username'])

        for letter in user.norent_letters.all():
            path = Path(
                f"norent-letter-{user.username}-"
                f"{letter.created_at.date().isoformat()}_"
                f"{letter.pk}.pdf"
            )
            print(f"Writing {path}.")
            pdf_bytes = render_multilingual_letter(letter)
            path.write_bytes(pdf_bytes)

toolness avatar Nov 05 '20 16:11 toolness