Skip to main content

A library that allows easy management of "Content-Disposition" details of HTTP responses.

Project description

Content Disposition

PyPI version Run linter and tests Codecov

A library that allows easy management of 'Content-Disposition' details of HTTP responses.

So far it provides:

Installation

  1. Install using pip:
pip install content-disposition

Usage

Use the rfc5987_content_disposition function to set an HTTP response's 'Content-Type' field in any endpoint, e.g.:

# within views.py

from django.http import FileResponse
from rest_framework import viewsets
from rest_framework.decorators import action

from content_disposition import rfc5987_content_disposition

class MyViewSet(viewsets.ModelViewSet):
    ...

    @action(
        detail=True,
        methods="get",
        url_path=r"download",
    )
    def download_route(self, request, pk=None):
        """
        Assuming that self.get_object() returns a model defining
        'name = models.CharField(...)', 'file = models.FileField(...)' and 'mime = models.CharField(...)'
        whereas 'mime' represents the correct mime_type related to 'file'
        """
        instance = self.get_object()

        response = FileResponse(
            instance.file,
            content_type=instance.mime,
        )
        response["Content-Disposition"] = rfc5987_content_disposition(instance.name)

        return response
    ...

Run tests in PyCharm

The package comes with a Django test app to verify functionalities in a realistic environment. To locally run the provided test cases, e.g. in your IntelliJ IDEA, you can:

  1. Configure the interpreter
    1. Go to File -> Settings -> Project: content-disposition -> Python interpreter
    2. Add a Virtualenv Environment and configure the correct Location and check Inherit global site-packages:
      1. python interpreter config
  2. Configure the Django framework
    1. Go to File -> Settings -> Languages & Frameworks -> Django
    2. Check Enable Django Support and configure the correct project root, settings and manage script location:
      1. django_config
  3. Install the requirements by running: pip install -r requirements.txt in a terminal within your venv
  4. Right click on the tests folder and select Run 'Test:'
  5. The "Run" window should list all (successfully) executed test cases:
    1. test_results

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

content-disposition-1.2.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

content_disposition-1.2.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file content-disposition-1.2.0.tar.gz.

File metadata

  • Download URL: content-disposition-1.2.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for content-disposition-1.2.0.tar.gz
Algorithm Hash digest
SHA256 5f7d955f4d9c0c85563de09ca21f9ebaac960f107d9a0f905728ac5961ec42c9
MD5 c380c98ab40820c000e443e0d8514878
BLAKE2b-256 b5de38bfa1e87141810c024ee2ddd237cc145ee75641a9cd8bb69b438ca565f2

See more details on using hashes here.

File details

Details for the file content_disposition-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for content_disposition-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfc04dd7ad3e6b32a0b61e9f8e290e7711348c6bcb799b45e9b994752df14085
MD5 6eb707085522e911dfa92b950876b6a6
BLAKE2b-256 b44454de305c65984c6ec39799212a117d2131cb375337eeab9c5d0326434576

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page