Commit a2740333 authored by Florent Chehab's avatar Florent Chehab
Browse files

Merge regions in countries acheived

parent 2f14478a
Pipeline #26264 passed with stages
......@@ -2,11 +2,10 @@ from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin
from import University, MainCampus
from rex.models.location import Country, Region, City
from rex.models.location import Country, City, CompareVersionAdmin)
# Generated by Django 2.0.3 on 2018-04-25 14:48
# Generated by Django 2.0.3 on 2018-08-18 08:44
import django.core.validators
from django.db import migrations, models
......@@ -28,6 +28,13 @@ class Migration(migrations.Migration):
('name', models.CharField(max_length=200)),
('iso_alpha2_code', models.CharField(max_length=2, primary_key=True, serialize=False)),
('iso_alpha3_code', models.CharField(max_length=3, unique=True)),
('region_name', models.CharField(max_length=200)),
('region_un_code', models.CharField(max_length=3)),
('sub_region_name', models.CharField(blank=True, max_length=200, null=True)),
('sub_region_un_code', models.CharField(blank=True, max_length=3, null=True)),
('intermediate_region_name', models.CharField(blank=True, max_length=200, null=True)),
('intermediate_region_un_code', models.CharField(blank=True, max_length=3, null=True)),
......@@ -44,14 +51,6 @@ class Migration(migrations.Migration):
'abstract': False,
('name', models.CharField(max_length=200)),
('un_code', models.CharField(max_length=3, primary_key=True, serialize=False)),
('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='rex.Region')),
......@@ -62,11 +61,6 @@ class Migration(migrations.Migration):
('main_campus', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='main_campus_of', to='rex.MainCampus')),
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='rex.Region'),
from .location import Country, CountrySerializer, CountryViewSet # noqa: F401
from .location import Region, RegionSerializer, RegionViewSet # noqa: F401
from .location import City, CitySerializer, CityViewSet # noqa: F401
......@@ -4,42 +4,16 @@ from rest_framework import serializers, viewsets, permissions
# Data model based on :
class Region(models.Model):
name = models.CharField(max_length=200)
un_code = models.CharField(primary_key=True, max_length=3)
parent = models.ForeignKey(
'self', on_delete=models.SET_NULL, null=True, blank=True)
def r_type(self):
p = 0
region = self
while region.parent is not None:
p += 1
region = region.parent
if p is 0:
return "main"
elif p is 1:
return "sub"
elif p is 2:
return "inter"
raise Exception("Impossible case found")
class Country(models.Model):
name = models.CharField(max_length=200)
iso_alpha2_code = models.CharField(primary_key=True, max_length=2)
region = models.ForeignKey(
Region, on_delete=models.PROTECT, null=True, blank=True)
def get_regions(self):
regions = [None] * 3
while regions[-1] is not None and regions[-1].parent is not None:
regions = regions[::-1]
return regions[0:3]
iso_alpha3_code = models.CharField(unique=True, max_length=3, blank=False, null=False)
region_name = models.CharField(max_length=200)
region_un_code = models.CharField(max_length=3)
sub_region_name = models.CharField(max_length=200, null=True, blank=True)
sub_region_un_code = models.CharField(max_length=3, null=True, blank=True)
intermediate_region_name = models.CharField(max_length=200, null=True, blank=True)
intermediate_region_un_code = models.CharField(max_length=3, null=True, blank=True)
class City(models.Model):
......@@ -55,38 +29,7 @@ class City(models.Model):
class CountrySerializer(serializers.ModelSerializer):
regions = serializers.SerializerMethodField()
def get_regions(self, obj):
regions = obj.get_regions()
def get_info(i):
r = regions[i]
r_code = r.un_code if r is not None else None
return {"code": r_code,
"url": self.get_region_url(regions[i])}
res = {}
res["main"] = get_info(0)
res["sub"] = get_info(1)
res["inter"] = get_info(2)
return res
def get_region_url(self, region):
if region is None:
return None
return serializers.HyperlinkedRelatedField(
read_only=True) \
.get_url(region, view_name='region-detail',
request=self.context['request'], format=None)
class Meta:
model = Country
fields = ('name', 'iso_alpha2_code',
'region', 'regions')
# TODO move somewhere else
class DictModeViewSet(viewsets.ModelViewSet):
......@@ -105,38 +48,20 @@ class DictModeViewSet(viewsets.ModelViewSet):
return response
class CountryViewSet(DictModeViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Country.objects.all() # pylint: disable=E1101
serializer_class = CountrySerializer
class RegionSerializer(serializers.ModelSerializer):
parent_url = serializers.SerializerMethodField()
region_type = serializers.SerializerMethodField()
def get_parent_url(self, obj):
if obj.parent is None:
return None
return serializers.HyperlinkedRelatedField(
read_only=True) \
.get_url(obj.parent, view_name='region-detail',
request=self.context['request'], format=None)
def get_region_type(self, obj):
return obj.r_type()
class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Region
fields = ('name', 'un_code', 'parent', 'parent_url', 'region_type')
model = Country
fields = ('name', 'iso_alpha2_code', 'iso_alpha3_code',
'region_name', 'region_un_code',
'sub_region_name', 'sub_region_un_code',
'intermediate_region_name', 'intermediate_region_un_code')
class RegionViewSet(DictModeViewSet):
class CountryViewSet(DictModeViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Region.objects.all() # pylint: disable=E1101
serializer_class = RegionSerializer
queryset = Country.objects.all() # pylint: disable=E1101
serializer_class = CountrySerializer
class CitySerializer(serializers.ModelSerializer):
from django.conf.urls import url, include
from rex import views
from rest_framework import routers
from rex.models.location import CountryViewSet, RegionViewSet, CityViewSet
from rex.models.location import CountryViewSet, CityViewSet
from import MainCampusViewSet, UniversityViewSet
from rest_framework.documentation import include_docs_urls
......@@ -14,7 +14,6 @@ urlpatterns = [
router = routers.DefaultRouter()
router.register(r'country', CountryViewSet)
router.register(r'region', RegionViewSet)
router.register(r'city', CityViewSet)
router.register(r'main-campus', MainCampusViewSet)
router.register(r'university', UniversityViewSet)
......@@ -65,50 +65,32 @@ def make_post(address, data):
return r.ok
def save_country(name, code, region):
def save_country(attributes):
a = api_address + "/country/"
data = {'name': name, 'iso_alpha2_code': code, 'region': region}
return make_post(a, data)
return make_post(a, attributes)
def save_region(name, code, parent):
a = api_address + "/region/"
data = {'name': name, 'un_code': code, 'parent': parent}
return make_post(a, data)
file_attr = ["Region Code", "Region Name",
"Sub-region Code", "Sub-region Name",
"Intermediate Region Code", "Intermediate Region Name",
"ISO-alpha3 Code", "Country or Area"]
db_attr = [
'region_un_code', 'region_name',
'sub_region_un_code', 'sub_region_name',
'intermediate_region_un_code', 'intermediate_region_name',
'iso_alpha3_code', 'name']
r = [("Region Code", "Region Name"),
("Sub-region Code", "Sub-region Name"),
("Intermediate Region Code", "Intermediate Region Name"),
("ISO-alpha3 Code", "Country or Area")]
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:
# look for parent region
parent = None
if i > 0:
parent_code = row[r[i - 1][0]]
if parent_code is not '':
parent = parent_code
save_region(region_name, region_code, parent)
c_code = row[r[3][0]]
c_name = row[r[3][1]]
if str(c_code) in conv_alpha_code.keys():
c_code = conv_alpha_code[str(c_code)]
save_country(c_name, c_code, last_region_code)
country_attributes = {}
for f_a, db_a in zip(file_attr, db_attr):
country_attributes[db_a] = str(row[f_a])
tmp_c = str(row['ISO-alpha3 Code'])
if tmp_c in conv_alpha_code.keys():
country_attributes['iso_alpha2_code'] = conv_alpha_code[tmp_c]
print("ignoring country :", c_name)
print("ignoring country :", country_attributes)
Supports Markdown
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