Skip to content

Commit

Permalink
Validate Linkedin urls
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed Jun 28, 2024
1 parent 22622a5 commit 463a57c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ynr/apps/people/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
StrippedCharField,
)
from people.helpers import (
clean_linkedin_url,
clean_mastodon_username,
clean_twitter_username,
clean_wikidata_id,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions ynr/apps/people/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -120,6 +121,12 @@ def clean_twitter_username(username):
return username


def clean_linkedin_url(url):
if not re.match(r"^https?://(www\.)?linkedin\.com/in/[\w-]+/?$", url):
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)
Expand Down
8 changes: 8 additions & 0 deletions ynr/apps/people/tests/test_person_form_identifier_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 463a57c

Please sign in to comment.