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() 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", "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)} 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( child=serializers.FloatField(), 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", ] 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 majburiy agar object_type=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."} ) # location majburiy agar need_delivering=true if attrs.get("need_delivering", True) and not attrs.get("location"): raise serializers.ValidationError( {"location": "Yetkazish kerak bo'lganda manzil majburiy."} ) return attrs def create(self, validated_data): location = validated_data.pop("location", None) if location: validated_data["location_lat"] = location.get("lat") validated_data["location_lng"] = location.get("lng") validated_data["user"] = self.context["request"].user return super().create(validated_data)