from rest_framework import serializers

from . import models


class CountrySerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Country
        exclude = ['created', 'updated']


class RegionSerializer(serializers.ModelSerializer):

    country = CountrySerializer()

    class Meta:
        model = models.Region
        exclude = ['created', 'updated']


class ProvinceSerializer(serializers.ModelSerializer):

    region = RegionSerializer()

    class Meta:
        model = models.Province
        exclude = ['created', 'updated']


class CityWriteSerializer(serializers.ModelSerializer):

    region = serializers.IntegerField()

    def validate_region(self, value):
        return models.Region.objects.using(self.context['db']).filter(id=value).first()

    class Meta:
        model = models.City
        exclude = ['created', 'updated']


class CitySerializer(serializers.ModelSerializer):

    class Meta:
        model = models.City
        exclude = ['created', 'updated']

