Commit 03447ac6 authored by Florent Chehab's avatar Florent Chehab

API updated, insert ready

parent 3765ae44
Pipeline #22034 passed with stage
in 1 minute and 39 seconds
......@@ -22,10 +22,40 @@ API RELATED STUFF BELLOW
"""
class CountrySerializer(serializers.HyperlinkedModelSerializer):
def get_regions(country):
regions = [None] * 3
regions.append(country.region)
while regions[-1] is not None and regions[-1].parent is not None:
regions.append(regions[-1].parent)
regions = regions[::-1]
return regions[0:3]
class CountrySerializer(serializers.ModelSerializer):
regions = serializers.SerializerMethodField()
regions_url = serializers.SerializerMethodField()
def get_regions(self, obj):
return [r.un_code if r is not None else None
for r in get_regions(obj)]
def get_regions_url(self, obj):
return [self.get_region_url(r) for r in get_regions(obj)]
def get_region_url(self, region):
if region is None:
return None
return serializers.HyperlinkedRelatedField(
view_name='country-detail',
read_only=True) \
.get_url(region, view_name='region-detail',
request=self.context['request'], format=None)
class Meta:
model = Country
fields = ('name', 'iso_alpha3_code', 'region', 'url')
fields = ('name', 'iso_alpha3_code',
'region', 'regions', 'regions_url')
class CountryViewSet(viewsets.ModelViewSet):
......@@ -44,10 +74,22 @@ class CountryViewSet(viewsets.ModelViewSet):
serializer_class = CountrySerializer
class RegionSerializer(serializers.HyperlinkedModelSerializer):
class RegionSerializer(serializers.ModelSerializer):
parent_url = serializers.SerializerMethodField()
def get_parent_url(self, obj):
if obj.parent is None:
return None
return serializers.HyperlinkedRelatedField(
view_name='country-detail',
read_only=True) \
.get_url(obj.parent, view_name='region-detail',
request=self.context['request'], format=None)
class Meta:
model = Region
fields = ('name', 'un_code', 'parent', 'url')
fields = ('name', 'un_code', 'parent', 'parent_url')
class RegionViewSet(viewsets.ModelViewSet):
......
......@@ -56,16 +56,12 @@ def make_post(address, data):
def save_country(name, code, region):
a = api_address + "/country/"
if region is not None:
region = api_address + "/region/" + region + '/'
data = {'name': name, 'iso_alpha3_code': code, 'region': region}
return make_post(a, data)
def save_region(name, code, parent):
a = api_address + "/region/"
if parent is not None:
parent = a + parent + '/'
data = {'name': name, 'un_code': code, 'parent': parent}
return make_post(a, data)
......@@ -78,11 +74,13 @@ r = [("Region Code", "Region Name"),
inserted_regions = []
for index, row in data.iterrows():
last_region_code = None
for i in range(len(r) - 1):
r_type = r[i]
region_code = row[r_type[0]]
region_name = row[r_type[1]]
if region_code is not '':
last_region_code = region_code
if region_code in inserted_regions:
continue
# look for parent region
......@@ -96,10 +94,8 @@ for index, row in data.iterrows():
else:
break
if region_code is '':
region_code = None
c_code = row[r[3][0]]
if c_code is '':
c_code = row["M49 Code"] # Don't forget countries
c_name = row[r[3][1]]
save_country(c_name, c_code, region_code)
save_country(c_name, c_code, last_region_code)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment