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
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
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)
def breadcrumbs_recursive(url, breadcrumbs_list): """Add tuples of (name, url) to the breadcrumbs list, progressively chomping off parts of the url.""" try: (view, unused_args, unused_kwargs) = resolve(url) except: pass else: # Check if this is a REST framework view, and if so add it to the breadcrumbs if isinstance(getattr(view, 'cls_instance', None), View): breadcrumbs_list.insert(0, (get_name(view), url)) if url == '': # All done return breadcrumbs_list elif url.endswith('/'): # Drop trailing slash off the end and continue to try to resolve more breadcrumbs return breadcrumbs_recursive(url.rstrip('/'), breadcrumbs_list) # Drop trailing non-slash off the end and continue to try to resolve more breadcrumbs return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list)
def breadcrumbs_recursive(url, breadcrumbs_list): """Add tuples of (name, url) to the breadcrumbs list, progressively chomping off parts of the url.""" try: (view, unused_args, unused_kwargs) = resolve(url) except Exception: pass else: # Check if this is a REST framework view, and if so add it to the breadcrumbs if isinstance(getattr(view, 'cls_instance', None), View): breadcrumbs_list.insert(0, (get_name(view), url)) if url == '': # All done return breadcrumbs_list elif url.endswith('/'): # Drop trailing slash off the end and continue to try to resolve more breadcrumbs return breadcrumbs_recursive(url.rstrip('/'), breadcrumbs_list) # Drop trailing non-slash off the end and continue to try to resolve more breadcrumbs return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list)
def test_resource_name_uses_classname_by_default(self): """Ensure Resource names are based on the classname by default.""" class MockView(View): pass self.assertEquals(get_name(MockView()), 'Mock')