<commit_before>from django.utils import encoding

from rest_framework.utils.encoders import JSONEncoder
from rest_framework_json_api.renderers import JsonApiRenderer \
    as BaseJsonApiRender
from rest_framework_json_api.renderers import WrapperNotApplicable
from rest_framework_json_api.utils import snakecase


class JsonApiRenderer(BaseJsonApiRender):
    encoder_class = JSONEncoder
    wrappers = ['wrap_jsonapi_aware'] + BaseJsonApiRender.wrappers

    def wrap_jsonapi_aware(self, data, renderer_context):
        jsonapi = renderer_context.get('jsonapi', {})
        direct = jsonapi.get('direct')
        if not jsonapi or not direct:
            raise WrapperNotApplicable('No jsonapi in context')
        return data

    def model_to_resource_type(self, model):
        if model:
            return snakecase(model._meta.verbose_name_plural)
        else:
            return 'data'

    def handle_related_field(self, resource, field, field_name, request):
        '''Handle PrimaryKeyRelatedField<commit_msg>fix bug 1063567 - Use OrderedDict for JSON API<commit_after>try:
    from collections import OrderedDict
except ImportError:  # pragma: no cover
    # py26 doesn't get ordered dicts
    OrderedDict = dict

from django.utils import encoding

from rest_framework.utils.encoders import JSONEncoder
from rest_framework_json_api.renderers import JsonApiRenderer \
    as BaseJsonApiRender
from rest_framework_json_api.renderers import WrapperNotApplicable
from rest_framework_json_api.utils import snakecase


class JsonApiRenderer(BaseJsonApiRender):
    encoder_class = JSONEncoder
    wrappers = ['wrap_jsonapi_aware'] + BaseJsonApiRender.wrappers
    dict_class = OrderedDict

    def wrap_jsonapi_aware(self, data, renderer_context):
        jsonapi = renderer_context.get('jsonapi', {})
        direct = jsonapi.get('direct')
        if not jsonapi or not direct:
            raise WrapperNotApplicable('No jsonapi in context')
        return data

    def model_to_resource_type(self, model):
        if model:
            return snakecase(model._meta.verbose_name_plural)
        else:
            return 'data'

    def handle_related_field(self, resource, field, field_name, request):
        '''Handle PrimaryKeyRelatedField
{'fraction_non_alphanumeric': 0.06438721136767318, 'fraction_numerical': 0.003996447602131439, 'mean_word_length': 3.8244111349036403, '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': '2338747', 'n_tokens_mistral': 641, 'n_tokens_neox': 627, 'n_words': 161}