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

Update country model, extraction and rename

parent 3169b73a
...@@ -2,7 +2,7 @@ from django.contrib import admin ...@@ -2,7 +2,7 @@ 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.localization import Country, Region, City from rex.models.location import Country, Region, City
admin.site.register(Country) admin.site.register(Country)
......
"alpha-2","alpha-3"
"AF","AFG"
"AX","ALA"
"AL","ALB"
"DZ","DZA"
"AS","ASM"
"AD","AND"
"AO","AGO"
"AI","AIA"
"AQ","ATA"
"AG","ATG"
"AR","ARG"
"AM","ARM"
"AW","ABW"
"AU","AUS"
"AT","AUT"
"AZ","AZE"
"BS","BHS"
"BH","BHR"
"BD","BGD"
"BB","BRB"
"BY","BLR"
"BE","BEL"
"BZ","BLZ"
"BJ","BEN"
"BM","BMU"
"BT","BTN"
"BO","BOL"
"BQ","BES"
"BA","BIH"
"BW","BWA"
"BV","BVT"
"BR","BRA"
"IO","IOT"
"BN","BRN"
"BG","BGR"
"BF","BFA"
"BI","BDI"
"CV","CPV"
"KH","KHM"
"CM","CMR"
"CA","CAN"
"KY","CYM"
"CF","CAF"
"TD","TCD"
"CL","CHL"
"CN","CHN"
"CX","CXR"
"CC","CCK"
"CO","COL"
"KM","COM"
"CG","COG"
"CD","COD"
"CK","COK"
"CR","CRI"
"CI","CIV"
"HR","HRV"
"CU","CUB"
"CW","CUW"
"CY","CYP"
"CZ","CZE"
"DK","DNK"
"DJ","DJI"
"DM","DMA"
"DO","DOM"
"EC","ECU"
"EG","EGY"
"SV","SLV"
"GQ","GNQ"
"ER","ERI"
"EE","EST"
"ET","ETH"
"FK","FLK"
"FO","FRO"
"FJ","FJI"
"FI","FIN"
"FR","FRA"
"GF","GUF"
"PF","PYF"
"TF","ATF"
"GA","GAB"
"GM","GMB"
"GE","GEO"
"DE","DEU"
"GH","GHA"
"GI","GIB"
"GR","GRC"
"GL","GRL"
"GD","GRD"
"GP","GLP"
"GU","GUM"
"GT","GTM"
"GG","GGY"
"GN","GIN"
"GW","GNB"
"GY","GUY"
"HT","HTI"
"HM","HMD"
"VA","VAT"
"HN","HND"
"HK","HKG"
"HU","HUN"
"IS","ISL"
"IN","IND"
"ID","IDN"
"IR","IRN"
"IQ","IRQ"
"IE","IRL"
"IM","IMN"
"IL","ISR"
"IT","ITA"
"JM","JAM"
"JP","JPN"
"JE","JEY"
"JO","JOR"
"KZ","KAZ"
"KE","KEN"
"KI","KIR"
"KP","PRK"
"KR","KOR"
"KW","KWT"
"KG","KGZ"
"LA","LAO"
"LV","LVA"
"LB","LBN"
"LS","LSO"
"LR","LBR"
"LY","LBY"
"LI","LIE"
"LT","LTU"
"LU","LUX"
"MO","MAC"
"MK","MKD"
"MG","MDG"
"MW","MWI"
"MY","MYS"
"MV","MDV"
"ML","MLI"
"MT","MLT"
"MH","MHL"
"MQ","MTQ"
"MR","MRT"
"MU","MUS"
"YT","MYT"
"MX","MEX"
"FM","FSM"
"MD","MDA"
"MC","MCO"
"MN","MNG"
"ME","MNE"
"MS","MSR"
"MA","MAR"
"MZ","MOZ"
"MM","MMR"
"NA","NAM"
"NR","NRU"
"NP","NPL"
"NL","NLD"
"NC","NCL"
"NZ","NZL"
"NI","NIC"
"NE","NER"
"NG","NGA"
"NU","NIU"
"NF","NFK"
"MP","MNP"
"NO","NOR"
"OM","OMN"
"PK","PAK"
"PW","PLW"
"PS","PSE"
"PA","PAN"
"PG","PNG"
"PY","PRY"
"PE","PER"
"PH","PHL"
"PN","PCN"
"PL","POL"
"PT","PRT"
"PR","PRI"
"QA","QAT"
"RE","REU"
"RO","ROU"
"RU","RUS"
"RW","RWA"
"BL","BLM"
"SH","SHN"
"KN","KNA"
"LC","LCA"
"MF","MAF"
"PM","SPM"
"VC","VCT"
"WS","WSM"
"SM","SMR"
"ST","STP"
"SA","SAU"
"SN","SEN"
"RS","SRB"
"SC","SYC"
"SL","SLE"
"SG","SGP"
"SX","SXM"
"SK","SVK"
"SI","SVN"
"SB","SLB"
"SO","SOM"
"ZA","ZAF"
"GS","SGS"
"SS","SSD"
"ES","ESP"
"LK","LKA"
"SD","SDN"
"SR","SUR"
"SJ","SJM"
"SZ","SWZ"
"SE","SWE"
"CH","CHE"
"SY","SYR"
"TW","TWN"
"TJ","TJK"
"TZ","TZA"
"TH","THA"
"TL","TLS"
"TG","TGO"
"TK","TKL"
"TO","TON"
"TT","TTO"
"TN","TUN"
"TR","TUR"
"TM","TKM"
"TC","TCA"
"TV","TUV"
"UG","UGA"
"UA","UKR"
"AE","ARE"
"GB","GBR"
"US","USA"
"UM","UMI"
"UY","URY"
"UZ","UZB"
"VU","VUT"
"VE","VEN"
"VN","VNM"
"VG","VGB"
"VI","VIR"
"WF","WLF"
"EH","ESH"
"YE","YEM"
"ZM","ZMB"
"ZW","ZWE"
# Generated by Django 2.0.3 on 2018-04-25 09:47 # Generated by Django 2.0.3 on 2018-04-25 13:44
from django.conf import settings
import django.core.validators import django.core.validators
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
...@@ -12,7 +11,6 @@ class Migration(migrations.Migration): ...@@ -12,7 +11,6 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
] ]
operations = [ operations = [
...@@ -21,28 +19,26 @@ class Migration(migrations.Migration): ...@@ -21,28 +19,26 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)), ('name', models.CharField(max_length=200)),
('local_name', models.CharField(max_length=200, null=True)), ('local_name', models.CharField(blank=True, max_length=200, null=True)),
('area', models.CharField(max_length=200, null=True)), ('area', models.CharField(blank=True, max_length=200, null=True)),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
name='Country', name='Country',
fields=[ fields=[
('name', models.CharField(max_length=200)), ('name', models.CharField(max_length=200)),
('iso_alpha3_code', models.CharField(max_length=3, primary_key=True, serialize=False)), ('iso_alpha2_code', models.CharField(max_length=2, primary_key=True, serialize=False)),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
name='MainCampus', name='MainCampus',
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('modified_date', models.DateTimeField(auto_now=True)),
('name', models.CharField(max_length=200, null=True)), ('name', models.CharField(max_length=200, null=True)),
('comment', models.TextField(blank=True, null=True)), ('comment', models.TextField(blank=True, null=True)),
('lat', models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-85.05112878), django.core.validators.MaxValueValidator(85.05112878)])), ('lat', models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-85.05112878), django.core.validators.MaxValueValidator(85.05112878)])),
('lon', models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-180), django.core.validators.MaxValueValidator(180)])), ('lon', models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-180), django.core.validators.MaxValueValidator(180)])),
('city', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rex.City')), ('city', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rex.City')),
('modified_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
], ],
options={ options={
'abstract': False, 'abstract': False,
...@@ -53,7 +49,7 @@ class Migration(migrations.Migration): ...@@ -53,7 +49,7 @@ class Migration(migrations.Migration):
fields=[ fields=[
('name', models.CharField(max_length=200)), ('name', models.CharField(max_length=200)),
('un_code', models.CharField(max_length=3, primary_key=True, serialize=False)), ('un_code', models.CharField(max_length=3, primary_key=True, serialize=False)),
('parent', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='rex.Region')), ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='rex.Region')),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
...@@ -69,7 +65,7 @@ class Migration(migrations.Migration): ...@@ -69,7 +65,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='country', model_name='country',
name='region', name='region',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='rex.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',
......
# Generated by Django 2.0.3 on 2018-04-25 09:50
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('rex', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='city',
name='area',
field=models.CharField(blank=True, max_length=200, null=True),
),
migrations.AlterField(
model_name='city',
name='local_name',
field=models.CharField(blank=True, max_length=200, null=True),
),
]
# Generated by Django 2.0.3 on 2018-04-25 10:17
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('rex', '0002_auto_20180425_1150'),
]
operations = [
migrations.RemoveField(
model_name='maincampus',
name='modified_by',
),
migrations.RemoveField(
model_name='maincampus',
name='modified_date',
),
]
# Generated by Django 2.0.3 on 2018-04-25 11:35
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('rex', '0003_auto_20180425_1217'),
]
operations = [
migrations.AlterField(
model_name='country',
name='region',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='rex.Region'),
),
migrations.AlterField(
model_name='region',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='rex.Region'),
),
]
from .localization import localization # noqa: F401 from .location import location # noqa: F401
from .university import university # noqa: F401 from .university import university # noqa: F401
from .module import module # noqa: F401 from .module import module # noqa: F401
from .localization import Country, CountrySerializer, CountryViewSet # noqa: F401
from .localization import Region, RegionSerializer, RegionViewSet # noqa: F401
from .localization import City, CitySerializer, CityViewSet # 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
...@@ -29,7 +29,7 @@ class Region(models.Model): ...@@ -29,7 +29,7 @@ class Region(models.Model):
class Country(models.Model): class Country(models.Model):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
iso_alpha3_code = models.CharField(primary_key=True, max_length=3) iso_alpha2_code = models.CharField(primary_key=True, max_length=2)
region = models.ForeignKey( region = models.ForeignKey(
Region, on_delete=models.PROTECT, null=True, blank=True) Region, on_delete=models.PROTECT, null=True, blank=True)
...@@ -85,7 +85,7 @@ class CountrySerializer(serializers.ModelSerializer): ...@@ -85,7 +85,7 @@ class CountrySerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Country model = Country
fields = ('name', 'iso_alpha3_code', fields = ('name', 'iso_alpha2_code',
'region', 'regions') 'region', 'regions')
......
from django.db import models from django.db import models
from rex.models.module import Module from rex.models.module import Module
from rex.models.localization import City from rex.models.location import City
from django.core.validators import MinValueValidator, MaxValueValidator from django.core.validators import MinValueValidator, MaxValueValidator
from rest_framework import serializers, viewsets, permissions from rest_framework import serializers, viewsets, permissions
...@@ -24,7 +24,7 @@ class Campus(Module): ...@@ -24,7 +24,7 @@ class Campus(Module):
MaxValueValidator(180)] MaxValueValidator(180)]
) )
def localization(self): def location(self):
return {'lat': self.lat, 'lon': self.lon} return {'lat': self.lat, 'lon': self.lon}
class Meta: class Meta:
...@@ -43,7 +43,7 @@ class MainCampusSerializer(serializers.ModelSerializer): ...@@ -43,7 +43,7 @@ class MainCampusSerializer(serializers.ModelSerializer):
modified_date = serializers.SerializerMethodField() modified_date = serializers.SerializerMethodField()
def get_gps_coordinates(self, obj): def get_gps_coordinates(self, obj):
return obj.localization() return obj.location()
def get_modified_by(self, obj): def get_modified_by(self, obj):
return obj.modified_by() return obj.modified_by()
......
...@@ -18,5 +18,5 @@ class University(models.Model): ...@@ -18,5 +18,5 @@ class University(models.Model):
main_campus = models.OneToOneField( main_campus = models.OneToOneField(
MainCampus, on_delete=models.PROTECT, related_name='main_campus_of') MainCampus, on_delete=models.PROTECT, related_name='main_campus_of')
def localization(self): def location(self):
return self.main_campus.localization() # pylint: disable=E1101 return self.main_campus.location() # pylint: disable=E1101
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
var universities_markers = L.markerClusterGroup(); var universities_markers = L.markerClusterGroup();
{% for univ in universities %} {% for univ in universities %}
L.marker([{{ univ.localization_lat }}, {{ univ.localization_lon }}]).addTo(universities_markers) L.marker([{{ univ.location_lat }}, {{ univ.location_lon }}]).addTo(universities_markers)
.bindPopup("<b>{{ univ.name }}</b>"); .bindPopup("<b>{{ univ.name }}</b>");
{% endfor %} {% endfor %}
......
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.localization import CountryViewSet, RegionViewSet, CityViewSet from rex.models.location import CountryViewSet, RegionViewSet, CityViewSet
from rex.models.university import MainCampusViewSet from rex.models.university import MainCampusViewSet
from rest_framework.documentation import include_docs_urls from rest_framework.documentation import include_docs_urls
......
...@@ -33,6 +33,9 @@ api_token = str(args.token) ...@@ -33,6 +33,9 @@ api_token = str(args.token)
tmp = os.path.join(os.path.realpath(__file__), '../../assets/country.csv') tmp = os.path.join(os.path.realpath(__file__), '../../assets/country.csv')
country_file_loc = os.path.abspath(tmp) country_file_loc = os.path.abspath(tmp)
tmp = os.path.join(os.path.realpath(__file__),
'../../assets/alpha-conv-table.csv')
conv_alpha_file_loc = os.path.abspath(tmp)
data = pd.read_csv(country_file_loc, sep=',', header=0, data = pd.read_csv(country_file_loc, sep=',', header=0,
dtype=object).fillna('') dtype=object).fillna('')
...@@ -42,6 +45,14 @@ data = data.drop(["Least Developed Countries (LDC)", ...@@ -42,6 +45,14 @@ data = data.drop(["Least Developed Countries (LDC)",
"Developed / Developing Countries"], "Developed / Developing Countries"],
axis=1) axis=1)
# Need to load the information for converting
# Countries alpha-3 code to alpha-2 code
data_conv = pd.read_csv(conv_alpha_file_loc, sep=',',
header=0, na_filter=False)
conv_alpha_code = {}
for index, row in data_conv.iterrows():
conv_alpha_code[row["alpha-3"]] = row["alpha-2"]
def make_post(address, data): def make_post(address, data):
h = {'Authorization': 'Token ' + api_token} h = {'Authorization': 'Token ' + api_token}
...@@ -56,7 +67,7 @@ def make_post(address, data): ...@@ -56,7 +67,7 @@ def make_post(address, data):
def save_country(name, code, region): def save_country(name, code, region):
a = api_address + "/country/" a = api_address + "/country/"
data = {'name': name, 'iso_alpha3_code': code, 'region': region} data = {'name': name, 'iso_alpha2_code': code, 'region': region}
return make_post(a, data) return make_post(a, data)
...@@ -95,7 +106,9 @@ for index, row in data.iterrows(): ...@@ -95,7 +106,9 @@ for index, row in data.iterrows():
break break
c_code = row[r[3][0]] 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]] c_name = row[r[3][1]]
save_country(c_name, c_code, last_region_code) if str(c_code) in conv_alpha_code.