Commit 6222ef22 authored by Florent Chehab's avatar Florent Chehab
Browse files

Merge branch 'merge_regions_in_countries' into 'master'

Merge regions in countries acheived

See merge request chehabfl/outgoing_rex!9
parents 2f14478a a2740333
Pipeline #26269 passed with stages
in 1 minute and 47 seconds
...@@ -2,11 +2,10 @@ from django.contrib import admin ...@@ -2,11 +2,10 @@ from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin from reversion_compare.admin import CompareVersionAdmin
from rex.models.university import University, MainCampus from rex.models.university import University, MainCampus
from rex.models.location import Country, Region, City from rex.models.location import Country, City
admin.site.register(Country) admin.site.register(Country)
admin.site.register(Region)
admin.site.register(City) admin.site.register(City)
admin.site.register(University, CompareVersionAdmin) admin.site.register(University, 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 import django.core.validators
from django.db import migrations, models from django.db import migrations, models
...@@ -28,6 +28,13 @@ class Migration(migrations.Migration): ...@@ -28,6 +28,13 @@ class Migration(migrations.Migration):
fields=[ fields=[
('name', models.CharField(max_length=200)), ('name', models.CharField(max_length=200)),
('iso_alpha2_code', models.CharField(max_length=2, primary_key=True, serialize=False)), ('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)),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
...@@ -44,14 +51,6 @@ class Migration(migrations.Migration): ...@@ -44,14 +51,6 @@ class Migration(migrations.Migration):
'abstract': False, 'abstract': False,
}, },
), ),
migrations.CreateModel(
name='Region',
fields=[
('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')),
],
),
migrations.CreateModel( migrations.CreateModel(
name='University', name='University',
fields=[ fields=[
...@@ -62,11 +61,6 @@ class Migration(migrations.Migration): ...@@ -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')), ('main_campus', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='main_campus_of', to='rex.MainCampus')),
], ],
), ),
migrations.AddField(
model_name='country',
name='region',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='rex.Region'),
),
migrations.AddField( migrations.AddField(
model_name='city', model_name='city',
name='country', name='country',
......
from .location import Country, CountrySerializer, CountryViewSet # noqa: F401 from .location import Country, CountrySerializer, CountryViewSet # noqa: F401
from .location import Region, RegionSerializer, RegionViewSet # noqa: F401
from .location import City, CitySerializer, CityViewSet # noqa: F401 from .location import City, CitySerializer, CityViewSet # noqa: F401
...@@ -4,42 +4,16 @@ from rest_framework import serializers, viewsets, permissions ...@@ -4,42 +4,16 @@ from rest_framework import serializers, viewsets, permissions
# Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/ # Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/
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"
else:
raise Exception("Impossible case found")
class Country(models.Model): class Country(models.Model):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
iso_alpha2_code = models.CharField(primary_key=True, max_length=2) iso_alpha2_code = models.CharField(primary_key=True, max_length=2)
region = models.ForeignKey( iso_alpha3_code = models.CharField(unique=True, max_length=3, blank=False, null=False)
Region, on_delete=models.PROTECT, null=True, blank=True) region_name = models.CharField(max_length=200)
region_un_code = models.CharField(max_length=3)
def get_regions(self): sub_region_name = models.CharField(max_length=200, null=True, blank=True)
regions = [None] * 3 sub_region_un_code = models.CharField(max_length=3, null=True, blank=True)
regions.append(self.region) intermediate_region_name = models.CharField(max_length=200, null=True, blank=True)
while regions[-1] is not None and regions[-1].parent is not None: intermediate_region_un_code = models.CharField(max_length=3, null=True, blank=True)
regions.append(regions[-1].parent)
regions = regions[::-1]
return regions[0:3]
class City(models.Model): class City(models.Model):
...@@ -55,38 +29,7 @@ class City(models.Model): ...@@ -55,38 +29,7 @@ class City(models.Model):
API RELATED STUFF BELLOW API RELATED STUFF BELLOW
""" """
# TODO move somewhere else
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(
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_alpha2_code',
'region', 'regions')
class DictModeViewSet(viewsets.ModelViewSet): class DictModeViewSet(viewsets.ModelViewSet):
...@@ -105,38 +48,20 @@ class DictModeViewSet(viewsets.ModelViewSet): ...@@ -105,38 +48,20 @@ class DictModeViewSet(viewsets.ModelViewSet):
return response return response
class CountryViewSet(DictModeViewSet): class CountrySerializer(serializers.ModelSerializer):
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(
view_name='country-detail',
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 Meta: class Meta:
model = Region model = Country
fields = ('name', 'un_code', 'parent', 'parent_url', 'region_type') 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,) permission_classes = (permissions.DjangoModelPermissions,)
queryset = Region.objects.all() # pylint: disable=E1101 queryset = Country.objects.all() # pylint: disable=E1101
serializer_class = RegionSerializer serializer_class = CountrySerializer
class CitySerializer(serializers.ModelSerializer): class CitySerializer(serializers.ModelSerializer):
......
from django.conf.urls import url, include from django.conf.urls import url, include
from rex import views from rex import views
from rest_framework import routers from rest_framework import routers
from rex.models.location import CountryViewSet, RegionViewSet, CityViewSet from rex.models.location import CountryViewSet, CityViewSet
from rex.models.university import MainCampusViewSet, UniversityViewSet from rex.models.university import MainCampusViewSet, UniversityViewSet
from rest_framework.documentation import include_docs_urls from rest_framework.documentation import include_docs_urls
...@@ -14,7 +14,6 @@ urlpatterns = [ ...@@ -14,7 +14,6 @@ urlpatterns = [
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'country', CountryViewSet) router.register(r'country', CountryViewSet)
router.register(r'region', RegionViewSet)
router.register(r'city', CityViewSet) router.register(r'city', CityViewSet)
router.register(r'main-campus', MainCampusViewSet) router.register(r'main-campus', MainCampusViewSet)
router.register(r'university', UniversityViewSet) router.register(r'university', UniversityViewSet)
......
...@@ -65,50 +65,32 @@ def make_post(address, data): ...@@ -65,50 +65,32 @@ def make_post(address, data):
return r.ok return r.ok
def save_country(name, code, region): def save_country(attributes):
a = api_address + "/country/" a = api_address + "/country/"
data = {'name': name, 'iso_alpha2_code': code, 'region': region} return make_post(a, attributes)
return make_post(a, data)
def save_region(name, code, parent): file_attr = ["Region Code", "Region Name",
a = api_address + "/region/" "Sub-region Code", "Sub-region Name",
data = {'name': name, 'un_code': code, 'parent': parent} "Intermediate Region Code", "Intermediate Region Name",
return make_post(a, data) "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(): for index, row in data.iterrows():
last_region_code = None country_attributes = {}
for i in range(len(r) - 1):
r_type = r[i] for f_a, db_a in zip(file_attr, db_attr):
region_code = row[r_type[0]] country_attributes[db_a] = str(row[f_a])
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
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)
inserted_regions.append(region_code)
else:
break
c_code = row[r[3][0]] tmp_c = str(row['ISO-alpha3 Code'])
c_name = row[r[3][1]] if tmp_c in conv_alpha_code.keys():
if str(c_code) in conv_alpha_code.keys(): country_attributes['iso_alpha2_code'] = conv_alpha_code[tmp_c]
c_code = conv_alpha_code[str(c_code)] save_country(country_attributes)
save_country(c_name, c_code, last_region_code)
else: else:
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