Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
piphone
campaign
Commits
27471a73
Commit
27471a73
authored
Jan 14, 2015
by
Mindiell
Browse files
First function to import meps from compotista - still under development, but functionnal
parent
6f3241fb
Changes
3
Show whitespace changes
Inline
Side-by-side
picampaign/contact/management/__init__.py
0 → 100644
View file @
27471a73
picampaign/contact/management/commands/__init__.py
0 → 100644
View file @
27471a73
picampaign/contact/management/commands/import_meps_from_compotista.py
0 → 100644
View file @
27471a73
# encoding: utf-8
#import os
from
datetime
import
datetime
import
json
from
os.path
import
exists
,
join
import
urllib
from
django.core.management.base
import
BaseCommand
from
django.db
import
transaction
from
picampaign.contact.models
import
Contact
JSON_DUMP_LOCALIZATION
=
join
(
'/tmp'
,
'latest_meps.json'
)
class
Command
(
BaseCommand
):
help
=
'Update the eurodeputies data by pulling it from compotista'
def
handle
(
self
,
*
args
,
**
options
):
if
not
exists
(
JSON_DUMP_LOCALIZATION
):
print
"download lastest data dump of meps from compotista"
urllib
.
urlretrieve
(
'http://compotista.mm.staz.be/latest/'
,
JSON_DUMP_LOCALIZATION
)
print
"load json"
meps
=
json
.
load
(
open
(
JSON_DUMP_LOCALIZATION
,
"r"
))
# This should not be used, since old contacts should stay in database
print
"cleaning Contacts"
Contact
.
objects
.
all
().
delete
()
print
"adding Contacts"
added
=
0
for
mep
in
meps
:
insert_mep
=
False
for
mandate
in
mep
[
'mandates'
]:
if
datetime
.
strptime
(
mandate
[
'begin_date'
],
'%Y-%m-%d'
)
<
datetime
.
today
()
<
datetime
.
strptime
(
mandate
[
'end_date'
],
'%Y-%m-%d'
):
insert_mep
=
True
if
insert_mep
:
first_name
=
mep
[
'personal'
][
'first_name'
]
last_name
=
mep
[
'personal'
][
'last_name'
]
if
mep
[
'contact'
][
'phones'
]
!=
[]:
phone
=
mep
[
'contact'
][
'phones'
][
0
][
'phone'
]
else
:
phone
=
''
if
mep
[
'contact'
][
'emails'
]
!=
[]:
mail
=
mep
[
'contact'
][
'emails'
][
0
][
'email'
]
else
:
mail
=
''
if
mep
[
'contact'
][
'websites'
]
!=
[]:
for
website
in
mep
[
'contact'
][
'websites'
]:
if
'twitter'
in
website
[
'website'
]:
twitter
=
''
.
join
((
'@'
,
website
[
'website'
].
split
(
'/'
)[
-
1
]))
break
else
:
twitter
=
''
if
phone
!=
''
:
contact
=
Contact
.
objects
.
create
(
first_name
=
first_name
,
last_name
=
last_name
,
phone
=
phone
,
mail
=
mail
,
twitter
=
twitter
)
added
+=
1
print
'%d - %s %s'
%
(
added
,
first_name
,
last_name
)
if
added
>
50
:
return
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment