Beispiel #1
0
class CountrySerializer(DynamicFieldsModelSerializer):
    class BasicCitySerializer(serializers.ModelSerializer):
        url = serializers.HyperlinkedIdentityField(view_name='cities:city-detail')

        class Meta:
            model = geodata.models.City
            fields = (
                'url',
                'id',
                'name'
            )

    url = serializers.HyperlinkedIdentityField(view_name='countries:country-detail')
    region = RegionSerializer(fields=('url', 'code', 'name'))
    un_region = RegionSerializer(fields=('url', 'code', 'name'))
    unesco_region = RegionSerializer(fields=('url', 'code', 'name'))
    capital_city = BasicCitySerializer()
    location = JSONField(source='center_longlat.json')
    polygon = JSONField()
    activities = serializers.SerializerMethodField()
    cities = serializers.SerializerMethodField()

    def get_activities(self, obj):
        request = self.context.get('request')
        url = request.build_absolute_uri(reverse('activities:activity-list'))
        return url + '?recipient_country=' + obj.code

    def get_cities(self, obj):
        request = self.context.get('request')
        url = request.build_absolute_uri(reverse('cities:city-list'))
        return url + '?country=' + obj.code

    class Meta:
        model = geodata.models.Country
        fields = (
            'url',
            'code',
            'pk',
            'numerical_code_un',
            'name',
            'alt_name',
            'language',
            'capital_city',
            'region',
            'un_region',
            'unesco_region',
            'dac_country_code',
            'iso3',
            'alpha3',
            'fips10',
            'data_source',
            'activities',
            'cities',
            'location',
            'polygon',
        )
Beispiel #2
0
class ActivityRecipientRegionSerializer(DynamicFieldsModelSerializer):
    region = RegionSerializer(fields=('url', 'code', 'name'))
    percentage = serializers.DecimalField(max_digits=5,
                                          decimal_places=2,
                                          coerce_to_string=False)
    vocabulary = VocabularySerializer()

    class Meta:
        model = iati.models.ActivityRecipientRegion
        fields = (
            'region',
            'percentage',
            'vocabulary',
        )