Skip to content

Commit e45a70b

Browse files
Clean and standardise instagram entries
1 parent 22622a5 commit e45a70b

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

ynr/apps/people/forms/forms.py

Lines changed: 12 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_instagram_username,
2223
clean_mastodon_username,
2324
clean_twitter_username,
2425
clean_wikidata_id,
@@ -116,6 +117,17 @@ def clean(self):
116117
self.add_error(None, e)
117118
return self.cleaned_data
118119

120+
def clean_instagram_url(self, username):
121+
if self.instance.value != username:
122+
self.instance.internal_identifier = None
123+
if self.instance.internal_identifier:
124+
return username
125+
try:
126+
return clean_instagram_username(username)
127+
except ValueError as e:
128+
raise ValidationError(e)
129+
return username
130+
119131
def clean_twitter_username(self, username):
120132
if self.instance.value != username:
121133
self.instance.internal_identifier = None

ynr/apps/people/helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ def clean_twitter_username(username):
120120
return username
121121

122122

123+
def clean_instagram_username(username):
124+
# Remove any URL bits around it:
125+
username = username.strip()
126+
m = re.search(r"^.*(instagram.com|x.com)/(\@?)(\w+)", username)
127+
if m:
128+
username = m.group(3)
129+
# If there's a leading '@', strip that off:
130+
username = re.sub(r"^@", "", username)
131+
if not re.search(r"^\w*$", username):
132+
message = "The Instagram username must only consist of alphanumeric characters or underscore"
133+
raise ValueError(message)
134+
return username
135+
136+
123137
def clean_wikidata_id(identifier):
124138
identifier = identifier.strip().lower()
125139
m = re.search(r"^.*wikidata.org/(wiki|entity)/(\w+)", identifier)

ynr/apps/people/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def get_value_html(self):
169169

170170
if self.value_type == "twitter_username":
171171
url = format_html("https://twitter.com/{}", self.value)
172+
if self.value_type == "instagram_url":
173+
url = format_html("https://instagram.com/{}", self.value)
172174

173175
if self.value.startswith("http"):
174176
url = format_html("{}", self.value)

ynr/apps/people/tests/test_person_form_identifier_crud.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,24 @@ def test_twitter_full_url(self):
185185
PersonIdentifier.objects.get().value, "madeuptwitteraccount"
186186
)
187187

188+
def test_clean_instagram_url(self):
189+
resp = self._submit_values("@blah", "instagram_url")
190+
self.assertEqual(resp.status_code, 302)
191+
self.assertEqual(
192+
PersonIdentifier.objects.get().value,
193+
"blah",
194+
)
195+
196+
def test_instagram_full_url(self):
197+
resp = self._submit_values(
198+
"http://www.instagram.com/blah", "instagram_url"
199+
)
200+
self.assertEqual(resp.status_code, 302)
201+
self.assertEqual(
202+
PersonIdentifier.objects.get().value,
203+
"blah",
204+
)
205+
188206
def test_mastodon_bad_url(self):
189207
# submit a username missing the `@` symbol
190208
resp = self._submit_mastodon_values("https://mastodon.social/joe")

ynr/apps/people/tests/test_person_identifiers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ def test_get_value_html_twitter(self):
3333
# Test the value type HTML
3434
self.assertEqual(pi.get_value_type_html, "Twitter")
3535

36+
def test_get_value_html_instagram(self):
37+
pi = PersonIdentifier.objects.create(
38+
person=self.person,
39+
value="democlub",
40+
value_type="instagram_url",
41+
internal_identifier="2324",
42+
)
43+
44+
# Test the value HTML
45+
self.assertEqual(
46+
pi.get_value_html,
47+
"""<a href="https://instagram.com/democlub" rel="nofollow">democlub</a>""",
48+
)
49+
50+
# Test the value type HTML
51+
self.assertEqual(pi.get_value_type_html, "Instagram")
52+
3653
def test_get_value_html_mastodon(self):
3754
pi = PersonIdentifier.objects.create(
3855
person=self.person,

0 commit comments

Comments
 (0)