user request yuborishda api fix qilindi
This commit is contained in:
@@ -17,6 +17,8 @@ class BaseEvaluationrequestSerializer(serializers.ModelSerializer):
|
||||
)
|
||||
location = serializers.SerializerMethodField()
|
||||
|
||||
location_name = serializers.CharField(source="location_name", required=False)
|
||||
|
||||
class Meta:
|
||||
model = EvaluationrequestModel
|
||||
fields = [
|
||||
@@ -36,6 +38,7 @@ class BaseEvaluationrequestSerializer(serializers.ModelSerializer):
|
||||
"chassi",
|
||||
"need_delivering",
|
||||
"location",
|
||||
"location_name",
|
||||
"status",
|
||||
"status_display",
|
||||
"created_at",
|
||||
@@ -44,7 +47,11 @@ class BaseEvaluationrequestSerializer(serializers.ModelSerializer):
|
||||
|
||||
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 {
|
||||
"lat": float(obj.location_lat),
|
||||
"lng": float(obj.location_lng),
|
||||
"name": obj.location_name
|
||||
}
|
||||
return None
|
||||
|
||||
|
||||
@@ -59,9 +66,9 @@ class RetrieveEvaluationrequestSerializer(BaseEvaluationrequestSerializer):
|
||||
|
||||
|
||||
class CreateEvaluationrequestSerializer(serializers.ModelSerializer):
|
||||
location = serializers.DictField(
|
||||
child=serializers.FloatField(), required=False
|
||||
)
|
||||
location = serializers.DictField(required=False)
|
||||
# Frontend may send locationName
|
||||
locationName = serializers.CharField(write_only=True, required=False)
|
||||
|
||||
class Meta:
|
||||
model = EvaluationrequestModel
|
||||
@@ -79,6 +86,7 @@ class CreateEvaluationrequestSerializer(serializers.ModelSerializer):
|
||||
"chassi",
|
||||
"need_delivering",
|
||||
"location",
|
||||
"locationName",
|
||||
]
|
||||
|
||||
def validate_tex_passport(self, value):
|
||||
@@ -104,7 +112,7 @@ class CreateEvaluationrequestSerializer(serializers.ModelSerializer):
|
||||
{"tex_passport": "rate_type 'auto' bo'lganda tex_passport majburiy."}
|
||||
)
|
||||
|
||||
# worked_hours va chassi majburiy agar object_type=truck_car
|
||||
# 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(
|
||||
@@ -115,18 +123,20 @@ class CreateEvaluationrequestSerializer(serializers.ModelSerializer):
|
||||
{"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)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user