<commit_before>from wq.db.rest import app
from rest_framework import serializers
from wq.db.patterns.base.serializers import TypedAttachmentSerializer
from wq.db.rest.serializers import ModelSerializer

import swapper
from wq.db.patterns.base.models import extract_nested_key
Event = swapper.load_model('vera', 'Event')
Parameter = swapper.load_model('vera', 'Parameter')
Result = swapper.load_model('vera', 'Result')


class ResultSerializer(TypedAttachmentSerializer):
    attachment_fields = ['id', 'value']
    type_model = Parameter
    value = serializers.Field()
    object_field = 'report'

    def to_native(self, obj):
        result = super(ResultSerializer, self).to_native(obj)
        if getattr(obj.type, 'units', None) is not None:
            result['units'] = obj.type.units
        has_parent = self.parent and hasattr(self.parent.opts, 'model')
        if not has_parent:
            result['report_id'] = obj.report.pk
        return result

    def from_native(self, data, files):
        obj = super(ResultSerializer, self).from_native(data, files)
        obj.value = data['value']
        return obj

    class Meta:
        exclude = ('label', 'report_id', 'report_label',
                   'value_text', 'value_numeric')


class EventSerializer(ModelSerializer):
    is_valid = serializers.Field()

    def get_default_fields(self, *args, **kwargs):
        fields = super(EventSerializer, self).get_default_fields(
            *args, **kwargs
        )
        if self.opts.depth > 0:
            Serializer = app.router.get_serializer_for_model(Result)
            fields['results'] = Serializer(context=self.context)
        return fields


class ReportSerializer(ModelSerializer):
    is_valid = serializers.Field()

    def from_native(self, data, files):
        data = data.dict()<commit_msg>allow for dict as data in from_native<commit_after>from wq.db.rest import app
from rest_framework import serializers
from wq.db.patterns.base.serializers import TypedAttachmentSerializer
from wq.db.rest.serializers import ModelSerializer

import swapper
from wq.db.patterns.base.models import extract_nested_key
Event = swapper.load_model('vera', 'Event')
Parameter = swapper.load_model('vera', 'Parameter')
Result = swapper.load_model('vera', 'Result')


class ResultSerializer(TypedAttachmentSerializer):
    attachment_fields = ['id', 'value']
    type_model = Parameter
    value = serializers.Field()
    object_field = 'report'

    def to_native(self, obj):
        result = super(ResultSerializer, self).to_native(obj)
        if getattr(obj.type, 'units', None) is not None:
            result['units'] = obj.type.units
        has_parent = self.parent and hasattr(self.parent.opts, 'model')
        if not has_parent:
            result['report_id'] = obj.report.pk
        return result

    def from_native(self, data, files):
        obj = super(ResultSerializer, self).from_native(data, files)
        obj.value = data['value']
        return obj

    class Meta:
        exclude = ('label', 'report_id', 'report_label',
                   'value_text', 'value_numeric')


class EventSerializer(ModelSerializer):
    is_valid = serializers.Field()

    def get_default_fields(self, *args, **kwargs):
        fields = super(EventSerializer, self).get_default_fields(
            *args, **kwargs
        )
        if self.opts.depth > 0:
            Serializer = app.router.get_serializer_for_model(Result)
            fields['results'] = Serializer(context=self.context)
        return fields


class ReportSerializer(ModelSerializer):
    is_valid = serializers.Field()

    def from_native(self, data, files):
        if hasattr(data, 'dict'):
            data = data.dict()
{'fraction_non_alphanumeric': 0.11033369214208827, 'fraction_numerical': 0.0005382131324004305, 'mean_word_length': 3.2528604118993134, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5589275', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1063, 'n_words': 250}