from rest_framework import serializers
from .models import Building


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

    class Meta:
        model = Building
        fields = ('id', 'area_name', 'building', 'floor', 'location')


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

    class Meta:
        model = Building
        fields = '__all__'

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

    def update(self, instance, validated_data):
        validated_data['area_id'] = self.context['request'].data['area']
        instance = super(BuildingSerializer, self).update(instance, validated_data)
        return instance
