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

Conversation

chrismytton
Copy link
Member

@chrismytton chrismytton commented Nov 24, 2017

Add a management command add EveryPolitician UUIDs to the Person model.

Blocked waiting for EveryPolitician South Africa import to be fixed

This is currently using the south-africa-update-peoples-assembly branch of everypolitician-data, which has the required UUIDs but is also losing some data, so can't be merged in its current state. So before this can be merged we need to fix the problems described in everypolitician/everypolitician-data#42096.

To work around this problem I've made the git ref that we use to fetch countries.json configurable. This means we can point to the branch that has the data we need for now, then when things are fixed upstream we can use a different ref to point to the fixed data.

I've also split the bit that adds a cronjob for this into a separate pull request #2342 so we can hold off merging that until we've got the data in EveryPolitician up-to-date, but we can still merge this in the meantime.

Fixes #2330

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.02%) to 54.171% when pulling 15739cc on 2330-add-everypolitician-uuid-to-person into 63942cc on master.

@mysociety-pusher mysociety-pusher force-pushed the 2330-add-everypolitician-uuid-to-person branch 2 times, most recently from b941251 to 19be09e Compare November 27, 2017 11:30
@chrismytton chrismytton changed the title [WIP] Sync EveryPolitician UUID to Person model Sync EveryPolitician UUID to Person model Nov 27, 2017
@chrismytton chrismytton changed the title Sync EveryPolitician UUID to Person model [Blocked] Sync EveryPolitician UUID to Person model Nov 27, 2017
@coveralls
Copy link

coveralls commented Nov 27, 2017

Coverage Status

Coverage decreased (-0.04%) to 54.156% when pulling 744fffd on 2330-add-everypolitician-uuid-to-person into a433adc on master.

@chrismytton
Copy link
Member Author

I think it would be good to get this merged ASAP rather than waiting for the EP/DC problems to be resolved. The best way to do this is probably to make the SHA configurable where there's currently a TODO comment in this change:

# TODO: Make this SHA configurable.
url = ('https://cdn.rawgit.com/everypolitician/everypolitician-data/'
'eaa170a15d61b024b49e22527fd1fdf6eba18ee6/countries.json')

Then this script will be usable with whatever EveryPolitician data we want to use.

@mysociety-pusher mysociety-pusher force-pushed the 2330-add-everypolitician-uuid-to-person branch from 4ea415a to 595c611 Compare January 31, 2018 09:51
@chrismytton chrismytton changed the title [Blocked] Sync EveryPolitician UUID to Person model Sync EveryPolitician UUID to Person model Jan 31, 2018
We need this so we can sync the EveryPolitician UUID into the local
database for use with WriteInPublic.
@mysociety-pusher mysociety-pusher force-pushed the 2330-add-everypolitician-uuid-to-person branch from 595c611 to 989f637 Compare January 31, 2018 13:10
@chrismytton
Copy link
Member Author

Have extracted the crontab bits out to #2342 so that the code for the management command can be merged now, and then we can add the cronjob once we're happy with the EP data.

@mysociety-pusher mysociety-pusher force-pushed the 2330-add-everypolitician-uuid-to-person branch from 989f637 to 159156d Compare January 31, 2018 16:40
@chrismytton chrismytton requested a review from mhl January 31, 2018 17:10
Copy link
Contributor

@mhl mhl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left a few minor comments inline. The only other thing that occurs to me is that it might helpful if the argument was optional rather than required, and have it default to master.

# `peoples_assembly` identifier, but this isn't on master because
# South Africa is broken in EveryPolitician at the moment.
#
# TODO: Make this SHA configurable.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these comments can be removed now :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yep!


id_lookup = {}
for popolo_person in south_africa_assembly.persons:
pa_ids = [i for i in popolo_person.identifiers if i['scheme'] == 'peoples_assembly']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use popolo_person.identifier_value('peoples_assembly') to simplify this: https://github.com/everypolitician/everypolitician-popolo-python/blob/master/popolo_data/base.py#L99 (this isn't documented, sorry)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah didn't know about that, thanks, have changed it to use that.

continue
id_lookup[pa_ids[0]['identifier']] = popolo_person.id

error_msg = u"No EveryPolitician UUID found for {} {} https://www.pa.org.za/person/{}/\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is partly just "fun with Python's .format" but, you could make this:

u"No EveryPolitician UUID found for {0.id} {0.name} https://www.pa.org.za/person/{0.slug}/\n"

... and then do error_msg.format(person). It's a tiny thing, but it'd make changing the format string later less prone to error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😮 Wow, didn't know .format could do that!! You're quite right, that's less error prone. Have switched to using that syntax.

for person in Person.objects.filter(hidden=False):
uuid = id_lookup.get(str(person.id))
if uuid is None:
if verbose_level > 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably collapse these two conditions into a single condition - perhaps makes it slightly easier to see that there's only one branch here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The continue on the line below is actually in the top level if statement, then the logging is part of the inner if statement. I've changed things around in 663e8dc so that it's all at a single level of if statement to make things hopefully a bit clearer.



class Command(BaseCommand):
help = 'Sync EveryPolitician UUID to Person.everypolitician_uuid field'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Person.everypolitician_uuid field" - I suspect this is an old comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if created:
msg = u"Created new identifier for {}: {}"
else:
msg = u"Existing identifier found for {}: {}"
Copy link
Contributor

@mhl mhl Feb 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's another very minor thing, but I'd probably make these use {name}: {identifier} and then .format(name=person.name, identifier=identifier.identifier)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@chrismytton
Copy link
Member Author

The only other thing that occurs to me is that it might helpful if the argument was optional rather than required, and have it default to master.

Ah yep, very sensible suggestion! Have done that in e7e2e96.

@chrismytton chrismytton requested a review from mhl February 1, 2018 11:26
@chrismytton
Copy link
Member Author

chrismytton commented Feb 1, 2018

@mhl I've addressed your very helpful comments, would you mind re-reviewing this please?

@mhl
Copy link
Contributor

mhl commented Feb 1, 2018

@chrismytton Looks good 👍 - just one last thing; since this is currently South Africa specific, could you make the filename pombola/core/management/commands/core_sync_everypolitician_uuid.py instead?

@chrismytton
Copy link
Member Author

@chrismytton Looks good 👍 - just one last thing; since this is currently South Africa specific, could you make the filename pombola/core/management/commands/core_sync_everypolitician_uuid.py instead?

I'm guessing that should have been pombola/south_africa/management/commands/south_africa_sync_everypolitician_uuid.py? If so I've done that in 2e63d66.

@mhl
Copy link
Contributor

mhl commented Feb 1, 2018

Yep, that's what I meant, sorry :)

@mysociety-pusher mysociety-pusher force-pushed the 2330-add-everypolitician-uuid-to-person branch from 2e63d66 to 744fffd Compare February 1, 2018 15:39
@mysociety-pusher mysociety-pusher merged commit 744fffd into master Feb 1, 2018
@mysociety-pusher mysociety-pusher deleted the 2330-add-everypolitician-uuid-to-person branch June 7, 2019 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ZA] Sync people's EveryPolitician UUID to the local database
4 participants