Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Rex Dri
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julien Jerphanion
Rex Dri
Commits
6c44bb1d
Commit
6c44bb1d
authored
6 years ago
by
Florent Chehab
Browse files
Options
Downloads
Patches
Plain Diff
Insertion through API
parent
a2172e2e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
rex/utils/insert_country.py
+62
-8
62 additions, 8 deletions
rex/utils/insert_country.py
with
62 additions
and
8 deletions
rex/utils/insert_country.py
+
62
−
8
View file @
6c44bb1d
...
...
@@ -6,9 +6,30 @@ Script to insert the country data in the database
IT HAS TO BE RUN INSIDE ./manage.py shell
"""
import
os
import
argparse
import
pandas
as
pd
import
requests
from
rex.models.country
import
Country
,
Region
if
__name__
!=
"
__main__
"
:
print
(
__name__
)
raise
Exception
(
"
Script has to be run directly
"
)
# getting args from command line
parser
=
argparse
.
ArgumentParser
(
description
=
'
Country and Region Insertion
'
)
parser
.
add_argument
(
'
--address
'
,
'
-a
'
,
dest
=
'
address
'
,
action
=
'
store
'
,
default
=
'
http://127.0.0.1:8000/api
'
,
help
=
'
Adresse of the web REST API
'
)
parser
.
add_argument
(
'
--token
'
,
'
-t
'
,
dest
=
'
token
'
,
action
=
'
store
'
,
default
=
'
NO_TOKEN_PROVIDED
'
,
help
=
'
Permanent token to use
'
)
args
=
parser
.
parse_args
()
api_address
=
str
(
args
.
address
)
api_token
=
str
(
args
.
token
)
tmp
=
os
.
path
.
join
(
os
.
path
.
realpath
(
__file__
),
'
../../assets/country.csv
'
)
country_file_loc
=
os
.
path
.
abspath
(
tmp
)
...
...
@@ -21,11 +42,41 @@ data = data.drop(["Least Developed Countries (LDC)",
"
Developed / Developing Countries
"
],
axis
=
1
)
def
make_post
(
address
,
data
):
h
=
{
'
Authorization
'
:
'
Token
'
+
api_token
}
r
=
requests
.
post
(
address
,
headers
=
h
,
data
=
data
)
if
r
.
status_code
is
403
:
raise
Exception
(
"
Authentification failed
"
)
if
not
r
.
ok
:
print
(
r
.
content
)
print
(
data
)
return
r
.
ok
def
save_country
(
name
,
code
,
region
):
a
=
api_address
+
"
/country/
"
if
region
is
not
None
:
region
=
api_address
+
"
/region/
"
+
region
+
'
/
'
data
=
{
'
name
'
:
name
,
'
iso_alpha3_code
'
:
code
,
'
region
'
:
region
}
return
make_post
(
a
,
data
)
def
save_region
(
name
,
code
,
parent
):
a
=
api_address
+
"
/region/
"
if
parent
is
not
None
:
parent
=
a
+
parent
+
'
/
'
data
=
{
'
name
'
:
name
,
'
un_code
'
:
code
,
'
parent
'
:
parent
}
return
make_post
(
a
,
data
)
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
i
in
range
(
len
(
r
)
-
1
):
...
...
@@ -33,20 +84,23 @@ for index, row in data.iterrows():
region_code
=
row
[
r_type
[
0
]]
region_name
=
row
[
r_type
[
1
]]
if
region_code
is
not
''
:
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
=
Region
.
objects
.
get
(
pk
=
parent_code
)
region
=
Region
(
un_code
=
region_code
,
name
=
region_name
,
parent
=
parent
)
region
.
save
()
parent
=
parent_code
save_region
(
region_name
,
region_code
,
parent
)
inserted_regions
.
append
(
region_code
)
else
:
break
if
region_code
is
''
:
region_code
=
None
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
=
Country
(
name
=
c_name
,
iso_alpha3_code
=
c_code
,
region
=
region
)
c
.
save
()
save_country
(
c_name
,
c_code
,
region_code
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment