Commit 573a4464 authored by Florent Chehab's avatar Florent Chehab

Country model operationnal, added foreign key in university, coherence between...

Country model operationnal, added foreign key in university, coherence between lat/lon and country is maintained through reverse geocoding
parent 89478fd1
{
"python.pythonPath": "/usr/bin/python3",
"python.pythonPath": "/home/florent/miniconda3/bin/python3",
"files.exclude": {
"**/.git": true,
"**/.svn": true,
......
......@@ -4,5 +4,17 @@
```
git clone
pip ...
./manage.py migrate rex
./manage.py migrate
```
Chargement des données des pays :
Avec pip requierements installer
```
./manage.py shell
```
puis à l'intérieur de ce shell
```
%run ./rex/utils/insert_country.py
```
\ No newline at end of file
......@@ -6,3 +6,5 @@ Pillow==5.0.0
django-imagekit==4.0.2
django-reversion==2.0.13
django-reversion-compare==0.8.4
reverse-geocoder==1.5.1
ipython
\ No newline at end of file
from django.contrib import admin
from .models.university import University
from reversion_compare.admin import CompareVersionAdmin
from rex.models.university import University
from rex.models.country import Country
admin.site.register(University, CompareVersionAdmin)
admin.site.register(Country)
"ISO 3166-1 alpha-2","Pays"
"AF","Afghanistan"
"AL","Albanie"
"AQ","Antarctique"
"DZ","Algérie"
"AS","Samoa Américaines"
"AD","Andorre"
"AO","Angola"
"AG","Antigua-et-Barbuda"
"AZ","Azerbaïdjan"
"AR","Argentine"
"AU","Australie"
"AT","Autriche"
"BS","Bahamas"
"BH","Bahreïn"
"BD","Bangladesh"
"AM","Arménie"
"BB","Barbade"
"BE","Belgique"
"BM","Bermudes"
"BT","Bhoutan"
"BO","Bolivie"
"BA","Bosnie-Herzégovine"
"BW","Botswana"
"BV","Île Bouvet"
"BR","Brésil"
"BZ","Belize"
"IO","Territoire Britannique de l'Océan Indien"
"SB","Îles Salomon"
"VG","Îles Vierges Britanniques"
"BN","Brunéi Darussalam"
"BG","Bulgarie"
"MM","Myanmar"
"BI","Burundi"
"BY","Bélarus"
"KH","Cambodge"
"CM","Cameroun"
"CA","Canada"
"CV","Cap-vert"
"KY","Îles Caïmanes"
"CF","République Centrafricaine"
"LK","Sri Lanka"
"TD","Tchad"
"CL","Chili"
"CN","Chine"
"TW","Taïwan"
"CX","Île Christmas"
"CC","Îles Cocos (Keeling)"
"CO","Colombie"
"KM","Comores"
"YT","Mayotte"
"CG","République du Congo"
"CD","République Démocratique du Congo"
"CK","Îles Cook"
"CR","Costa Rica"
"HR","Croatie"
"CU","Cuba"
"CY","Chypre"
"CZ","République Tchèque"
"BJ","Bénin"
"DK","Danemark"
"DM","Dominique"
"DO","République Dominicaine"
"EC","Équateur"
"SV","El Salvador"
"GQ","Guinée Équatoriale"
"ET","Éthiopie"
"ER","Érythrée"
"EE","Estonie"
"FO","Îles Féroé"
"FK","Îles (malvinas) Falkland"
"GS","Géorgie du Sud et les Îles Sandwich du Sud"
"FJ","Fidji"
"FI","Finlande"
"AX","Îles Åland"
"FR","France"
"GF","Guyane Française"
"PF","Polynésie Française"
"TF","Terres Australes Françaises"
"DJ","Djibouti"
"GA","Gabon"
"GE","Géorgie"
"GM","Gambie"
"PS","Territoire Palestinien Occupé"
"DE","Allemagne"
"GH","Ghana"
"GI","Gibraltar"
"KI","Kiribati"
"GR","Grèce"
"GL","Groenland"
"GD","Grenade"
"GP","Guadeloupe"
"GU","Guam"
"GT","Guatemala"
"GN","Guinée"
"GY","Guyana"
"HT","Haïti"
"HM","Îles Heard et Mcdonald"
"VA","Saint-Siège (état de la Cité du Vatican)"
"HN","Honduras"
"HK","Hong-Kong"
"HU","Hongrie"
"IS","Islande"
"IN","Inde"
"ID","Indonésie"
"IR","République Islamique d'Iran"
"IQ","Iraq"
"IE","Irlande"
"IL","Israël"
"IT","Italie"
"CI","Côte d'Ivoire"
"JM","Jamaïque"
"JP","Japon"
"KZ","Kazakhstan"
"JO","Jordanie"
"KE","Kenya"
"KP","République Populaire Démocratique de Corée"
"KR","République de Corée"
"KW","Koweït"
"KG","Kirghizistan"
"LA","République Démocratique Populaire Lao"
"LB","Liban"
"LS","Lesotho"
"LV","Lettonie"
"LR","Libéria"
"LY","Jamahiriya Arabe Libyenne"
"LI","Liechtenstein"
"LT","Lituanie"
"LU","Luxembourg"
"MO","Macao"
"MG","Madagascar"
"MW","Malawi"
"MY","Malaisie"
"MV","Maldives"
"ML","Mali"
"MT","Malte"
"MQ","Martinique"
"MR","Mauritanie"
"MU","Maurice"
"MX","Mexique"
"MC","Monaco"
"MN","Mongolie"
"MD","République de Moldova"
"MS","Montserrat"
"MA","Maroc"
"MZ","Mozambique"
"OM","Oman"
"NA","Namibie"
"NR","Nauru"
"NP","Népal"
"NL","Pays-Bas"
"AN","Antilles Néerlandaises"
"AW","Aruba"
"NC","Nouvelle-Calédonie"
"VU","Vanuatu"
"NZ","Nouvelle-Zélande"
"NI","Nicaragua"
"NE","Niger"
"NG","Nigéria"
"NU","Niué"
"NF","Île Norfolk"
"NO","Norvège"
"MP","Îles Mariannes du Nord"
"UM","Îles Mineures Éloignées des États-Unis"
"FM","États Fédérés de Micronésie"
"MH","Îles Marshall"
"PW","Palaos"
"PK","Pakistan"
"PA","Panama"
"PG","Papouasie-Nouvelle-Guinée"
"PY","Paraguay"
"PE","Pérou"
"PH","Philippines"
"PN","Pitcairn"
"PL","Pologne"
"PT","Portugal"
"GW","Guinée-Bissau"
"TL","Timor-Leste"
"PR","Porto Rico"
"QA","Qatar"
"RE","Réunion"
"RO","Roumanie"
"RU","Fédération de Russie"
"RW","Rwanda"
"SH","Sainte-Hélène"
"KN","Saint-Kitts-et-Nevis"
"AI","Anguilla"
"LC","Sainte-Lucie"
"PM","Saint-Pierre-et-Miquelon"
"VC","Saint-Vincent-et-les Grenadines"
"SM","Saint-Marin"
"ST","Sao Tomé-et-Principe"
"SA","Arabie Saoudite"
"SN","Sénégal"
"SC","Seychelles"
"SL","Sierra Leone"
"SG","Singapour"
"SK","Slovaquie"
"VN","Viet Nam"
"SI","Slovénie"
"SO","Somalie"
"ZA","Afrique du Sud"
"ZW","Zimbabwe"
"ES","Espagne"
"EH","Sahara Occidental"
"SD","Soudan"
"SR","Suriname"
"SJ","Svalbard etÎle Jan Mayen"
"SZ","Swaziland"
"SE","Suède"
"CH","Suisse"
"SY","République Arabe Syrienne"
"TJ","Tadjikistan"
"TH","Thaïlande"
"TG","Togo"
"TK","Tokelau"
"TO","Tonga"
"TT","Trinité-et-Tobago"
"AE","Émirats Arabes Unis"
"TN","Tunisie"
"TR","Turquie"
"TM","Turkménistan"
"TC","Îles Turks et Caïques"
"TV","Tuvalu"
"UG","Ouganda"
"UA","Ukraine"
"MK","L'ex-République Yougoslave de Macédoine"
"EG","Égypte"
"GB","Royaume-Uni"
"IM","Île de Man"
"TZ","République-Unie de Tanzanie"
"US","États-Unis"
"VI","Îles Vierges des États-Unis"
"BF","Burkina Faso"
"UY","Uruguay"
"UZ","Ouzbékistan"
"VE","Venezuela"
"WF","Wallis et Futuna"
"WS","Samoa"
"YE","Yémen"
"CS","Serbie-et-Monténégro"
"ZM","Zambie"
# Generated by Django 2.0.3 on 2018-04-01 14:57
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('rex', '0004_auto_20180401_1550'),
]
operations = [
migrations.CreateModel(
name='Country',
fields=[
('name', models.CharField(max_length=200)),
('iso_code', models.CharField(max_length=2, primary_key=True, serialize=False)),
],
),
]
# Generated by Django 2.0.3 on 2018-04-01 15:50
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('rex', '0005_country'),
]
operations = [
migrations.AddField(
model_name='university',
name='country',
field=models.ForeignKey(default='FR', on_delete='CASCADE', to='rex.Country'),
preserve_default=False,
),
]
# Generated by Django 2.0.3 on 2018-04-01 16:06
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('rex', '0006_university_country'),
]
operations = [
migrations.AlterField(
model_name='university',
name='localization_lat',
field=models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-85.05112878), django.core.validators.MaxValueValidator(85.05112878)]),
),
migrations.AlterField(
model_name='university',
name='localization_lon',
field=models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-180), django.core.validators.MaxValueValidator(180)]),
),
]
from .country import country
from .university import university
\ No newline at end of file
from django.db import models
class Country(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
name = models.CharField(max_length=200)
\ No newline at end of file
name = models.CharField(max_length=200)
iso_code = models.CharField(primary_key = True, max_length=2)
\ No newline at end of file
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from rex.utils.friendly_path import friendly_path
from rex.models.country import Country
import reverse_geocoder as rg
path_and_rename = friendly_path("uploads/universities/logos/",'name')
class University(models.Model):
"""
Model storing information about universities
"""
name = models.CharField(max_length=200)
acronym = models.CharField(max_length=20)
localization_lat = models.DecimalField(max_digits=10, decimal_places=6)
localization_lon = models.DecimalField(max_digits=10, decimal_places=6)
localization_lat = models.DecimalField(
max_digits=10,
decimal_places=6,
validators=[MinValueValidator(-85.05112878), MaxValueValidator(85.05112878)]
)
localization_lon = models.DecimalField(
max_digits=10,
decimal_places=6,
validators=[MinValueValidator(-180), MaxValueValidator(180)]
)
country = models.ForeignKey(Country, on_delete='CASCADE')
logo = models.ImageField(upload_to=path_and_rename, max_length=250)
def localization(self):
return {'lat':self.localization_lat, 'lon':self.localization_lon}
\ No newline at end of file
return {'lat':self.localization_lat, 'lon':self.localization_lon}
def save(self, *args, **kwargs):
"""
Override traditional save function to make sure that
the Country is coherent with given lat / lon
"""
coord = (self.localization_lat, self.localization_lon)
res = rg.search(coord, verbose=False)
self.country = Country.objects.get(pk=res[0]['cc'])
super(University, self).save(*args, **kwargs)
\ No newline at end of file
#!/usr/bin/env python3
"""
Script to insert the country data in the database
IT HAS TO BE RUN INSIDE ./manage.py shell
"""
import csv
import os
from rex.models.country import Country
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")
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()
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