<commit_before>from coreapi.compat import force_bytes
from django.conf import settings
from django.shortcuts import resolve_url
from django.template import loader, RequestContext
from openapi_codec import OpenAPICodec
from rest_framework.renderers import BaseRenderer
import simplejson as json

from .settings import swagger_settings


class OpenAPIRenderer(BaseRenderer):
    media_type = 'application/openapi+json'
    charset = None
    format = 'openapi'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        codec = OpenAPICodec()
        data = json.loads(codec.dump(data))

        if swagger_settings.SECURITY_DEFINITIONS:
            data['securityDefinitions'] = swagger_settings.SECURITY_DEFINITIONS

        return force_bytes(json.dumps(data))


class SwaggerUIRenderer(BaseRenderer):
    media_type = 'text/html'
    format = 'swagger'
    template = 'rest_framework_swagger/index.html'
    charset = 'utf-8'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        context = self.get_context(renderer_context)
        template = loader.get_template(self.template)

        return template.render(context)

    def get_context(self, renderer_context):
        renderer_context['USE_SESSION_AUTH'] = \
            swagger_settings.USE_SESSION_AUTH
        self.set_session_auth_urls(renderer_context)

        return RequestContext(renderer_context['request'], renderer_context)

    def set_session_auth_urls(self, renderer_context):
        path = renderer_context['request'].path
        urls = {
            'LOGIN_URL': settings.LOGIN_URL,
            'LOGOUT_URL': settings.LOGOUT_URL
        }
        renderer_context.update({
            key: '%s?next=%s' % (resolve_url(val), path)
            for key, val in urls.items()
        })<commit_msg>Use render() shortcut for UI. Removes RendererContext 1.10 deprecation warning.<commit_after>from coreapi.compat import force_bytes
from django.conf import settings
from django.shortcuts import render, resolve_url
from openapi_codec import OpenAPICodec
from rest_framework.renderers import BaseRenderer
import simplejson as json

from .settings import swagger_settings


class OpenAPIRenderer(BaseRenderer):
    media_type = 'application/openapi+json'
    charset = None
    format = 'openapi'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        codec = OpenAPICodec()
        data = json.loads(codec.dump(data))

        if swagger_settings.SECURITY_DEFINITIONS:
            data['securityDefinitions'] = swagger_settings.SECURITY_DEFINITIONS

        return force_bytes(json.dumps(data))


class SwaggerUIRenderer(BaseRenderer):
    media_type = 'text/html'
    format = 'swagger'
    template = 'rest_framework_swagger/index.html'
    charset = 'utf-8'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        self.set_context(renderer_context)
        return render(
            renderer_context['request'],
            self.template,
            renderer_context
        )

    def set_context(self, renderer_context):
        renderer_context['USE_SESSION_AUTH'] = \
            swagger_settings.USE_SESSION_AUTH
        self.set_session_auth_urls(renderer_context)

    def set_session_auth_urls(self, renderer_context):
        path = renderer_context['request'].path
        urls = {
            'LOGIN_URL': settings.LOGIN_URL,
            'LOGOUT_URL': settings.LOGOUT_URL
        }
        renderer_context.update({
            key: '%s?next=%s' % (resolve_url(val), path)
            for key, val in urls.items()
        })
{'fraction_non_alphanumeric': 0.08572216097023153, 'fraction_numerical': 0.0013781697905181918, 'mean_word_length': 3.3617788461538463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4920558', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1053, 'n_words': 214}