diff --git a/ynr/apps/people/forms/forms.py b/ynr/apps/people/forms/forms.py index 16dcca803..e42cf5ee0 100644 --- a/ynr/apps/people/forms/forms.py +++ b/ynr/apps/people/forms/forms.py @@ -19,6 +19,7 @@ StrippedCharField, ) from people.helpers import ( + clean_linkedin_url, clean_mastodon_username, clean_twitter_username, clean_wikidata_id, @@ -128,6 +129,18 @@ def clean_twitter_username(self, username): except ValueError as e: raise ValidationError(e) + def clean_linkedin_url(self, url): + if self.instance.value != url: + self.instance.internal_identifier = None + + if self.instance.internal_identifier: + return url + + try: + clean_linkedin_url(url) + except ValueError as e: + raise ValidationError(e) + def clean_mastodon_username(self, username): if self.instance.value != username: self.instance.internal_identifier = None diff --git a/ynr/apps/people/helpers.py b/ynr/apps/people/helpers.py index a8df99634..30434de55 100644 --- a/ynr/apps/people/helpers.py +++ b/ynr/apps/people/helpers.py @@ -8,6 +8,7 @@ ) from dateutil import parser from django.conf import settings +from django.core.exceptions import ValidationError from django_date_extensions.fields import ApproximateDate @@ -120,6 +121,15 @@ def clean_twitter_username(username): return username +def clean_linkedin_url(url): + parsed_url = urlparse(url) + if parsed_url.netloc != "linkedin.com" or not parsed_url.path.startswith( + "/in/" + ): + raise ValidationError("Please enter a valid LinkedIn URL.") + return url + + def clean_wikidata_id(identifier): identifier = identifier.strip().lower() m = re.search(r"^.*wikidata.org/(wiki|entity)/(\w+)", identifier) diff --git a/ynr/apps/people/tests/test_person_form_identifier_crud.py b/ynr/apps/people/tests/test_person_form_identifier_crud.py index d081f0249..2b0b30d30 100644 --- a/ynr/apps/people/tests/test_person_form_identifier_crud.py +++ b/ynr/apps/people/tests/test_person_form_identifier_crud.py @@ -209,6 +209,14 @@ def test_mastodon_full_url(self): qs = PersonIdentifier.objects.all() self.assertEqual(qs[0].value, "joe") + def test_bad_linkedin_url(self): + resp = self._submit_values("http://example.com/@blah", "linkedin_url") + form = resp.context["identifiers_formset"] + self.assertFalse(form.is_valid()) + self.assertEqual( + form[0].non_field_errors(), ["Please enter a valid LinkedIn URL."] + ) + def test_bad_email_address(self): resp = self._submit_values("whoops", "email") form = resp.context["identifiers_formset"] diff --git a/ynr/apps/ynr_refactoring/settings.py b/ynr/apps/ynr_refactoring/settings.py index 42f89e619..308f01cdf 100644 --- a/ynr/apps/ynr_refactoring/settings.py +++ b/ynr/apps/ynr_refactoring/settings.py @@ -12,7 +12,7 @@ class PersonIdentifierFields(Enum): facebook_personal_url = "Facebook Personal" homepage_url = "Homepage" blog_url = "Blog" - linkedin_url = "Linkedin" + linkedin_url = "LinkedIn URL" party_ppc_page_url = "Party Candidate Page" twitter_username = "Twitter" mastodon_username = "Mastodon"