- Author:
- ckniffen
- Posted:
- September 17, 2011
- Language:
- Python
- Version:
- 1.3
- Score:
- 0 (after 0 ratings)
Generic class view to abstract out the task of serving up files from within Django. Recommended usage is to combine it with SingleObjectMixin and extend certain methods based on your particular use case.
Example usage
class Snippet(models.Model):
name = models.CharField(max_length = 100)
slug = SlugField()
code = models.TextField()
from django.views.generic.detail import SingleObjectMixin
class DownloadSnippetView(SingleObjectMixin, DownloadView):
model = Snippet
use_xsendfile = False
mimetype = 'application/python'
def get_contents(self):
return self.get_object().code
def get_filename(self):
return self.get_object().slug + '.py'
'''
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | '''
Created on Jan 29, 2011
@author: Caleb Kniffen
'''
from django.views.generic.base import View
from django.http import HttpResponse
class DownloadView(View):
'''
Generic class view to abstract out the task of serving up files from within Django.
Recommended usage is to combine it with SingleObjectMixin and extend certain methods based on your particular use case.
Example usage::
class Snippet(models.Model):
name = models.CharField(max_length = 100)
slug = SlugField()
code = models.TextField()
from django.views.generic.detail import SingleObjectMixin
class DownloadSnippetView(SingleObjectMixin, DownloadView):
model = Snippet
use_xsendfile = False
mimetype = 'application/python'
def get_contents(self):
return self.get_object().code
def get_filename(self):
return self.get_object().slug + '.py'
'''
mimetype = None
extension = None
filename = None
use_xsendfile = True
def get_filename(self):
return self.filename
def get_extension(self):
return self.extension
def get_mimetype(self):
return self.mimetype
def get_location(self):
''' Returns the path the file is currently located at. Used only if use_xsendfile is True '''
pass
def get_contents(self):
''' Returns the contents of the file download. Used only if use_xsendfile is False '''
pass
def get(self, request, *args, **kwargs):
response = HttpResponse(mimetype=self.get_mimetype())
response['Content-Disposition'] = 'filename=' + self.get_filename()
if self.use_xsendfile is True:
response['X-Sendfile'] = self.get_location()
else:
response.write(self.get_contents())
return response
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.