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 09eb443
Show file tree
Hide file tree
Showing 5 changed files with 63 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
2 changes: 2 additions & 0 deletions ynr/apps/people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ def get_value_html(self):

if self.value_type == "twitter_username":
url = format_html("https://twitter.com/{}", self.value)
if self.value_type == "instagram_url":
url = format_html("https://instagram.com/{}", self.value)

if self.value.startswith("http"):
url = format_html("{}", self.value)
Expand Down
18 changes: 18 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,24 @@ 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, 302)
self.assertEqual(
PersonIdentifier.objects.get().value,
"blah",
)

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

def test_mastodon_bad_url(self):
# submit a username missing the `@` symbol
resp = self._submit_mastodon_values("https://mastodon.social/joe")
Expand Down
17 changes: 17 additions & 0 deletions ynr/apps/people/tests/test_person_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ def test_get_value_html_twitter(self):
# Test the value type HTML
self.assertEqual(pi.get_value_type_html, "Twitter")

def test_get_value_html_instagram(self):
pi = PersonIdentifier.objects.create(
person=self.person,
value="democlub",
value_type="instagram_username",
internal_identifier="2324",
)

# Test the value HTML
self.assertEqual(
pi.get_value_html,
"""<a href="https://instagram.com/democlub" rel="nofollow">democlub</a>""",
)

# Test the value type HTML
self.assertEqual(pi.get_value_type_html, "Instagram")

def test_get_value_html_mastodon(self):
pi = PersonIdentifier.objects.create(
person=self.person,
Expand Down

0 comments on commit 09eb443

Please sign in to comment.