Пример #1
0
def create_pdf(request, slug):
    """Returns article as PDF, with the help of RML markup."""

    from rml import rml2pdf

    # We only allow PDF creation of existing articles
    article = get_object_or_404(Article, slug__iexact=slug)

    # Create XML of article, with content unescaped
    unescaped_article = render_to_string('article-to-xml.xsl', article)

    # Generate RML of article
    format = "%a, %d %b %Y %H:%M:%S"
    timestamp = datetime.datetime.today().strftime(format)
    params = {'timestamp' : xslt_param_builder(timestamp)}
    rml = StringIO.StringIO(render_xml_to_string('rml.xsl', unescaped_article, params))

    # Create the PDF
    buf = cStringIO.StringIO()
    rml2pdf.go(rml, outputFileName=buf)
    buf.seek(0)
    pdf = buf.read()
    buf.close()

    # Set up response object
    response = HttpResponse(mimetype='application/pdf')
    response.write(pdf)
    response['Content-Disposition'] = ('attachment; filename=%s.pdf' % article.title)
    return response
Пример #2
0
def render_to_response_filtered(template, queryset, params=None, mimetype="text/xml"):
    """
    Renders a queryset to xml but it strips all objects that have a field named
    published that is set to False.
    
    :param template: The xslt template path.
    :param queryset: The queryset that should be turned into xml.
    :param params: xslt params, correctly escaped using :func:`~easymode.xslt.response.prepare_string_param`
    :param mimetype: The mimetype of the response.
    :result: A :class:`~django.http.HttpResponse` with xml, with only published models.
    """
    xml = filter_unpublished(to_xml(queryset))
    return HttpResponse(render_xml_to_string(template, xml, params), mimetype=mimetype)
Пример #3
0
def render_to_response_with_revision(template, queryset, revision_id, params=None, mimetype="text/html"):
    """
    Renders a queryset to xml, but inserts the data from revision *revision_id*
    in the appropriate place. This can be used to enable previews
    
    :param template: The xslt template path.
    :param queryset: The queryset that should be turned into xml.
    :param revision_id: The id of the revision whose data we want to include in the xml.
    :param params: xslt params, correctly escaped using :func:`~easymode.xslt.response.prepare_string_param`
    :param mimetype: The mimetype of the response.
    :result: A :class:`~django.http.HttpResponse` with xml, including the data in revision *revision_id*
    """
    xml = insert_draft(revision_id, to_xml(queryset))
    return HttpResponse(render_xml_to_string(template, xml, params), mimetype=mimetype)
Пример #4
0
def render_to_response_filtered(template,
                                queryset,
                                params=None,
                                mimetype='text/xml'):
    """
    Renders a queryset to xml but it strips all objects that have a field named
    published that is set to False.
    
    :param template: The xslt template path.
    :param queryset: The queryset that should be turned into xml.
    :param params: xslt params, correctly escaped using :func:`~easymode.xslt.response.prepare_string_param`
    :param mimetype: The mimetype of the response.
    :result: A :class:`~django.http.HttpResponse` with xml, with only published models.
    """
    xml = filter_unpublished(to_xml(queryset))
    return HttpResponse(render_xml_to_string(template, xml, params),
                        mimetype=mimetype)
Пример #5
0
def render_to_response_with_revision(template,
                                     queryset,
                                     revision_id,
                                     params=None,
                                     mimetype='text/html'):
    """
    Renders a queryset to xml, but inserts the data from revision *revision_id*
    in the appropriate place. This can be used to enable previews
    
    :param template: The xslt template path.
    :param queryset: The queryset that should be turned into xml.
    :param revision_id: The id of the revision whose data we want to include in the xml.
    :param params: xslt params, correctly escaped using :func:`~easymode.xslt.response.prepare_string_param`
    :param mimetype: The mimetype of the response.
    :result: A :class:`~django.http.HttpResponse` with xml, including the data in revision *revision_id*
    """
    xml = insert_draft(revision_id, to_xml(queryset))
    return HttpResponse(render_xml_to_string(template, xml, params),
                        mimetype=mimetype)