Exemplo n.º 1
0
    def render(self, obj=None, media_type=None):
        """
        Renders *obj* using the :attr:`template` set on the class.

        The context used in the template contains all the information
        needed to self-document the response to this request.
        """
        content = self._get_content(self.view, self.view.request, obj, media_type)

        put_form_instance = self._get_form_instance(self.view, 'put')
        post_form_instance = self._get_form_instance(self.view, 'post')

        if url_resolves(settings.LOGIN_URL) and url_resolves(settings.LOGOUT_URL):
            login_url = "%s?next=%s" % (settings.LOGIN_URL, quote_plus(self.view.request.path))
            logout_url = "%s?next=%s" % (settings.LOGOUT_URL, quote_plus(self.view.request.path))
        else:
            login_url = None
            logout_url = None

        name = get_name(self.view)
        description = get_description(self.view)

        markeddown = None
        if apply_markdown:
            try:
                markeddown = apply_markdown(description)
            except AttributeError:
                markeddown = None

        breadcrumb_list = get_breadcrumbs(self.view.request.path)

        template = loader.get_template(self.template)
        context = RequestContext(self.view.request, {
            'content': content,
            'view': self.view,
            'request': self.view.request, # TODO: remove
            'response': self.view.response,
            'description': description,
            'name': name,
            'markeddown': markeddown,
            'breadcrumblist': breadcrumb_list,
            'available_media_types': self.view._rendered_media_types,
            'put_form': put_form_instance,
            'post_form': post_form_instance,
            'login_url': login_url,
            'logout_url': logout_url,
            'ACCEPT_PARAM': getattr(self.view, '_ACCEPT_QUERY_PARAM', None),
            'METHOD_PARAM': getattr(self.view, '_METHOD_PARAM', None),
            'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX
        })
        
        ret = template.render(context)

        # Munge DELETE Response code to allow us to return content
        # (Do this *after* we've rendered the template so that we include
        # the normal deletion response code in the output)
        if self.view.response.status == 204:
            self.view.response.status = 200

        return ret
Exemplo n.º 2
0
 def options(self, request, *args, **kwargs):
     response_obj = {
         'name': get_name(self),
         'description': get_description(self),
         'renders': self._rendered_media_types,
         'parses': self._parsed_media_types,
     }
     form = self.get_bound_form()
     if form is not None:
         field_name_types = {}
         for name, field in form.fields.iteritems():
             field_name_types[name] = field.__class__.__name__
         response_obj['fields'] = field_name_types
     return response_obj
Exemplo n.º 3
0
 def options(self, request, *args, **kwargs):
     response_obj = {
         'name': get_name(self),
         'description': get_description(self),
         'renders': self._rendered_media_types,
         'parses': self._parsed_media_types,
     }
     form = self.get_bound_form()
     if form is not None:
         field_name_types = {}
         for name, field in form.fields.iteritems():
             field_name_types[name] = field.__class__.__name__
         response_obj['fields'] = field_name_types
     # Note 'ErrorResponse' is misleading, it's just any response
     # that should be rendered and returned immediately, without any
     # response filtering.
     raise ErrorResponse(status.HTTP_200_OK, response_obj)
Exemplo n.º 4
0
 def options(self, request, *args, **kwargs):
     response_obj = {
         'name': get_name(self),
         'description': get_description(self),
         'renders': self._rendered_media_types,
         'parses': self._parsed_media_types,
     }
     form = self.get_bound_form()
     if form is not None:
         field_name_types = {}
         for name, field in form.fields.iteritems():
             field_name_types[name] = field.__class__.__name__
         response_obj['fields'] = field_name_types
     # Note 'ErrorResponse' is misleading, it's just any response
     # that should be rendered and returned immediately, without any
     # response filtering.
     raise ErrorResponse(status.HTTP_200_OK, response_obj)
Exemplo n.º 5
0
    def test_resource_description_uses_docstring_by_default(self):
        """Ensure Resource names are based on the docstring by default."""
        class MockView(View):
            """an example docstring
            ====================

            * list
            * list
            
            another header
            --------------

                code block

            indented
            
            # hash style header #"""
        
        self.assertEquals(get_description(MockView()), DESCRIPTION)
Exemplo n.º 6
0
    def test_resource_description_uses_docstring_by_default(self):
        """Ensure Resource names are based on the docstring by default."""
        class MockView(View):
            """an example docstring
            ====================

            * list
            * list

            another header
            --------------

                code block

            indented

            # hash style header #"""

        self.assertEquals(get_description(MockView()), DESCRIPTION)
Exemplo n.º 7
0
 def test_resource_description_can_be_empty(self):
     """Ensure that if a resource has no doctring or 'description' class attribute, then it's description is the empty string"""
     class MockView(View):
         pass
     self.assertEquals(get_description(MockView()), '')
Exemplo n.º 8
0
 def test_resource_description_can_be_empty(self):
     """Ensure that if a resource has no doctring or 'description' class attribute, then it's description is the empty string"""
     class MockView(View):
         pass
     self.assertEquals(get_description(MockView()), '')