-
Notifications
You must be signed in to change notification settings - Fork 27
Remove inactive person links #2364
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
Open
VirginiaDooley
wants to merge
2
commits into
master
Choose a base branch
from
hotfix/remove-inactive-links
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
80 changes: 80 additions & 0 deletions
80
ynr/apps/candidatebot/management/commands/candidatebot_remove_inactive_person_links.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,80 @@ | ||
| from typing import List | ||
| from urllib.parse import urlparse | ||
|
|
||
| import requests | ||
| from candidatebot.helpers import CandidateBot | ||
| from django.core.management.base import BaseCommand | ||
| from people.models import Person | ||
| from popolo.models import Membership | ||
|
|
||
|
|
||
| def get_domain(url): | ||
| parsed_url = urlparse(url) | ||
| return parsed_url.netloc | ||
|
|
||
|
|
||
| def is_facebook_url(url): | ||
| domain = get_domain(url) | ||
| return "facebook.com" in domain or "fb.com" in domain | ||
Check failureCode scanning / CodeQL Incomplete URL substring sanitization
The string [fb.com](1) may be at an arbitrary position in the sanitized URL.
|
||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| """ | ||
| Test and remove inactive or dead links from Person objects. | ||
| """ | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "--person-id", | ||
| help="Person ID to test", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| """ | ||
| Iterate over all Person objects and check if the | ||
| person identifier urls return a 200 status code. | ||
| """ | ||
| inactive_links: List[List] = [] | ||
| # facebook_url is any url with facebook or fb in the url | ||
| memberships = Membership.objects.filter( | ||
| ballot__election__slug="parl.2024-07-04" | ||
| ) | ||
|
|
||
| people = Person.objects.all().filter(memberships__in=memberships) | ||
| for person in people: | ||
| person_identifiers = person.get_all_identifiers | ||
| person_identifiers = [ | ||
| identifier | ||
| for identifier in person_identifiers | ||
| if identifier.value.startswith("http") | ||
| ] | ||
|
|
||
| if not person_identifiers: | ||
| continue | ||
| for identifier in person_identifiers: | ||
| resp = None | ||
| try: | ||
| resp = requests.get(identifier.value, timeout=2).status_code | ||
| except requests.exceptions.RequestException as e: | ||
| self.stdout.write( | ||
| f"Request exception: {e} for {person.name}" | ||
| ) | ||
| pass | ||
| if resp == 404 and not is_facebook_url(identifier.value): | ||
| self.stdout.write( | ||
| f"Status code: {resp} for {person.name} {identifier.value}" | ||
| ) | ||
| inactive_links.append( | ||
| [ | ||
| str(person.pk), | ||
| person.name, | ||
| identifier.value, | ||
| str(resp), | ||
| ] | ||
| ) | ||
| # delete the identifier from the person identifiers | ||
| bot = CandidateBot(person.pk, ignore_errors=True) | ||
| bot.remove_person_identifier(identifier) | ||
| print( | ||
| f"Candidatebot deleted {identifier.value_type}:{identifier.value} from {person.name}" | ||
| ) | ||
70 changes: 70 additions & 0 deletions
70
ynr/apps/candidates/tests/test_remove_inactive_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,70 @@ | ||
| from unittest import TestCase | ||
|
|
||
| import pytest | ||
| from candidates.models.popolo_extra import Ballot | ||
| from candidates.tests.factories import ( | ||
| ElectionFactory, | ||
| MembershipFactory, | ||
| PostFactory, | ||
| ) | ||
| from django.core.management import call_command | ||
| from parties.tests.factories import PartyFactory | ||
| from people.tests.factories import PersonFactory, PersonIdentifierFactory | ||
|
|
||
|
|
||
| class TestPersonIdentifiers(TestCase): | ||
| def setUp(self): | ||
| self.person = PersonFactory.create() | ||
| # 200 example | ||
| PersonIdentifierFactory.create( | ||
| person=self.person, | ||
| value="https://en.wikipedia.org/wiki/Rishi_Sunak", | ||
| value_type="https://en.wikipedia_url", | ||
| ) | ||
| # 404 example | ||
| PersonIdentifierFactory.create( | ||
| person=self.person, | ||
| value="http://www.conservatives.com/about/our-team/example.com", | ||
| value_type="party_ppc_page_url", | ||
| ) | ||
| post = PostFactory.create(slug="parl.2024-07-04") | ||
|
|
||
| election = ElectionFactory.create( | ||
| slug="parl.2024-07-04", | ||
| election_date="2024-07-04", | ||
| name="2024 General Election", | ||
| ) | ||
| ballot = Ballot.objects.create( | ||
| election=election, post=post, ballot_paper_id="parl.2024-07-04" | ||
| ) | ||
| party = PartyFactory.create() | ||
| MembershipFactory.create( | ||
| person=self.person, | ||
| post=post, | ||
| party=party, | ||
| ballot=ballot, | ||
| ) | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_remove_inactive_person_identifiers(self): | ||
| self.assertEqual(len(self.person.get_all_identifiers), 2) | ||
| self.assertEqual( | ||
| self.person.get_all_identifiers[0].value, | ||
| "https://en.wikipedia.org/wiki/Rishi_Sunak", | ||
| ) | ||
| self.assertEqual( | ||
| self.person.get_all_identifiers[1].value, | ||
| "http://www.conservatives.com/about/our-team/example.com", | ||
| ) | ||
|
|
||
| call_command( | ||
| "candidatebot_remove_inactive_person_links", | ||
| "--person-id", | ||
| self.person.id, | ||
| ) | ||
| self.person.refresh_from_db() | ||
| self.assertEqual(len(self.person.get_all_identifiers), 1) | ||
| self.assertEqual( | ||
| self.person.get_all_identifiers[0].value, | ||
| "https://en.wikipedia.org/wiki/Rishi_Sunak", | ||
| ) |
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.
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization