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")
jmb.core also provides Class Based Views
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")
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 |
Interface to get handle templates in .odt, .ods
Object init function.
Parameters: |
|
---|
Instance render function.
Parameters: |
|
---|---|
Returns: | generated file stream if created, else None |
Call LibreOffice in server mode to convert or update the result.
Parameters: |
|
---|
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.
PdfTemplateView: | |
---|---|
return a Pdf object | |
OdtTemplateView: | |
return a Odt object | |
OdsTemplateView: | |
return a Ods object |
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')
)
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()
A mixin class that implements Odt rendering and Django response construction.
Boolean. Add ‘attachment;’ to Content-Disposition so that a popup queringwhat to do is triggerend rather that in-browser representation
forces use of openoffice even if the output is and odt file (passed to appyod render)
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() |
---|
Renders Odt document and prepares response.
Parameters: |
|
---|---|
Returns: | a rendered template (.pdf, .odt or .ods document) |
Type of the output; used to set content-type appropriately
Concrete view for serving Pdf files.
Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.