from rest_framework import serializers
from django.contrib.contenttypes.models import ContentType
from BaseModels.mailSender import techSendMail


class Import_Element_Srializer(serializers.Serializer):
    element = serializers.JSONField()
    class Meta:
        fields = (
            'element',
        )


class Import_Pocket_Srializer(serializers.Serializer):
    timestamp = serializers.IntegerField()
    warehouse = serializers.CharField()
    data_list = Import_Element_Srializer(many=True)

    class Meta:
        fields = (
            'timestamp', 'warehouse', 'data_list'
        )



class Generic_base_Serializer(serializers.ModelSerializer):
    linked_object_type = serializers.CharField(required=False)

    def create(self, validated_data):
        if 'linked_object_type' in validated_data:
            try:
                validated_data['content_type'] = ContentType.objects.get(model=validated_data['linked_object_type'])
                del validated_data['linked_object_type']
            except Exception as e:
                msg = 'Ошибка создания generic объекта<br>{0}({1})<br>{2}'.format(
                    str(e),
                    str(e.args),
                    str(validated_data)
                )
                print(msg)
                title = 'ОШИБКА tE Generic_base_Serializer create'
                techSendMail(msg, title)

        return super(Generic_base_Serializer, self).create(validated_data)