OpenOffice Utils via appy.pod¶
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
- 
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¶
PdfTemplateViewreturn a Pdf object
OdtTemplateViewreturn a Odt object
OdsTemplateViewreturn 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_filenamevalue 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
 context – the context
response_kwargs – received from
render_to_response()
- Returns
 a rendered template (.pdf, .odt or .ods document)
- 
output_type= None¶ Type of the output; used to set content-type appropriately
- 
 
- 
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.