# coding=utf-8
from rest_framework import serializers
from .models import Order,ProgressDetails
from django.conf import settings

class OrderSerializer(serializers.ModelSerializer):
    service_user_text = serializers.CharField(source='service_user.name', read_only=True)
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)

    name = serializers.CharField(source='customer.name', read_only=True)
    tel = serializers.CharField(source='customer.tel', read_only=True)
    village = serializers.CharField(source='customer.village', read_only=True)
    address = serializers.CharField(source='customer.address', read_only=True)
    stage_progress_text = serializers.CharField(source='stage_progress.name', read_only=True)
    project_text = serializers.SerializerMethodField()

    def get_project_text(self, obj):
        project = obj.project.all()
        return ','.join(project.values_list('name', flat=True))

    class Meta:
        model = Order
        fields = '__all__'


class ProgressDetailsSerializer(serializers.ModelSerializer):
    user_text = serializers.CharField(source='user.name', read_only=True)
    operation_time_f = serializers.DateTimeField(source='operation_time', format=settings.SHORT_DATETIME_FORMAT,
                                                 read_only=True)

    class Meta:
        model = ProgressDetails
        fields = '__all__'