<commit_before>from datetime import datetime, timedelta
import json
from django import template
from django.conf import settings
from django.core.urlresolvers import reverse
from django.template.base import Variable, VariableDoesNotExist
from django.template.loader import render_to_string
from django.utils.datastructures import SortedDict
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.http import QueryDict
from corehq.apps.domain.models import Domain
from dimagi.utils.couch.cache import cache_core
from dimagi.utils.logging import notify_exception
from dimagi.utils.web import json_handler

import corehq.apps.style.utils as style_utils


register = template.Library()


@register.filter
def JSON(obj):
    # json.dumps does not properly convert QueryDict array parameter to json
    if isinstance(obj, QueryDict):
        obj = dict(obj)
    return mark_safe(json.dumps(obj, default=json_handler))


@register.filter
def to_javascript_string(obj):
    # seriously: http://stackoverflow.com/a/1068548/8207
    return mark_safe(JSON(obj).replace('</script>', '<" + "/script>'))<commit_msg>Send error email if coercing data to json failed<commit_after>from datetime import datetime, timedelta
import json
from django import template
from django.conf import settings
from django.core.urlresolvers import reverse
from django.template.base import Variable, VariableDoesNotExist
from django.template.loader import render_to_string
from django.utils.datastructures import SortedDict
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.http import QueryDict
from corehq.apps.domain.models import Domain
from dimagi.utils.logging import notify_exception
from dimagi.utils.web import json_handler

import corehq.apps.style.utils as style_utils


register = template.Library()


@register.filter
def JSON(obj):
    # json.dumps does not properly convert QueryDict array parameter to json
    if isinstance(obj, QueryDict):
        obj = dict(obj)
    try:
        return mark_safe(json.dumps(obj, default=json_handler))
    except TypeError as e:
        msg = ("Unserializable data was sent to the `|JSON` template tag.  "
               "If DEBUG is off, Django will silently swallow this error.")
        notify_exception(None, message=msg)
        raise e


@register.filter
def to_javascript_string(obj):
    # seriously: http://stackoverflow.com/a/1068548/8207
    return mark_safe(JSON(obj).replace('</script>', '<" + "/script>'))
{'fraction_non_alphanumeric': 0.0784313725490196, 'fraction_numerical': 0.008627450980392156, 'mean_word_length': 4.758465011286682, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5557747', 'n_tokens_mistral': 730, 'n_tokens_neox': 701, 'n_words': 224}