from rest_framework import serializers from .models import PropertyCustomQA class PropertyCustomQASerializer(serializers.ModelSerializer): class Meta: model = PropertyCustomQA fields = ['id', 'question', 'answer', 'order', 'is_active'] read_only_fields = ['id', 'created_at', 'updated_at'] def validate_question(self, value): if len(value.strip()) < 5: raise serializers.ValidationError("Question must be at least 5 characters") return value.strip() def validate_answer(self, value): if len(value.strip()) < 5: raise serializers.ValidationError("Answer must be at least 5 characters") return value.strip() class EnableAutoChatSerializer(serializers.Serializer): """Serializer for enabling auto-chat with custom questions""" property_id = serializers.UUIDField() enable = serializers.BooleanField(default=True) custom_questions = serializers.ListField( child=PropertyCustomQASerializer(), required=False, max_length=5, # Max 5 questions min_length=0 ) def validate_custom_questions(self, value): if len(value) > 5: raise serializers.ValidationError("Maximum 5 custom questions allowed") return value def validate(self, data): # Check if all master questions are answered first from .models import MasterQuestion, PropertyQA property_id = data['property_id'] total_master = MasterQuestion.objects.filter(is_active=True).count() answered_master = PropertyQA.objects.filter( property_id=property_id, master_question__is_active=True ).count() if answered_master < total_master: raise serializers.ValidationError( f"Please answer all {total_master} master questions first. " f"Currently answered: {answered_master}/{total_master}" ) return data