import re from rest_framework import serializers from core.apps.evaluation.models import EvaluationrequestModel class BaseEvaluationrequestSerializer(serializers.ModelSerializer): rate_type_display = serializers.CharField( source="get_rate_type_display", read_only=True ) object_type_display = serializers.CharField( source="get_object_type_display", read_only=True, default=None ) status_display = serializers.CharField( source="get_status_display", read_only=True ) location = serializers.SerializerMethodField() location_name = serializers.CharField(source="location_name", required=False) class Meta: model = EvaluationrequestModel fields = [ "id", "rate_type", "rate_type_display", "object_type", "object_type_display", "customer_inn_number", "owner_inn_number", "tex_passport", "value_determined", "rate_goal", "property_rights", "form_ownership", "worked_hours", "chassi", "need_delivering", "location", "location_name", "status", "status_display", "created_at", "updated_at", ] def get_location(self, obj): if obj.location_lat is not None and obj.location_lng is not None: return { "lat": float(obj.location_lat), "lng": float(obj.location_lng), "name": obj.location_name } return None class ListEvaluationrequestSerializer(BaseEvaluationrequestSerializer): class Meta(BaseEvaluationrequestSerializer.Meta): pass class RetrieveEvaluationrequestSerializer(BaseEvaluationrequestSerializer): class Meta(BaseEvaluationrequestSerializer.Meta): pass class CreateEvaluationrequestSerializer(serializers.ModelSerializer): location = serializers.DictField(required=False) # Frontend may send locationName locationName = serializers.CharField(write_only=True, required=False) class Meta: model = EvaluationrequestModel fields = [ "rate_type", "object_type", "customer_inn_number", "owner_inn_number", "tex_passport", "value_determined", "rate_goal", "property_rights", "form_ownership", "worked_hours", "chassi", "need_delivering", "location", "locationName", ] def validate_tex_passport(self, value): if value and not re.match(r"^[A-Z]{2}\s?\d{7}$", value): raise serializers.ValidationError( "Format: AA 1234567 (2 harf + 7 raqam)" ) return value def validate(self, attrs): rate_type = attrs.get("rate_type") object_type = attrs.get("object_type") # object_type majburiy agar rate_type=auto if rate_type == "auto" and not object_type: raise serializers.ValidationError( {"object_type": "rate_type 'auto' bo'lganda object_type majburiy."} ) # tex_passport majburiy agar rate_type=auto if rate_type == "auto" and not attrs.get("tex_passport"): raise serializers.ValidationError( {"tex_passport": "rate_type 'auto' bo'lganda tex_passport majburiy."} ) # worked_hours va chassi faqat yuk automobil uchun majburiy (truck_car) if object_type == "truck_car": if attrs.get("worked_hours") is None: raise serializers.ValidationError( {"worked_hours": "Yuk automobil uchun ishlagan soati majburiy."} ) if attrs.get("chassi") is None: raise serializers.ValidationError( {"chassi": "Yuk automobil uchun shassi majburiy."} ) return attrs def create(self, validated_data): location = validated_data.pop("location", None) location_name = validated_data.pop("locationName", None) if location: validated_data["location_lat"] = location.get("lat") validated_data["location_lng"] = location.get("lng") if not location_name: location_name = location.get("name") or location.get("locationName") if location_name: validated_data["location_name"] = location_name validated_data["user"] = self.context["request"].user return super().create(validated_data)