cardinal_pythonlib.django.serve


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Helper functions to serve specific types of content via Django (e.g. raw files, PDFs).

cardinal_pythonlib.django.serve.add_download_filename(response: HttpResponse, filename: str) None[source]

Adds a Content-Disposition header to the HTTP response to say that there is an attachment with the specified filename.

cardinal_pythonlib.django.serve.add_http_headers_for_attachment(response: HttpResponse, offered_filename: str | None = None, content_type: str | None = None, as_attachment: bool = False, as_inline: bool = False, content_length: int | None = None, default_content_type: str | None = 'application/force-download') None[source]

Add HTTP headers to a Django response class object.

Parameters:
  • responseHttpResponse instance

  • offered_filename – filename that the client browser will suggest

  • content_type – HTTP content type

  • as_attachment – If True, browsers will generally save to disk. If False, they may display it inline. https://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html

  • as_inline – attempt to force inline (only if not as_attachment)

  • content_length – HTTP content length

  • default_content_type – HTTP content type to use as default, if content_type is None

cardinal_pythonlib.django.serve.file_response(data: bytes | str, content_type: str, filename: str) HttpResponse[source]

Returns an HttpResponse with an attachment containing the specified data with the specified filename as an attachment.

cardinal_pythonlib.django.serve.serve_buffer(data: bytes, offered_filename: str | None = None, content_type: str | None = None, as_attachment: bool = True, as_inline: bool = False, default_content_type: str | None = 'application/force-download') HttpResponse[source]

Serve up binary data from a buffer. Options as for serve_file().

cardinal_pythonlib.django.serve.serve_concatenated_pdf_from_disk(filenames: Iterable[str], offered_filename: str = 'crate_download.pdf', **kwargs) HttpResponse[source]

Concatenates PDFs from disk and serves them.

cardinal_pythonlib.django.serve.serve_concatenated_pdf_from_memory(pdf_plans: Iterable[PdfPlan], start_recto: bool = True, offered_filename: str = 'crate_download.pdf') HttpResponse[source]

Concatenates PDFs into memory and serves it. WATCH OUT: may not apply e.g. wkhtmltopdf options as you’d wish.

cardinal_pythonlib.django.serve.serve_file(path_to_file: str, offered_filename: str | None = None, content_type: str | None = None, as_attachment: bool = False, as_inline: bool = False, default_content_type: str | None = 'application/force-download') HttpResponseBase[source]

Serve up a file from disk.

There are two methods. One is chosen via settings.XSENDFILE (a non-standard value in the Django settings file, defaulting to False):

  1. serve directly (if XSENDFILE is absent or False);

  2. serve by asking the web server to do so via the X-SendFile directive (if XSENDFILE is True).

Parameters:
  • path_to_file – path to file on our server

  • offered_filename – filename that the client browser will suggest

  • content_type – HTTP content type

  • as_attachment – If True, browsers will generally save to disk. If False, they may display it inline. https://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html

  • as_inline – attempt to force inline (only if not as_attachment)

  • default_content_type – HTTP content type to use as default, if content_type is None

cardinal_pythonlib.django.serve.serve_pdf_from_html(html: str, offered_filename: str = 'test.pdf', **kwargs) HttpResponse[source]

Same args as pdf_from_html(). WATCH OUT: may not apply e.g. wkhtmltopdf options as you’d wish.