Пример #1
0
 def test_snail_mail_prepare(self):
     """Generate a SnailMailPDF"""
     snail = SnailMailTaskFactory()
     pdf = SnailMailPDF(
         snail.communication, snail.category, snail.switch, snail.amount
     )
     prepared_pdf, page_count, files, mail = pdf.prepare()
     ok_(prepared_pdf)
     eq_(page_count, 1)
     eq_(files, [])
     ok_(isinstance(mail, MailCommunication))
Пример #2
0
def snail_mail_bulk_pdf_task(pdf_name, get, **kwargs):
    """Save a PDF file for all open snail mail tasks"""
    # pylint: disable=too-many-locals
    # pylint: disable=unused-argument
    # pylint: disable=too-many-statements
    cover_info = []
    bulk_merger = PdfFileMerger(strict=False)

    snails = SnailMailTaskFilterSet(
        get,
        queryset=SnailMailTask.objects.filter(resolved=False)
        .order_by("-amount", "communication__foia__agency")
        .preload_pdf(),
    ).qs[:100]

    blank_pdf = FPDF()
    blank_pdf.add_page()
    blank = BytesIO(blank_pdf.output(dest="S").encode("latin-1"))
    for snail in snails.iterator():
        # generate the pdf and merge all pdf attachments
        pdf = SnailMailPDF(
            snail.communication, snail.category, snail.switch, snail.amount
        )
        prepared_pdf, page_count, files, _mail = pdf.prepare()
        cover_info.append((snail, page_count, files))

        if prepared_pdf is not None:
            # append to the bulk pdf
            bulk_merger.append(prepared_pdf)
            # ensure we align for double sided printing
            if PdfFileReader(prepared_pdf).getNumPages() % 2 == 1:
                blank.seek(0)
                bulk_merger.append(blank)

    # preprend the cover sheet
    cover_pdf = CoverPDF(cover_info)
    cover_pdf.generate()
    if cover_pdf.page % 2 == 1:
        cover_pdf.add_page()
    bulk_merger.merge(0, BytesIO(cover_pdf.output(dest="S").encode("latin-1")))

    bulk_pdf = BytesIO()
    bulk_merger.write(bulk_pdf)
    bulk_pdf.seek(0)

    s3 = boto3.client("s3")
    s3.upload_fileobj(
        bulk_pdf,
        settings.AWS_MEDIA_BUCKET_NAME,
        pdf_name,
        ExtraArgs={"ACL": settings.AWS_DEFAULT_ACL},
    )
Пример #3
0
def snail_mail_bulk_pdf_task(pdf_name, get, **kwargs):
    """Save a PDF file for all open snail mail tasks"""
    # pylint: disable=too-many-locals
    # pylint: disable=unused-argument
    # pylint: disable=too-many-statements
    cover_info = []
    bulk_merger = PdfFileMerger(strict=False)

    snails = SnailMailTaskFilterSet(
        get,
        queryset=SnailMailTask.objects.filter(resolved=False).order_by(
            '-amount',
            'communication__foia__agency',
        ).preload_pdf(),
    ).qs[:100]

    blank_pdf = FPDF()
    blank_pdf.add_page()
    blank = StringIO(blank_pdf.output(dest='S'))
    for snail in snails.iterator():
        # generate the pdf and merge all pdf attachments
        pdf = SnailMailPDF(snail.communication, snail.category, snail.switch,
                           snail.amount)
        prepared_pdf, page_count, files, _mail = pdf.prepare()
        cover_info.append((snail, page_count, files))

        if prepared_pdf is not None:
            # append to the bulk pdf
            bulk_merger.append(prepared_pdf)
            # ensure we align for double sided printing
            if PdfFileReader(prepared_pdf).getNumPages() % 2 == 1:
                blank.seek(0)
                bulk_merger.append(blank)

    # preprend the cover sheet
    cover_pdf = CoverPDF(cover_info)
    cover_pdf.generate()
    if cover_pdf.page % 2 == 1:
        cover_pdf.add_page()
    bulk_merger.merge(0, StringIO(cover_pdf.output(dest='S')))

    bulk_pdf = StringIO()
    bulk_merger.write(bulk_pdf)
    bulk_pdf.seek(0)

    conn = S3Connection(settings.AWS_ACCESS_KEY_ID,
                        settings.AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME)
    key = Key(bucket)
    key.key = pdf_name
    key.set_contents_from_file(bulk_pdf)
    key.set_canned_acl('public-read')
Пример #4
0
def snail_mail_pdf(request, pk):
    """Return a PDF file for a snail mail request"""
    # pylint: disable=unused-argument
    snail = get_object_or_404(SnailMailTask.objects.preload_pdf(), pk=pk)

    # generate the pdf and merge all pdf attachments
    pdf = SnailMailPDF(snail.communication, snail.category, snail.switch,
                       snail.amount)
    prepared_pdf, _page_count, _files, _mail = pdf.prepare()

    if prepared_pdf is None:
        return render(request, "error.html",
                      {"message": "There was an error processing this PDF"})

    # return as a response
    response = HttpResponse(content_type="application/pdf")
    response["Content-Disposition"] = 'filename="{}.pdf"'.format(
        snail.communication.pk)
    response.write(prepared_pdf.read())
    return response
Пример #5
0
 def test_pdf_emoji(self):
     """Strip emojis to prevent PDF generation from crashing"""
     comm = FOIACommunicationFactory(
         communication=u'Thank you\U0001f60a\n\n')
     pdf = SnailMailPDF(comm, 'n')
     pdf.generate()
     pdf.output(dest='S')
Пример #6
0
 def test_pdf_emoji(self):
     """Strip emojis to prevent PDF generation from crashing"""
     comm = FOIACommunicationFactory(
         communication="Thank you\U0001f60a\n\n")
     pdf = SnailMailPDF(comm, "n", switch=False)
     pdf.generate()
     pdf.output(dest="S").encode("latin-1")
Пример #7
0
def snail_mail_pdf(request, pk):
    """Return a PDF file for a snail mail request"""
    # pylint: disable=unused-argument
    snail = get_object_or_404(SnailMailTask.objects.preload_pdf(), pk=pk)
    merger = PdfFileMerger(strict=False)

    # generate the pdf and merge all pdf attachments
    pdf = SnailMailPDF(snail.communication, snail.category, snail.amount)
    pdf.generate()
    merger.append(StringIO(pdf.output(dest='S')))
    for file_ in snail.communication.files.all():
        if file_.get_extension() == 'pdf':
            merger.append(file_.ffile)
    output = StringIO()
    merger.write(output)

    # attach to the mail communication
    mail, _ = MailCommunication.objects.update_or_create(
        communication=snail.communication,
        defaults={
            'to_address': snail.communication.foia.address,
            'sent_datetime': timezone.now(),
        }
    )
    output.seek(0)
    mail.pdf.save(
        '{}.pdf'.format(snail.communication.pk),
        ContentFile(output.read()),
    )

    # return as a response
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'
             ] = ('filename="{}.pdf"'.format(snail.communication.pk))
    output.seek(0)
    response.write(output.read())
    return response
Пример #8
0
def snail_mail_bulk_pdf_task(pdf_name, get, **kwargs):
    """Save a PDF file for all open snail mail tasks"""
    # pylint: disable=too-many-locals
    # pylint: disable=unused-argument
    cover_info = []
    bulk_merger = PdfFileMerger(strict=False)

    snails = SnailMailTaskFilterSet(
        get,
        queryset=SnailMailTask.objects.filter(resolved=False).order_by(
            'communication__foia__agency').preload_pdf(),
    ).qs

    blank_pdf = FPDF()
    blank_pdf.add_page()
    blank = StringIO(blank_pdf.output(dest='S'))
    for snail in snails:
        # generate the pdf and merge all pdf attachments
        pdf = SnailMailPDF(snail.communication, snail.category, snail.amount)
        pdf.generate()
        single_merger = PdfFileMerger(strict=False)
        single_merger.append(StringIO(pdf.output(dest='S')))
        files = []
        for file_ in snail.communication.files.all():
            if file_.get_extension() == 'pdf':
                try:
                    single_merger.append(file_.ffile)
                    files.append((file_, 'attached'))
                except (PdfReadError, ValueError):
                    files.append((file_, 'error'))
            else:
                files.append((file_, 'skipped'))
        single_pdf = StringIO()
        try:
            single_merger.write(single_pdf)
        except PdfReadError:
            cover_info.append((snail, None, files))
            continue
        else:
            cover_info.append((snail, pdf.page, files))

        # attach to the mail communication
        mail, _ = MailCommunication.objects.update_or_create(
            communication=snail.communication,
            defaults={
                'to_address': snail.communication.foia.address,
                'sent_datetime': timezone.now(),
            })
        single_pdf.seek(0)
        mail.pdf.save(
            '{}.pdf'.format(snail.communication.pk),
            ContentFile(single_pdf.read()),
        )

        # append to the bulk pdf
        single_pdf.seek(0)
        bulk_merger.append(single_pdf)
        # ensure we align for double sided printing
        if PdfFileReader(single_pdf).getNumPages() % 2 == 1:
            blank.seek(0)
            bulk_merger.append(blank)

    # preprend the cover sheet
    cover_pdf = CoverPDF(cover_info)
    cover_pdf.generate()
    if cover_pdf.page % 2 == 1:
        cover_pdf.add_page()
    bulk_merger.merge(0, StringIO(cover_pdf.output(dest='S')))

    bulk_pdf = StringIO()
    bulk_merger.write(bulk_pdf)
    bulk_pdf.seek(0)

    conn = S3Connection(settings.AWS_ACCESS_KEY_ID,
                        settings.AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME)
    key = Key(bucket)
    key.key = pdf_name
    key.set_contents_from_file(bulk_pdf)
    key.set_canned_acl('public-read')