insert_country.py 631 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/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()