From 9dbdf34621b13ce11fbde30eef8d63204c87b6b8 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 1 Feb 2017 15:09:49 +0000 Subject: [PATCH] DocumentationRenderer fixes --- rest_framework/renderers.py | 2 +- rest_framework/schemas.py | 10 +++++++--- .../templates/rest_framework/docs/index.html | 2 +- rest_framework/templatetags/rest_framework.py | 10 ++++------ 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 95ea3690c..40ec51ac6 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -808,7 +808,7 @@ class DocumentationRenderer(BaseRenderer): code_style = formatter.get_style_defs('.highlight') langs = ['shell', 'javascript', 'python'] codec = coreapi.codecs.CoreJSONCodec() - schema = mark_safe(codec.encode(data)) + schema = mark_safe(json.dumps(codec.encode(data))) return { 'document': data, 'langs': langs, diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index cdfb4aff9..a875cc451 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -1,5 +1,4 @@ import re -import coreschema from collections import OrderedDict from importlib import import_module @@ -14,7 +13,7 @@ from django.utils.translation import ugettext_lazy as _ from rest_framework import exceptions, renderers, serializers from rest_framework.compat import ( - RegexURLPattern, RegexURLResolver, coreapi, uritemplate, urlparse + RegexURLPattern, RegexURLResolver, coreapi, coreschema, uritemplate, urlparse ) from rest_framework.request import clone_request from rest_framework.response import Response @@ -279,6 +278,7 @@ class SchemaGenerator(object): def __init__(self, title=None, url=None, patterns=None, urlconf=None): assert coreapi, '`coreapi` must be installed for schema support.' + assert coreschema, '`coreschema` must be installed for schema support.' if url and not url.endswith('/'): url += '/' @@ -522,6 +522,7 @@ class SchemaGenerator(object): for variable in uritemplate.variables(path): title = '' description = '' + schema_cls = coreschema.String if model is not None: # Attempt to infer a field description if possible. try: @@ -537,11 +538,14 @@ class SchemaGenerator(object): elif model_field is not None and model_field.primary_key: description = get_pk_description(model, model_field) + if isinstance(model_field, models.AutoField): + schema_cls = coreschema.Integer + field = coreapi.Field( name=variable, location='path', required=True, - schema=coreschema.String(title=title, description=description) + schema=schema_cls(title=title, description=description) ) fields.append(field) diff --git a/rest_framework/templates/rest_framework/docs/index.html b/rest_framework/templates/rest_framework/docs/index.html index 62fab7356..c486538ee 100644 --- a/rest_framework/templates/rest_framework/docs/index.html +++ b/rest_framework/templates/rest_framework/docs/index.html @@ -58,7 +58,7 @@ const coreapi = window.coreapi const codec = new coreapi.codecs.CoreJSONCodec() const schema = {{ schema }} - const doc = codec.decode(schema, {preloaded: true}) + const doc = codec.decode(schema) const client = new coreapi.Client(null, null, csrf) $('body').scrollspy({ target: '#toc' }) diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py index 014010562..3e8bba004 100644 --- a/rest_framework/templatetags/rest_framework.py +++ b/rest_framework/templatetags/rest_framework.py @@ -39,13 +39,8 @@ class FencedCodeExtension(markdown.Extension): ">normalize_whitespace") -from pygments import highlight -from pygments.lexers import get_lexer_by_name -from pygments.formatters import HtmlFormatter - - @register.tag(name='code') -def do_code(parser,token): +def highlight_code(parser,token): code = token.split_contents()[-1] nodelist = parser.parse(('endcode',)) parser.delete_first_token() @@ -60,6 +55,9 @@ class CodeNode(template.Node): self.nodelist = code def render(self, context): + from pygments import highlight + from pygments.lexers import get_lexer_by_name + from pygments.formatters import HtmlFormatter body = self.nodelist.render(context) lexer = get_lexer_by_name(self.lang, stripall=False) formatter = HtmlFormatter(nowrap=True, style=self.style)