Skip to content

Commit 463a57c

Browse files
Validate Linkedin urls
1 parent 22622a5 commit 463a57c

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

ynr/apps/people/forms/forms.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
StrippedCharField,
2020
)
2121
from people.helpers import (
22+
clean_linkedin_url,
2223
clean_mastodon_username,
2324
clean_twitter_username,
2425
clean_wikidata_id,
@@ -128,6 +129,18 @@ def clean_twitter_username(self, username):
128129
except ValueError as e:
129130
raise ValidationError(e)
130131

132+
def clean_linkedin_url(self, url):
133+
if self.instance.value != url:
134+
self.instance.internal_identifier = None
135+
136+
if self.instance.internal_identifier:
137+
return url
138+
139+
try:
140+
clean_linkedin_url(url)
141+
except ValueError as e:
142+
raise ValidationError(e)
143+
131144
def clean_mastodon_username(self, username):
132145
if self.instance.value != username:
133146
self.instance.internal_identifier = None

ynr/apps/people/helpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
)
99
from dateutil import parser
1010
from django.conf import settings
11+
from django.core.exceptions import ValidationError
1112
from django_date_extensions.fields import ApproximateDate
1213

1314

@@ -120,6 +121,12 @@ def clean_twitter_username(username):
120121
return username
121122

122123

124+
def clean_linkedin_url(url):
125+
if not re.match(r"^https?://(www\.)?linkedin\.com/in/[\w-]+/?$", url):
126+
raise ValidationError("Please enter a valid LinkedIn URL.")
127+
return url
128+
129+
123130
def clean_wikidata_id(identifier):
124131
identifier = identifier.strip().lower()
125132
m = re.search(r"^.*wikidata.org/(wiki|entity)/(\w+)", identifier)

ynr/apps/people/tests/test_person_form_identifier_crud.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ def test_mastodon_full_url(self):
209209
qs = PersonIdentifier.objects.all()
210210
self.assertEqual(qs[0].value, "joe")
211211

212+
def test_bad_linkedin_url(self):
213+
resp = self._submit_values("http://example.com/@blah", "linkedin_url")
214+
form = resp.context["identifiers_formset"]
215+
self.assertFalse(form.is_valid())
216+
self.assertEqual(
217+
form[0].non_field_errors(), ["Please enter a valid LinkedIn URL."]
218+
)
219+
212220
def test_bad_email_address(self):
213221
resp = self._submit_values("whoops", "email")
214222
form = resp.context["identifiers_formset"]

0 commit comments

Comments
 (0)