# coding=utf-8

from rest_framework import serializers
from .models import Area,Department


class AreaSerializer(serializers.ModelSerializer):
    enable_text = serializers.SerializerMethodField()

    def get_enable_text(self,obj):
        if obj.enable:
            return '是'
        return '否'

    class Meta:
        model = Area
        fields = '__all__'

    def create(self, validated_data):
        validated_data['tenant'] = self.context['request'].user.employee.tenant
        validated_data['create_user'] = self.context['request'].user
        instance = super(AreaSerializer, self).create(validated_data)
        return instance

class AreaDepartmentSerializer(serializers.ModelSerializer):
    area_name = serializers.CharField(source='area.name', read_only=True)
    enable_text = serializers.SerializerMethodField()

    def get_enable_text(self,obj):
        if obj.enable:
            return '是'
        return '否'

    class Meta:
        model = Department
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        instance = super(AreaDepartmentSerializer, self).create(validated_data)
        return instance
