A library that allows easy management of "Content-Disposition" details of HTTP responses.
Project description
Content Disposition
A library that allows easy management of 'Content-Disposition' details of HTTP responses.
So far it provides:
- RFC 5987 (file response, https://datatracker.ietf.org/doc/html/rfc5987)
Installation
- 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:
- Configure the interpreter
- Go to
File
->Settings
->Project: content-disposition
->Python interpreter
Add
aVirtualenv Environment
and configure the correctLocation
and checkInherit global site-packages
:
- Go to
- Configure the Django framework
- Go to
File
->Settings
->Languages & Frameworks
->Django
- Check
Enable Django Support
and configure the correct project root, settings and manage script location:
- Go to
- Install the requirements by running:
pip install -r requirements.txt
in a terminal within your venv - Right click on the
tests
folder and selectRun 'Test:'
- The "Run" window should list all (successfully) executed test cases:
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
Built Distribution
Close
Hashes for content-disposition-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | da4b8819c6f07f1739ba2b7ad109d1a1aea139b31f4c2a728a26de981e0f3387 |
|
MD5 | cf06073e90d4e4d59f990ddfbd4e580c |
|
BLAKE2b-256 | defdf2854446c04f1275bcc72343c06e6ad0e65a657885182281b5142ba9c136 |
Close
Hashes for content_disposition-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b335f48fe2628b390a3350eeaa46838e5778a2d1acb985032a3d21ed6c2e2ced |
|
MD5 | 21f7b170eb60e1475699450b3f9fd735 |
|
BLAKE2b-256 | 4ec978b5a0bbe05b21fae7a1e28c0d1e0611b427d45628537b0e4923be79c26f |