Skip to content

Commit

Permalink
Clean and standardise instagram entries
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed Jun 26, 2024
1 parent 22622a5 commit b63527b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
12 changes: 12 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_instagram_username,
clean_mastodon_username,
clean_twitter_username,
clean_wikidata_id,
Expand Down Expand Up @@ -116,6 +117,17 @@ def clean(self):
self.add_error(None, e)
return self.cleaned_data

def clean_instagram_url(self, username):
if self.instance.value != username:
self.instance.internal_identifier = None
if self.instance.internal_identifier:
return username
try:
return clean_instagram_username(username)
except ValueError as e:
raise ValidationError(e)
return username

def clean_twitter_username(self, username):
if self.instance.value != username:
self.instance.internal_identifier = None
Expand Down
14 changes: 14 additions & 0 deletions ynr/apps/people/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ def clean_twitter_username(username):
return username


def clean_instagram_username(username):
# Remove any URL bits around it:
username = username.strip()
m = re.search(r"^.*(instagram.com|x.com)/(\@?)(\w+)", username)
if m:
username = m.group(3)
# If there's a leading '@', strip that off:
username = re.sub(r"^@", "", username)
if not re.search(r"^\w*$", username):
message = "The Instagram username must only consist of alphanumeric characters or underscore"
raise ValueError(message)
return username


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 @@ -185,6 +185,14 @@ def test_twitter_full_url(self):
PersonIdentifier.objects.get().value, "madeuptwitteraccount"
)

def test_clean_instagram_url(self):
resp = self._submit_values("blah", "instagram_url")
self.assertEqual(resp.status_code, 200)
self.assertEqual(
PersonIdentifier.objects.get().value,
"http://www.instagram.com/blah",
)

def test_mastodon_bad_url(self):
# submit a username missing the `@` symbol
resp = self._submit_mastodon_values("https://mastodon.social/joe")
Expand Down

0 comments on commit b63527b

Please sign in to comment.