Commit a2172e2e authored by Florent Chehab's avatar Florent Chehab

Country update

parent cd62298f
Pipeline #22029 passed with stage
in 1 minute and 15 seconds
#OutGoing_REX_dev
pandas # usefull for reading csv but necessary for the app to work
requests
......@@ -2,8 +2,9 @@ from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin
from rex.models.university import University
from rex.models.country import Country
from rex.models.country import Country, Region
admin.site.register(University, CompareVersionAdmin)
admin.site.register(Country)
admin.site.register(Region)
This diff is collapsed.
# Generated by Django 2.0.3 on 2018-04-24 19:19
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('rex', '0001_initial'),
]
operations = [
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(null=True, on_delete=django.db.models.deletion.SET_NULL, to='rex.Region')),
],
),
migrations.RemoveField(
model_name='country',
name='iso_code',
),
migrations.AddField(
model_name='country',
name='iso_alpha3_code',
field=models.CharField(default='TMP', max_length=3, primary_key=True, serialize=False),
preserve_default=False,
),
migrations.AddField(
model_name='country',
name='region',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='rex.Region'),
),
]
from .country import Country, CountryViewSet, CountrySerializer # noqa: F401
from .country import Country, CountrySerializer, CountryViewSet # noqa: F401
from .country import Region, RegionSerializer, RegionViewSet # noqa: F401
from django.db import models
from rest_framework import serializers, viewsets, permissions
# 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)
class Country(models.Model):
name = models.CharField(max_length=200)
iso_code = models.CharField(primary_key=True, max_length=2)
iso_alpha3_code = models.CharField(primary_key=True, max_length=3)
region = models.ForeignKey(Region, on_delete=models.PROTECT, null=True)
"""
API RELATED STUFF BELLOW
"""
class CountrySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Country
fields = ('name', 'iso_code', 'url')
fields = ('name', 'iso_alpha3_code', 'region', 'url')
class CountryViewSet(viewsets.ModelViewSet):
......@@ -27,3 +42,25 @@ class CountryViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Country.objects.all() # noqa: E1101
serializer_class = CountrySerializer
class RegionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Region
fields = ('name', 'un_code', 'parent', 'url')
class RegionViewSet(viewsets.ModelViewSet):
"""
retrieve:
Retourne un pays.
list:
Retourne une liste de pays.
create:
Créée un nouveau pays.
"""
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Region.objects.all() # noqa: E1101
serializer_class = RegionSerializer
from django.conf.urls import url, include
from rex import views
from rest_framework import routers
from rex.models.country import CountryViewSet
from rex.models.country import CountryViewSet, RegionViewSet
from rest_framework.documentation import include_docs_urls
......@@ -13,5 +13,6 @@ urlpatterns = [
router = routers.DefaultRouter()
router.register(r'country', CountryViewSet)
router.register(r'region', RegionViewSet)
urlpatterns += [url(r'^api/', include(router.urls))]
......@@ -5,22 +5,48 @@ Script to insert the country data in the database
IT HAS TO BE RUN INSIDE ./manage.py shell
"""
import csv
import os
import pandas as pd
from rex.models.country import Country
from rex.models.country import Country, Region
tmp = os.path.join(os.path.realpath(__file__), '../../assets/country.csv')
country_file_loc = os.path.abspath(tmp)
if not os.path.isfile(country_file_loc):
print(country_file_loc)
raise Exception("Missing file containing country data")
data = pd.read_csv(country_file_loc, sep=',', header=0,
dtype=object).fillna('')
data = data.drop(["Least Developed Countries (LDC)",
"Land Locked Developing Countries (LLDC)",
"Small Island Developing States (SIDS)",
"Developed / Developing Countries"],
axis=1)
r = [("Region Code", "Region Name"),
("Sub-region Code", "Sub-region Name"),
("Intermediate Region Code", "Intermediate Region Name"),
("ISO-alpha3 Code", "Country or Area")]
for index, row in data.iterrows():
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 '':
# look for parent region
parent = None
if i > 0:
parent_code = row[r[i - 1][0]]
if parent_code is not '':
parent = Region.objects.get(pk=parent_code)
region = Region(un_code=region_code,
name=region_name,
parent=parent)
region.save()
else:
break
with open(country_file_loc, 'rt') as f:
reader = csv.reader(f)
for row in reader:
if len(row[0]) == 2:
c = Country(iso_code=row[0], name=row[1])
c.save()
c_code = row[r[3][0]]
c_name = row[r[3][1]]
c = Country(name=c_name, iso_alpha3_code=c_code, region=region)
c.save()
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