def get_view_description(view_cls, html=False): """ Given a view class, return a textual description to represent the view. This name is used in the browsable API, and in OPTIONS responses. This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting. """ description = view_cls.__doc__ or '' description = formatting.dedent(smart_text(description)) if hasattr(view_cls, 'serializer_class'): doc_url = get_doc_url( 'api' '{0}s'.format( view_cls.serializer_class.Meta.model.__name__.lower() ) ) else: doc_url = get_doc_url('api') description = '\n\n'.join(( description, DOC_TEXT.format(doc_url) )) if html: return formatting.markup_description(description) return description
def get_view_description(view_cls, html=False, instance=None): """ Given a view class, return a textual description to represent the view. This name is used in the browsable API, and in OPTIONS responses. This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting. """ # doc can come from the class or from a detail_route or a list_route method documented_object = view_cls if instance: view_method = get_subroute_method(instance) if view_method: documented_object = view_method description = documented_object.__doc__ or '' description = formatting.dedent(smart_text(description)) if hasattr(documented_object, 'filter_class'): default_filter = documented_object.filter_class() filters = default_filter.filters filters_doc = description_tpl.render( Context({ 'filters': filters, 'default_ordering': default_filter._default_ordering_field() })) description += filters_doc if html: return formatting.markup_description(description) return description
def get_view_description(view_cls, html=False): """ Given a view class, return a textual description to represent the view. This name is used in the browsable API, and in OPTIONS responses. This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting. """ description = view_cls.__doc__ or '' description = formatting.dedent(smart_text(description)) if hasattr(getattr(view_cls, 'serializer_class', 'None'), 'Meta'): doc_url = get_doc_url( 'api', '{0}s'.format( view_cls.serializer_class.Meta.model.__name__.lower() ) ) else: doc_url = get_doc_url('api') if html: return ( formatting.markup_description(description) + mark_safe(DOC_TEXT.format(doc_url)) ) return description
def get_view_doc(view, html=True): """ Build view documentation. Return in html format. If you want in markdown format, use html=False """ try: description = view.__doc__ or '' description = formatting.dedent(smart_text(description)) # include filters in description filter_fields = get_filter_fields(view) if filter_fields: filter_doc = ['\n\n\n## Filters', ''] for f in filter_fields: filter_doc.append('- `%s`' % f) description += '\n'.join(filter_doc) # replace {api_url} by current base url api_url = "/api" description = description.replace('{api_url}', api_url) if html: description = formatting.markup_description(description) return description except: import traceback traceback.print_exc() raise
def get_view_description(self, html=False): description = self.__doc__ or """ Returns a nested list of objects that would be also be deleted when this object is deleted. """ description = formatting.dedent(description) if html: return formatting.markup_description(description) return description
def get_view_description(self, obj, attr='__doc__', html=False): """ Get the doc string from either cls or function, parse it and return """ description = getattr(obj, attr, "No description provided by developer") description = formatting.dedent(smart_text(description)) if html: return formatting.markup_description(description) return description
def get_view_description(view_cls, html=False): """ Given a view class, return a textual description to represent the view. This name is used in the browsable API, and in OPTIONS responses. This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting. """ description = view_cls.__doc__ or '' description = formatting.dedent(smart_text(description)) if html: return formatting.markup_description(description) return description
def get_description(self, view, status_code): try: description = getattr(view, view.action).__doc__ or '' description = formatting.dedent( smart_text(description)).split('---') description = description[0] if isinstance(description, list) else description return formatting.markup_description(description) except (AttributeError, TypeError): return super(ShoutitBrowsableAPIRenderer, self).get_description(view, status_code)
def format_docstring(self, view, method, docstring): macros = settings.BROWSABLE_DOCUMENT_MACROS if view: macros['FILTERS'] = get_filters(view) if '%(SERIALIZER)s' in docstring: macros['SERIALIZER'] = get_serializer(view, include_read_only=True) if '%(WRITABLE_SERIALIZER)s' in docstring: macros['WRITABLE_SERIALIZER'] = get_serializer(view, include_read_only=False) string = formatting.dedent(docstring) formatted = string % macros formatted = self.substitute_urls(view, method, formatted) string = smart_text(formatted) return formatting.markup_description(string)
def get_view_description(view_cls, html=False): from django_markwhat.templatetags.markup import restructuredtext description = view_cls.__doc__ or '' description = formatting.dedent(smart_text(description)) if html: rst_doc = getattr(view_cls, 'rst_doc', None) if rst_doc: if isinstance(rst_doc, str): path = os.path.dirname(sys.modules[view_cls.__module__].__file__) path = os.path.join(path, rst_doc) with open(path) as rst: return restructuredtext(rst.read()) return restructuredtext(description) return formatting.markup_description(description) return description
def get_view_description(cls, html=False): description = '' if getattr(cls, 'get_description', None): cache_key = cls.__name__ cached_description = cache.get(cache_key) if not cached_description: description = cls.get_description() description = formatting.dedent(smart_text(description)) # Cache for 1 hour (if we update description, it will take 1 hour to show) cache.set(cache_key, description, 60 * 60) else: description = cached_description if html: return formatting.markup_description(description) return description
def get_view_description(cls, html=False): description = '' if getattr(cls, 'get_description', None): cache_key = cls.__name__ cached_description = cache.get(cache_key) if not cached_description: description = cls.get_description() description = formatting.dedent(smart_text(description)) # Cache for 1 hour (if we update description, it will take 1 hour to show) cache.set(cache_key, description, 60*60) else: description = cached_description if html: return formatting.markup_description(description) return description
def markup_description(description): split_into_tabs = tab_markup.split(description) if len(split_into_tabs) == 1: # no tabs just return straight markup return formatting.markup_description(description) tab_content = split_into_tabs[0:None:2] tab_labels = ['About'] + split_into_tabs[1:None:2] chunks = [] chunks.append('<ul class="nav nav-tabs">') for i, label in enumerate(tab_labels): chunks.append(make_tab_item(i + 1, label)) chunks.append('</ul><div class="well tab-content">') for i, content in enumerate(tab_content): chunks.append(make_tab_pane(i + 1, markup_description(content))) chunks.append('</div>') return ''.join(chunks)
def get_view_description(view, html=False): """ Given a view instance, return a textual description to represent the view. This name is used in the browsable API, and in OPTIONS responses. This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting. """ # Description may be set by some Views, such as a ViewSet. description = getattr(view, 'description', None) if description is None: description = view.__class__.__doc__ or '' description = formatting.dedent(smart_str(description)) if html: return formatting.markup_description(description) return description
def get_view_description(view, html=False): """ Given a view class, return a textual description to represent the view. This name is used in the browsable API, and in OPTIONS responses. This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting. """ # Description may be set by some Views, such as a ViewSet. description = getattr(view, 'description', None) if description is None: description = view.__class__.__doc__ or '' description = formatting.dedent(smart_text(description)) if html: return formatting.markup_description(description) return description
def format_docstring(self, view, method, docstring): macros = settings.BROWSABLE_DOCUMENT_MACROS if view: macros['FILTERS'] = get_filters(view) # If the API has the LIST method, show ordering field info. if 'list' == method and view.serializer_class: macros['FILTERS'] += ORDERING_STRING if '%(SERIALIZER)s' in docstring: macros['SERIALIZER'] = get_serializer(view, include_read_only=True) if '%(WRITABLE_SERIALIZER)s' in docstring: macros['WRITABLE_SERIALIZER'] = get_serializer( view, include_read_only=False) if hasattr(view, 'docstring_macros'): macros.update(view.docstring_macros) string = formatting.dedent(docstring) formatted = string % macros formatted = self.substitute_urls(view, method, formatted) string = smart_text(formatted) return formatting.markup_description(string)
def get_view_description(view_cls, html=False): """Given a view class, return a textual description to represent the view. This name is used in the browsable API, and in OPTIONS responses. This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting. """ description = view_cls.__doc__ or "" description = formatting.dedent(smart_str(description)) if hasattr(getattr(view_cls, "serializer_class", "None"), "Meta"): doc_url = get_doc_url( "api", "{0}s".format( view_cls.serializer_class.Meta.model.__name__.lower())) else: doc_url = get_doc_url("api") if html: return formatting.markup_description(description) + mark_safe( DOC_TEXT.format(doc_url)) return description
def format_docstring(self, view, method, docstring): macros = settings.BROWSABLE_DOCUMENT_MACROS if view: macros['FILTERS'] = get_filters(view) if 'list' == method: ordering_field = get_ordering_field(view, method) if ordering_field: ordering_string = ORDERING_STRING + " %s ." % ordering_field macros['FILTERS'] += ordering_string if '%(SERIALIZER)s' in docstring: macros['SERIALIZER'] = get_serializer(view, include_read_only=True) if '%(WRITABLE_SERIALIZER)s' in docstring: macros['WRITABLE_SERIALIZER'] = get_serializer(view, include_read_only=False) if hasattr(view, 'docstring_macros'): macros.update(view.docstring_macros) string = formatting.dedent(docstring) formatted = string % macros formatted = self.substitute_urls(view, method, formatted) string = smart_text(formatted) return formatting.markup_description(string)
def format_docstring(self, view, method, docstring): macros = settings.BROWSABLE_DOCUMENT_MACROS if view: macros['FILTERS'] = get_filters(view) if 'list' == method: ordering_field = get_ordering_field(view, method) if ordering_field: ordering_string = ORDERING_STRING + " %s . " % ordering_field + ORDERING_STRING_NESTED macros['FILTERS'] += ordering_string if '%(SERIALIZER)s' in docstring: macros['SERIALIZER'] = get_serializer(view, include_read_only=True) if '%(WRITABLE_SERIALIZER)s' in docstring: macros['WRITABLE_SERIALIZER'] = get_serializer( view, include_read_only=False) if hasattr(view, 'docstring_macros'): macros.update(view.docstring_macros) string = formatting.dedent(docstring) formatted = string % macros formatted = self.substitute_urls(view, method, formatted) string = smart_text(formatted) return formatting.markup_description(string)
def format_docstring(self, docstring): formatted = docstring % settings.BROWSABLE_DOCUMENT_MACROS string = formatting.dedent(smart_text(formatted)) return formatting.markup_description(string)
def markdown(value): return markup_description(value)
def get_view_description(cls, html=False): description = cls.__doc__ or '' description = formatting.dedent(smart_text(description)) if html: return formatting.markup_description(description) return description