<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from rest_framework.serializers import Serializer, ListSerializer
from rest_framework.serializers import ModelSerializer, ValidationError
from rest_framework.serializers import CharField
from rest_framework import fields


from formidable.models import Preset, PresetArg
from formidable.forms.validations.presets import presets_register
from formidable.serializers.list import NestedListSerializerDummyUpdate
from formidable.serializers.common import WithNestedSerializer


class ClassAttrSerializer(object):

    def get_attribute(self, instance):
        return super(ClassAttrSerializer, self).get_attribute(
            instance.__class__
        )


class CharFieldClassAttr(ClassAttrSerializer, fields.CharField):
    pass


class SlugFieldClassAttr(ClassAttrSerializer, fields.SlugField):
    pass


class ListField(fields.Field):

    def to_representation(self, value):
        return list(value)


class PresetArgsSerializerList(ListSerializer):

    def get_attribute(self, instance):
        return instance.__class__._declared_arguments.values()


class PresetsArgsSerializer(Serializer):

    class Meta:
        list_serializer_class = PresetArgsSerializerList

    slug = fields.SlugField()
    label = fields.CharField()
    help_text = fields.CharField(required=False)
    placehorlder = fields.CharField(required=False)
    types = ListField()


class PresetsSerializer(Serializer):

    slug = SlugFieldClassAttr()
    label = CharFieldClassAttr()
    description = CharFieldClassAttr()
    message = CharFieldClassAttr(source='default_message')
    fields = PresetsArgsSerializer(many=True)


class PresetArgListSerializer(NestedListSerializerDummyUpdate):
    parent_name = 'preset_id'


class PresetArgModelSerializer(ModelSerializer):

    class Meta:
        model = PresetArg
        list_serializer_class = PresetArgListSerializer
        exclude = ('preset',)

    def validate(self, data):
        field_id = data.get('field_id')
        value = data.get('value')
        if field_id or value:<commit_msg>refs #60, fix the presets declarations serializer<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from rest_framework.serializers import Serializer, ListSerializer
from rest_framework.serializers import ModelSerializer, ValidationError
from rest_framework.serializers import CharField
from rest_framework import fields


from formidable.models import Preset, PresetArg
from formidable.forms.validations.presets import presets_register
from formidable.serializers.list import NestedListSerializerDummyUpdate
from formidable.serializers.common import WithNestedSerializer


class ClassAttrSerializer(object):

    def get_attribute(self, instance):
        return super(ClassAttrSerializer, self).get_attribute(
            instance.__class__
        )


class CharFieldClassAttr(ClassAttrSerializer, fields.CharField):
    pass


class SlugFieldClassAttr(ClassAttrSerializer, fields.SlugField):
    pass


class ListField(fields.Field):

    def to_representation(self, value):
        return list(value)


class PresetArgsSerializerList(ListSerializer):

    def get_attribute(self, instance):
        return instance.__class__._declared_arguments.values()


class PresetsArgsSerializer(Serializer):

    class Meta:
        list_serializer_class = PresetArgsSerializerList

    slug = fields.SlugField()
    label = fields.CharField()
    help_text = fields.CharField(required=False)
    placehorlder = fields.CharField(required=False)
    types = ListField()


class PresetsSerializer(Serializer):

    slug = SlugFieldClassAttr()
    label = CharFieldClassAttr()
    description = CharFieldClassAttr()
    message = CharFieldClassAttr(source='default_message')
    arguments = PresetsArgsSerializer(many=True)


class PresetArgListSerializer(NestedListSerializerDummyUpdate):
    parent_name = 'preset_id'


class PresetArgModelSerializer(ModelSerializer):

    class Meta:
        model = PresetArg
        list_serializer_class = PresetArgListSerializer
        exclude = ('preset',)

    def validate(self, data):
        field_id = data.get('field_id')
        value = data.get('value')
        if field_id or value:
{'fraction_non_alphanumeric': 0.07311928755565972, 'fraction_numerical': 0.0009374267635340989, 'mean_word_length': 4.5645371577574965, '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': '1190410', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1116, 'n_words': 257}