Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync EveryPolitician UUID to Person model #2331

Merged
merged 2 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from everypolitician import EveryPolitician

from django.core.management.base import BaseCommand

from pombola.core.models import Person


class Command(BaseCommand):
help = "Sync EveryPolitician UUID to Person's identifiers array"

def add_arguments(self, parser):
parser.add_argument('everypolitician_countries_json_git_ref',
default='master', nargs='?',
help="A git ref from the everypolitician-data repo")

def handle(self, **options):
verbose_level = options['verbosity']

url_template = ('https://cdn.rawgit.com/everypolitician/everypolitician-data/'
'{git_ref}/countries.json')

url = url_template.format(git_ref=options['everypolitician_countries_json_git_ref'])

ep = EveryPolitician(countries_json_url=url)
south_africa_assembly = ep.country('South-Africa').legislature('Assembly').popolo()

id_lookup = {}
for popolo_person in south_africa_assembly.persons:
id_lookup[popolo_person.identifier_value('peoples_assembly')] = popolo_person.id

error_msg = u"No EveryPolitician UUID found for {0.id} {0.name} https://www.pa.org.za/person/{0.slug}/\n"
for person in Person.objects.filter(hidden=False):
uuid = id_lookup.get(str(person.id))
if uuid is None:
verbose_level > 1 and self.stderr.write(error_msg.format(person))
continue
identifier, created = person.identifiers.get_or_create(
scheme='everypolitician',
identifier=uuid,
)
if verbose_level > 0:
if created:
msg = u"Created new identifier for {name}: {identifier}"
else:
msg = u"Existing identifier found for {name}: {identifier}"
self.stdout.write(msg.format(name=person.name, identifier=identifier.identifier))
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,7 @@ gunicorn==19.4.5
whitenoise==2.0.6

cffi

# everypolitician packages
everypolitician==0.0.13
everypolitician-popolo==0.0.11