Skip to content

Fix slugify reporter variations #5997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cl/citations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def slugify_reporter(reporter: str) -> str:
slugify(f"{reporter} {REPORTERS[reporter][0]['cite_type']}")
)

# final fallback check for known variations
if reporter in VARIATIONS_ONLY and len(VARIATIONS_ONLY[reporter]) == 1:
return slugify(VARIATIONS_ONLY[reporter][0])

return slug


Expand Down
81 changes: 81 additions & 0 deletions cl/opinion_page/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,87 @@ async def test_show_error_for_non_opinion_citations(self):
self.assertIn("No Citations Detected", r.content.decode())
self.assertEqual(r.status_code, HTTPStatus.BAD_REQUEST)

async def test_disambiguated_reporter_variants_redirect_properly(self):
"""Can we resolve correctly some reporter variants with collisions to slug?"""

test_pairs = [
("Vr.", "vroom"),
("V.R.", "vt"),
("Black Rep.", "black"),
("Black. Rep.", "blackf"),
("Cal. App. 2d Supp", "cal-app-2d"),
("Cal. App. 2d Supp.", "cal-app-supp-2d"),
("CLR", "conn-l-rptr"),
("Cl.R.", "cl-ch"),
("Dec. Commr. Pat.", "dec-com-pat"),
("Dec. Comm'r Pat.", "dec-commr-pat"),
("Hayw. & H.", "hayw-hdc"),
("Hayw.& H.", "hay-haz"),
("Johns.(N.Y.)", "johns-ch"),
("Johns.N.Y.", "johns"),
("Mt.", "mont"),
("mt", "mt"),
("Pa.C.", "pa-commw"),
("Pac.", "p"),
("Sc.", "scam"),
]

for variation, expected_slug in test_pairs:
with self.subTest(variation=variation):
r = await self.async_client.get(
reverse(
"citation_redirector",
kwargs={"reporter": variation},
),
follow=True,
)
if r.redirect_chain:
# Get path from the redirection from string to slug reporter
path = r.redirect_chain[-1][0]
else:
# No redirect, mt is a variation but matches its slug
path = r.asgi_request.path
expected_path = f"/c/{expected_slug}/"
self.assertEqual(
expected_path,
path,
msg=f"Expected path: {expected_path} is different from the obtained path: {path}",
)

async def test_too_ambiguous_reporter_variations(self):
"""Some abbreviations are too ambiguous to resolve safely"""

test_pairs = [
("B.R.", "br"),
("BR", "br"),
("Wash.", "wash"),
("WASH", "wash"),
("HOW", "how"),
("How.", "how"),
("OKla.", "okla"),
("Okla.", "okla"),
("S.C.", "sc"),
]

for variation, expected_slug in test_pairs:
with self.subTest(variation=variation):
r = await self.async_client.get(
reverse(
"citation_redirector",
kwargs={"reporter": variation},
),
follow=True,
)
# Get path from the redirection from string to slug reporter
path = r.redirect_chain[-1][0] if r.redirect_chain else None

expected_path = f"/c/{expected_slug}/"
self.assertEqual(
expected_path,
path,
msg=f"Expected path: {expected_path} is different from the obtained path: {path}",
)


class ViewRecapDocketTest(TestCase):
@classmethod
Expand Down