from apps.utils.serializers import CustomModelSerializer
from apps.bi.models import Dataset, DatasetRecord
from apps.utils.constants import EXCLUDE_FIELDS
from rest_framework import serializers
from apps.bi.services import check_sql_safe


class DatasetCreateUpdateSerializer(CustomModelSerializer):
    class Meta:
        model = Dataset
        exclude = EXCLUDE_FIELDS

    def validate(self, attrs):
        sql_query = attrs.get('sql_query', '')
        if sql_query:
            check_sql_safe(sql_query)
        return attrs


class DatasetSerializer(CustomModelSerializer):
    class Meta:
        model = Dataset
        fields = '__all__'

class DatasetRecordSerializer(CustomModelSerializer):
    class Meta:
        model = DatasetRecord
        fields = '__all__'

# class ReportSerializer(CustomModelSerializer):
#     class Meta:
#         model = Report
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS


class DataExecSerializer(serializers.Serializer):
    query = serializers.JSONField(
        label="查询字典参数", required=False, allow_null=True)
    is_test = serializers.BooleanField(label='是否测试', default=False)
    raise_exception = serializers.BooleanField(label='是否直接报错', default=False)
