Exemplo n.º 1
0
    def save_pdf(self, save_model=True):
        """
        Save the receipt as a PDF related to this model.

        The related :class:`~.Receipt` should be validated first, of course.

        :param bool save_model: If True, immediately save this model instance.
        """
        from django_afip.views import ReceiptPDFView

        if not self.receipt.is_validated:
            raise exceptions.DjangoAfipException(
                _('Cannot generate pdf for non-authorized receipt')
            )

        self.pdf_file = File(BytesIO(), name='{}.pdf'.format(uuid.uuid4().hex))
        render_pdf(
            template='receipts/code_{}.html'.format(
                self.receipt.receipt_type.code,
            ),
            file_=self.pdf_file,
            context=ReceiptPDFView.get_context_for_pk(self.receipt_id),
        )

        if save_model:
            self.save()
Exemplo n.º 2
0
    def save_pdf(self, save_model=True):
        """
        Save the receipt as a PDF related to this model.

        The related :class:`~.Receipt` should be validated first, of course.

        :param bool save_model: If True, immediately save this model instance.
        """
        from django_afip.views import ReceiptPDFView

        if not self.receipt.is_validated:
            raise exceptions.DjangoAfipException(
                _('Cannot generate pdf for non-authorized receipt')
            )

        self.pdf_file = File(BytesIO(), name='{}.pdf'.format(uuid.uuid4().hex))
        render_pdf(
            template='receipts/code_{}.html'.format(
                self.receipt.receipt_type.code,
            ),
            file_=self.pdf_file,
            context=ReceiptPDFView.get_context_for_pk(self.receipt_id),
        )

        if save_model:
            self.save()
Exemplo n.º 3
0
def test_render_pdf_several_templates():
    file_ = io.BytesIO()
    helpers.render_pdf(
        ["test_template.html", "test_template_with_staticfile.html"],
        file_,
    )

    # Pdf for this template should be about 8kB
    assert len(file_.getvalue()) > 3000
Exemplo n.º 4
0
    def save_pdf(self, save_model: bool = True):
        """
        Save the receipt as a PDF related to this model.

        The related :class:`~.Receipt` should be validated first, of course.
        This model instance must have been saved prior to calling this method.

        :param save_model: If True, immediately save this model instance.
        """
        from django_afip.views import ReceiptPDFView

        if not self.receipt.is_validated:
            raise exceptions.DjangoAfipException(
                _("Cannot generate pdf for non-authorized receipt"))

        self.pdf_file = File(BytesIO(), name="{}.pdf".format(uuid.uuid4().hex))
        render_pdf(
            template=ReceiptPDFView().get_template_names(self.receipt),
            file_=self.pdf_file,
            context=ReceiptPDFView.get_context_for_pk(self.receipt_id),
        )

        if save_model:
            self.save()
Exemplo n.º 5
0
def test_render_pdf_with_some_non_existant():
    file_ = io.BytesIO()
    helpers.render_pdf(["idontexist.html", "test_template.html"], file_)

    # Pdf for this template should be about 8kB
    assert len(file_.getvalue()) > 3000
Exemplo n.º 6
0
def test_render_pdf_single_template():
    file_ = io.BytesIO()
    helpers.render_pdf("test_template.html", file_)

    # Pdf for this template should be about 8kB
    assert len(file_.getvalue()) > 3000
Exemplo n.º 7
0
def test_render_pdf_with_non_existant():
    file_ = io.BytesIO()

    with pytest.raises(TemplateDoesNotExist):
        helpers.render_pdf(["idontexist.html"], file_)