<commit_before>from ckantoolkit import _
import json
from ckanext.scheming.validation import scheming_validator

import logging

logger = logging.getLogger(__name__)

import ckan.lib.navl.dictization_functions as df

StopOnError = df.StopOnError


def envidat_shortname_validator(key, data, errors, context):
    value = data.get(key)
    if not value or len(value) > 80:
        errors[key].append(_('text should be maximum 80 characters long'))
        raise StopOnError


def envidat_string_uppercase(key, data, errors, context):
    """
      if the value is a string, make it uppercase, otherwise leave the value as it is.
      make all tags uppercase if possible.
    """
    # Plain value to uppercase
    tags = data[key]
    if isinstance(tags, str):
        data[key] = tags.upper()

    # tags to uppercase
    num = 0
    tag = data.get(('tags', num, 'name'), "")
    while tag:
        data[('tags', num, 'name')] = _safe_upper(tag)
        num += 1
        tag = data.get(('tags', num, 'name'), "")


def envidat_minimum_tag_count(key, data, errors, context):
    """
      count tags and raise an error if there are less than 5
    """
    min_tags = 5
    # tags to count
    num = 0
    tag = data.get(('tags', num, 'name'), "")
    while tag:
        num += 1
        tag = data.get(('tags', num, 'name'), "")

    if num < min_tags:
        errors[key].append(_('at least ' + str(min_tags) + ' tags'))
        raise StopOnError


def envidat_minimum_description_length(key, data, errors, context):
    """
      count description chars and raise an error if there are less than 100
    """
    min_length = 100
    # tags to count
    description = data.get(key, "")
    description_length = len(description)

    if description_length < min_length:
        errors[key].append(_('at least {0} characters, currently {1}'.format(min_length, description_length)))
        raise StopOnError


@scheming_validator
def envidat_reorder(field, schema):
    def validator(key, data, errors, context):
        """
          reorder sub elements
        """

        try:
            field_data = json.loads(data[key])
            sorted_list = sorted(field_data, key=lambda k: k.get('order', len(field_data)))
            for element in sorted_list:
                element.pop('order', 0)
            data[key] = json.dumps(sorted_list)
<commit_msg>Bugfix order converted to integer<commit_after>from ckantoolkit import _
import json
from ckanext.scheming.validation import scheming_validator

import logging

logger = logging.getLogger(__name__)

import ckan.lib.navl.dictization_functions as df

StopOnError = df.StopOnError


def envidat_shortname_validator(key, data, errors, context):
    value = data.get(key)
    if not value or len(value) > 80:
        errors[key].append(_('text should be maximum 80 characters long'))
        raise StopOnError


def envidat_string_uppercase(key, data, errors, context):
    """
      if the value is a string, make it uppercase, otherwise leave the value as it is.
      make all tags uppercase if possible.
    """
    # Plain value to uppercase
    tags = data[key]
    if isinstance(tags, str):
        data[key] = tags.upper()

    # tags to uppercase
    num = 0
    tag = data.get(('tags', num, 'name'), "")
    while tag:
        data[('tags', num, 'name')] = _safe_upper(tag)
        num += 1
        tag = data.get(('tags', num, 'name'), "")


def envidat_minimum_tag_count(key, data, errors, context):
    """
      count tags and raise an error if there are less than 5
    """
    min_tags = 5
    # tags to count
    num = 0
    tag = data.get(('tags', num, 'name'), "")
    while tag:
        num += 1
        tag = data.get(('tags', num, 'name'), "")

    if num < min_tags:
        errors[key].append(_('at least ' + str(min_tags) + ' tags'))
        raise StopOnError


def envidat_minimum_description_length(key, data, errors, context):
    """
      count description chars and raise an error if there are less than 100
    """
    min_length = 100
    # tags to count
    description = data.get(key, "")
    description_length = len(description)

    if description_length < min_length:
        errors[key].append(_('at least {0} characters, currently {1}'.format(min_length, description_length)))
        raise StopOnError


@scheming_validator
def envidat_reorder(field, schema):
    def validator(key, data, errors, context):
        """
          reorder sub elements
        """

        try:
            field_data = json.loads(data[key])
            sorted_list = sorted(field_data, key=lambda k: int(k.get('order', len(field_data))))
            for element in sorted_list:
                element.pop('order', 0)
            data[key] = json.dumps(sorted_list)

{'fraction_non_alphanumeric': 0.11633298208640674, 'fraction_numerical': 0.008008429926238146, 'mean_word_length': 2.7696584590945195, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4873516', 'n_tokens_mistral': 1489, 'n_tokens_neox': 1437, 'n_words': 430}