<commit_before>import json
from django import forms
from django.utils.translation import ugettext as _
from corehq.apps.userreports.models import DataSourceConfiguration
from corehq.apps.userreports.ui.widgets import JsonWidget


class ReportDataSourceField(forms.ChoiceField):

    def __init__(self, domain, *args, **kwargs):
        self.domain = domain
        available_data_sources = DataSourceConfiguration.by_domain(self.domain)
        super(ReportDataSourceField, self).__init__(
            choices=[(src._id, src.display_name) for src in available_data_sources],
            *args, **kwargs
        )


class JsonField(forms.CharField):
    widget = JsonWidget
    expected_type = None

    def __init__(self, expected_type=None, *args, **kwargs):
        self.expected_type = expected_type
        super(JsonField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        val = super(JsonField, self).to_python(value)
        try:
            return json.loads(val)
        except:
            raise forms.ValidationError(_(u'Please enter valid JSON. This is not valid: {}'.format(value)))

    def validate(self, value):
        if value in (None, '', ()) and self.required:
            raise forms.ValidationError(self.error_messages['required'])
        if self.expected_type and not isinstance(value, self.expected_type):
            raise forms.ValidationError(_(u'Expected {} but was {}'.format(self.expected_type, type(value))))<commit_msg>Revert "Remove unused method"<commit_after>import json
from django import forms
from django.utils.translation import ugettext as _
from corehq.apps.userreports.models import DataSourceConfiguration
from corehq.apps.userreports.ui.widgets import JsonWidget


class ReportDataSourceField(forms.ChoiceField):

    def __init__(self, domain, *args, **kwargs):
        self.domain = domain
        available_data_sources = DataSourceConfiguration.by_domain(self.domain)
        super(ReportDataSourceField, self).__init__(
            choices=[(src._id, src.display_name) for src in available_data_sources],
            *args, **kwargs
        )


class JsonField(forms.CharField):
    widget = JsonWidget
    expected_type = None

    def __init__(self, expected_type=None, *args, **kwargs):
        self.expected_type = expected_type
        super(JsonField, self).__init__(*args, **kwargs)

    # Is this doing anything?
    def prepare_value(self, value):
        if isinstance(value, basestring):
            try:
                return json.loads(value)
            except ValueError:
                return value
        else:
            return value

    def to_python(self, value):
        val = super(JsonField, self).to_python(value)
        try:
            return json.loads(val)
        except:
            raise forms.ValidationError(_(u'Please enter valid JSON. This is not valid: {}'.format(value)))

    def validate(self, value):
        if value in (None, '', ()) and self.required:
            raise forms.ValidationError(self.error_messages['required'])
        if self.expected_type and not isinstance(value, self.expected_type):
            raise forms.ValidationError(_(u'Expected {} but was {}'.format(self.expected_type, type(value))))
{'fraction_non_alphanumeric': 0.10142238713667286, 'fraction_numerical': 0.0, 'mean_word_length': 3.121019108280255, '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': '2539058', 'n_tokens_mistral': 885, 'n_tokens_neox': 869, 'n_words': 220}