1
# Django REST Framework
2 6
from rest_framework import renderers
3 6
from rest_framework.request import override_method
4

5 6
import six
6

7

8 6
class BrowsableAPIRenderer(renderers.BrowsableAPIRenderer):
9
    '''
10
    Customizations to the default browsable API renderer.
11
    '''
12

13 6
    def get_default_renderer(self, view):
14 0
        renderer = super(BrowsableAPIRenderer, self).get_default_renderer(view)
15
        # Always use JSON renderer for browsable OPTIONS response.
16 0
        if view.request.method == 'OPTIONS' and not isinstance(renderer, renderers.JSONRenderer):
17 0
            return renderers.JSONRenderer()
18 0
        return renderer
19

20 6
    def get_context(self, data, accepted_media_type, renderer_context):
21
        # Store the associated response status to know how to populate the raw
22
        # data form.
23 0
        try:
24 0
            setattr(renderer_context['view'], '_raw_data_response_status', renderer_context['response'].status_code)
25 0
            setattr(renderer_context['view'], '_request', renderer_context['request'])
26 0
            return super(BrowsableAPIRenderer, self).get_context(data, accepted_media_type, renderer_context)
27
        finally:
28 0
            delattr(renderer_context['view'], '_raw_data_response_status')
29 0
            delattr(renderer_context['view'], '_request')
30

31 6
    def get_raw_data_form(self, data, view, method, request):
32
        # Set a flag on the view to indiciate to the view/serializer that we're
33
        # creating a raw data form for the browsable API.  Store the original
34
        # request method to determine how to populate the raw data form.
35 0
        if request.method in {'OPTIONS', 'DELETE'}:
36 0
            return
37 0
        try:
38 0
            setattr(view, '_raw_data_form_marker', True)
39 0
            setattr(view, '_raw_data_request_method', request.method)
40 0
            return super(BrowsableAPIRenderer, self).get_raw_data_form(data, view, method, request)
41
        finally:
42 0
            delattr(view, '_raw_data_form_marker')
43 0
            delattr(view, '_raw_data_request_method')
44

45 6
    def get_rendered_html_form(self, data, view, method, request):
46
        # Never show auto-generated form (only raw form).
47 0
        obj = getattr(view, 'object', None)
48 0
        if obj is None and hasattr(view, 'get_object') and hasattr(view, 'retrieve'):
49 0
            try:
50 0
                view.object = view.get_object()
51 0
                obj = view.object
52 0
            except Exception:
53 0
                obj = None
54 0
        with override_method(view, request, method) as request:
55 0
            if not self.show_form_for_method(view, method, request, obj):
56 0
                return
57 0
            if method in ('DELETE', 'OPTIONS'):
58 0
                return True  # Don't actually need to return a form
59

60 6
    def get_filter_form(self, data, view, request):
61
        # Don't show filter form in browsable API.
62 0
        return
63

64

65 6
class PlainTextRenderer(renderers.BaseRenderer):
66

67 6
    media_type = 'text/plain'
68 6
    format = 'txt'
69

70 6
    def render(self, data, media_type=None, renderer_context=None):
71 0
        if not isinstance(data, six.string_types):
72 0
            data = six.text_type(data)
73 0
        return data.encode(self.charset)
74

75

76 6
class DownloadTextRenderer(PlainTextRenderer):
77

78 6
    format = "txt_download"
79

80

81 6
class AnsiTextRenderer(PlainTextRenderer):
82

83 6
    media_type = 'text/plain'
84 6
    format = 'ansi'
85

86

87 6
class AnsiDownloadRenderer(PlainTextRenderer):
88

89 6
    format = "ansi_download"

Read our documentation on viewing source code .

Loading