<commit_before>from django import http
from django.db.models import Model
from django.db.models.query import QuerySet
from django.utils import simplejson as json
from django.utils.translation import ugettext as _
from urllib import urlencode
from urlparse import urlunparse


class JSONResponseMixin(object):
    def render_to_response(self, context):
        "Returns a JSON response containing 'context' as payload"
        return self.get_json_response(self.convert_context_to_json(context))

    def get_json_response(self, content, **httpresponse_kwargs):
        "Construct an `HttpResponse` object."
        return http.HttpResponse(content,
                                 content_type='application/json',
                                 **httpresponse_kwargs)

    def convert_context_to_json(self, context):
        "Convert passed context into JSON string"
        data_key = self.context_object_name
        data = {
            '$'+_('data'): data_key,
            data_key: context.get(data_key)
        }

        try:
            # Add pagination information to data
            page_obj = context['page_obj']
            data['$'+_('page')] = page_obj.number
            data['$'+_('pages')] = page_obj.paginator.num_pages
            if page_obj.has_previous():
                data['$'+_('previous')] = self.get_page_link(page_obj.previous_page_number())
            if page_obj.has_next():
                data['$'+_('next')] = self.get_page_link(page_obj.next_page_number())
        except KeyError:
            # If no pagination is in use, ignore it
            pass

        return json.dumps(data, default=self.complex_dumps_default)

    def get_page_link(self, page):
        path = self.request.path
        query = self.request.GET.copy()
        if page == 1:
            del query['page']
        else:
            query['page'] = page
        return urlunparse(('', '', path, '', urlencode(query), ''))

    def complex_dumps_default(self, obj):
        if isinstance(obj, QuerySet):
            return list(obj)

        # This needs a bit of work - rather naive object dump
        if isinstance(obj, Model):
            return dict(((key, value) for key, value in obj.__dict__.iteritems() if not key.startswith('_')))

        return str(type(obj))


<commit_msg>We'll get there eventually<commit_after>from django import http
from django.db.models import Model
from django.db.models.query import QuerySet
from django.utils import simplejson as json
from django.utils.translation import ugettext as _
from urllib import urlencode
from urlparse import urlunparse


class JSONResponseMixin(object):
    def render_to_response(self, context):
        "Returns a JSON response containing 'context' as payload"
        return self.get_json_response(self.convert_context_to_json(context))

    def get_json_response(self, content, **httpresponse_kwargs):
        "Construct an `HttpResponse` object."
        return http.HttpResponse(content,
                                 content_type='application/json',
                                 **httpresponse_kwargs)

    def convert_context_to_json(self, context):
        "Convert passed context into JSON string"
        data_key = self.context_object_name
        data = {
            '$'+_('data'): data_key,
            data_key: context.get(data_key)
        }

        try:
            # Add pagination information to data
            page_obj = context['page_obj']
            data['$'+_('page')] = page_obj.number
            data['$'+_('pages')] = page_obj.paginator.num_pages
            if page_obj.has_previous():
                data['$'+_('previous')] = self.get_page_link(page_obj.previous_page_number())
            if page_obj.has_next():
                data['$'+_('next')] = self.get_page_link(page_obj.next_page_number())
        except (KeyError, AttributeError,):
            # If no pagination is in use, ignore it
            pass

        return json.dumps(data, default=self.complex_dumps_default)

    def get_page_link(self, page):
        path = self.request.path
        query = self.request.GET.copy()
        if page == 1:
            del query['page']
        else:
            query['page'] = page
        return urlunparse(('', '', path, '', urlencode(query), ''))

    def complex_dumps_default(self, obj):
        if isinstance(obj, QuerySet):
            return list(obj)

        # This needs a bit of work - rather naive object dump
        if isinstance(obj, Model):
            return dict(((key, value) for key, value in obj.__dict__.iteritems() if not key.startswith('_')))

        return str(type(obj))



{'fraction_non_alphanumeric': 0.10513816925734024, 'fraction_numerical': 0.0004317789291882556, 'mean_word_length': 2.419188191881919, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2990839', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1266, 'n_words': 326}