Login

DownloadView generic class view

Author:
ckniffen
Posted:
September 17, 2011
Language:
Python
Version:
1.3
Tags:
view download class-based-views
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

  1. ExtendibleModelAdminMixin by dokterbob 5 years, 5 months ago
  2. Revisiting Pygments and Markdown by djypsy 7 years, 8 months ago
  3. EasyFeed class by limodou 8 years, 1 month ago
  4. Class based generic views that automatically check permissions by humphreymurray 4 years, 3 months ago
  5. HTTP (basic) auth enabled (new-style) syndication framework feed class by hupf 4 years, 4 months ago

Comments

Please login first before commenting.