OpenOffice Utils via appy.pod

Note

Nuovo

Questo modulo rimpiazza OpenOffice utils

Il modulo utilizza la libreria appy.pod per generare documenti a partire da un template in formato .odt.

Esempio d’uso:

from django.http import HttpResponse

from jmb.core.utils.appypod import AppyPodTemplate

def example_view_pdf(request):
    context = dict(first_name="<name>", last_name="<last_name>")
    appy_pod_template = AppyPodTemplate('admin/fax/cover.odt', context)
    return HttpResponse(appy_pod_template.render(), content_type="application/pdf")

def example_view_odt(request):
    context = dict(first_name="<name>", last_name="<last_name>")
    appy_pod_template = AppyPodTemplate('admin/fax.cover.odt', context)
    return HttpResponse(appy_pod_template.render(file_type="odt"),
                        content_type="application/vnd.oasis.opendocument.text")

views

jmb.core also provides Class Based Views

conversion

A conversion utility is provided that uses OpenOffice/LibreOffice to convert among possible formats. Tipical usage allows to convert bunch of documents toghether, passing both patterns of directory to transverse and patterns:

from jmb.core.utils import appypod

appypod.convert(name_pattern='/tmp/report.odt', result_type='doc')
appypod.convert(tree='/home/data/fax', name_pattern="*doc")

settings

JMB_OOO_SERVER_POR

port at which OpenOffice/LibreOffice is listening. Default: 8100

JMB_OOO_UNO_PYTHON_PATH

Alternative python interpreter that has ‘uno’ python module

API

class jmb.core.utils.appypod.AppyPodTemplate(template=None, context=None)[source]

Interface to get handle templates in .odt, .ods

Object init function.

Parameters
  • template – (string) template file to render. Std django search is performed

  • context – context to be used on template rendering

render(file_type='pdf', context=None, forceOoCall=False)[source]

Instance render function.

Parameters
  • file_type – output file extension

  • context – context (default: self.context)

Returns

generated file stream if created, else None

save_as(output_path=None, file_type=None, context=None, forceOoCall=False)[source]

Save the template into file

Parameters

output_path – the output path

Returns

the open handler of the generated file

templ = AppyPodTemplate('admin/fax/cover.odt', context)
templ.save_as('/tmp/fax.pdf')
set_template(template_names)[source]

Set template. The first existing template according to standard django search is used.

Parameters

template – (string or list) template file to render.

jmb.core.utils.appypod.convert(name_pattern, result_type='pdf', tree=None)[source]

Call LibreOffice in server mode to convert or update the result.

Parameters
  • name_pattern – the file name to be converted. A list, a set, a tuple of filenames or a glob pattern is accepted. If tree is defined name_pattern is interpreted as a pattern.

  • file_type – the resul_type desired (default: pdf)

  • tree – if tree is defined the whole directory is traversed for pattern and name_pattern is interpreted as a pattern

OdtTemplateView

these views are Class Views implementation that return a response to serve pdf, odt or ods files. It’s pretty easy to create a view that is not class based so I’m not stating this is really needed, but you may prefere this approach.

variants

PdfTemplateView

return a Pdf object

OdtTemplateView

return a Odt object

OdsTemplateView

return a Ods object

examples

A simple example:

class MyPdfTemplateView(PdfTemplateView):
    template_name = app_label/template.odt

class MyOdtTemplateView(PdfTemplateView):
    template_name = app_label/template.odt

urlpatterns = patterns('',
     url(r'^pdf/(?P<slug>[a-zA-Z-]+)/$', MyPdfTemplateView.as_view(),  name='pdf_detail')
     url(r'^odt/(?P<slug>[a-zA-Z-]+)/$', MyOdtTemplateView.as_view(),  name='odt_detail')
)

debug

Since it’s pretty standard to create the .odt template via try and fix you can create a method named debug_template to step into and just lanch creation of the file w/o recreating the context:

def debug_template(self, template, context):
      template.save_as('/tmp/output.odt', context=context)
      import ipdb; ipdb.set_trace()

API

class jmb.core.views.odt.OdtTemplateResponseMixin[source]

A mixin class that implements Odt rendering and Django response construction.

attachment = False

Boolean. Add ‘attachment;’ to Content-Disposition so that a popup queringwhat to do is triggerend rather that in-browser representation

forceOoCall = False

forces use of openoffice even if the output is and odt file (passed to appyod render)

get_output_filename()[source]

Returns pdf_filename value by default.

If left blank the browser will display the Odt inline. Otherwise it will pop up the “Save as..” dialog.

Return type

str()

get_response(context, **response_kwargs)[source]

Renders Odt document and prepares response.

Parameters
Returns

a rendered template (.pdf, .odt or .ods document)

get_template_names()[source]

Return the template names (string or list)

output_type = None

Type of the output; used to set content-type appropriately

render_to_response(context, **response_kwargs)[source]

Return a rendered template as an HttpRespons :rtype: django.http.HttpResponse

class jmb.core.views.odt.PdfTemplateView(**kwargs)[source]

Concrete view for serving Pdf files.

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

get(request, *args, **kwargs)[source]

Handles GET request and returns HTTP response.

class jmb.core.views.odt.OdtTemplateView(**kwargs)[source]

Concrete view for serving Odt files.

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

class jmb.core.views.odt.OdsTemplateView(**kwargs)[source]

Concrete view for serving Ods files.

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.