-
Notifications
You must be signed in to change notification settings - Fork 27
Person identifier cleaning #2433
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e020730
Validate Linkedin urls
VirginiaDooley 5c10852
Clean and standardise instagram entries
VirginiaDooley 1468e67
Management command to check and update invalid links
VirginiaDooley 6f6d621
Teach CandidateBot to remove fields
symroe 6327f12
DRY up submitting values logic
symroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
ynr/apps/people/management/commands/reformat_person_identifiers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| from candidatebot.helpers import CandidateBot | ||
| from django.core.exceptions import ValidationError | ||
| from django.core.management.base import BaseCommand | ||
| from django.db import transaction | ||
| from people.helpers import ( | ||
| clean_instagram_url, | ||
| clean_linkedin_url, | ||
| clean_mastodon_username, | ||
| clean_twitter_username, | ||
| clean_wikidata_id, | ||
| ) | ||
| from people.models import PersonIdentifier | ||
|
|
||
| # Add to this list when we have dedicated clean_* functions for an value type | ||
| supported_identifiers = { | ||
| "instagram_url": clean_instagram_url, | ||
| "linkedin_url": clean_linkedin_url, | ||
| "twitter_username": clean_twitter_username, | ||
| "mastodon_username": clean_mastodon_username, | ||
| "wikidata_id": clean_wikidata_id, | ||
| } | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = f"""Checks and updates links in the PersonIdentifier field. | ||
|
|
||
| Currently the fields supported are {", ".join(supported_identifiers)}. | ||
| """ | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "--update", | ||
| help="Write changes to IDs that have been cleaned", | ||
| action="store_true", | ||
| ) | ||
| parser.add_argument( | ||
| "--remove", | ||
| help="Remove invalid IDs that can't be cleaned", | ||
| action="store_true", | ||
| ) | ||
|
|
||
| @transaction.atomic() | ||
| def handle(self, *args, **options): | ||
| """ | ||
| Iterate over all PersonIdentifier objects and reformat urls as needed. | ||
| """ | ||
|
|
||
| if not options["update"] or not options["remove"]: | ||
| self.stdout.write("Not modifying data, just reporting on changes.") | ||
|
|
||
| person_identifiers = PersonIdentifier.objects.filter( | ||
| value_type__in=supported_identifiers.keys() | ||
| ) | ||
| for identifier in person_identifiers: | ||
| initial_value = identifier.value | ||
| try: | ||
| cleaned_value = supported_identifiers[identifier.value_type]( | ||
| initial_value | ||
| ) | ||
| if initial_value != cleaned_value: | ||
| self.change_identifier( | ||
| identifier, cleaned_value, save=options["update"] | ||
| ) | ||
|
|
||
| except (ValueError, ValidationError) as exception: | ||
| self.unclean_identifier( | ||
| identifier, exception, remove=options["remove"] | ||
| ) | ||
|
|
||
| def change_identifier(self, identifier, cleaned_value, save=False): | ||
| self.stdout.write( | ||
| f"Changing person {identifier.person_id}: {identifier.value} to {cleaned_value}" | ||
| ) | ||
| if save: | ||
| bot = CandidateBot(identifier.person_id, update=True) | ||
| bot.edit_field(identifier.value_type, cleaned_value) | ||
| bot.save(source="Auto-cleaning URLs") | ||
|
|
||
| def unclean_identifier(self, identifier, exception, remove=False): | ||
| self.stdout.write( | ||
| f"ERROR: {identifier.person_id}: {identifier.value}: {exception}" | ||
| ) | ||
| if remove: | ||
| bot = CandidateBot(identifier.person_id, update=True) | ||
| bot.delete_field(identifier.value_type) | ||
| bot.save(source="Auto removing invalid IDs") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.