Skip to content

Commit

Permalink
fix(alerts): Fixed no HL fields in OA alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
albertisfu committed May 22, 2024
1 parent 7260d54 commit a993092
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cl/alerts/templates/alert_email_es.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h2 style="font-size: 2em; font-weight: normal; font-family: inherit; color: #11
<h3 class="alt bottom" style="font-size: 1.5em; font-weight: normal; line-height: 1; font-family: 'Warnock Pro', 'Goudy Old Style','Palatino','Book Antiqua', Georgia, serif; color: #666; border: 0; vertical-align: baseline; font-style: italic; margin: 0; padding: 0;">
<a href="https://www.courtlistener.com{{result.absolute_url}}" style="font-size: 100%; font-weight: inherit; font-family: inherit; color: #009; border: 0; font-style: inherit; padding: 0; text-decoration: none; vertical-align: baseline; margin: 0;">
{{ forloop.counter }}. {{ result|get_highlight:"caseName"|safe }}
({% if result.court_id != 'scotus' %}{{ result|get_highlight:"court_citation_string"|nbsp|safe }}{{ result.court_citation_string|render_string_or_list|nbsp|safe }}&nbsp;{% endif %}{% if type == 'o' %}{{ result.dateFiled|date:"Y" }}{% elif type == 'oa' %}{{ result.dateArgued|date:"Y" }}{% endif %})
({% if result.court_id != 'scotus' %}{{ result|get_highlight:"court_citation_string"|nbsp|safe }}&nbsp;{% endif %}{% if type == 'o' %}{{ result.dateFiled|date:"Y" }}{% elif type == 'oa' %}{{ result.dateArgued|date:"Y" }}{% endif %})
</a>
</h3>
<p style="font-size: 100%; font-weight: inherit; font-family: inherit; border: 0; vertical-align: baseline; font-style: inherit; margin: 0; padding: 0;">
Expand Down
91 changes: 90 additions & 1 deletion cl/alerts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,11 @@ def setUpTestData(cls):
jurisdiction="FB",
citation_string="Bankr. C.D. Cal.",
)
cls.docket = DocketFactory(
court=cls.court_1,
date_argued=now().date(),
docket_number="20-5736",
)
cls.user_profile = UserProfileWithParentsFactory()
NeonMembership.objects.create(
level=NeonMembership.LEGACY, user=cls.user_profile.user
Expand Down Expand Up @@ -1890,6 +1895,12 @@ def setUpTestData(cls):
name="Test Alert OA Monthly",
query="q=Test+OA&type=oa",
)
cls.search_alert_7 = AlertFactory(
user=cls.user_profile.user,
rate=Alert.REAL_TIME,
name="Test Alert OA RT Docket ID",
query=f"q=docket_id:{cls.docket.pk}&type=oa",
)

@classmethod
def tearDownClass(cls):
Expand Down Expand Up @@ -2046,6 +2057,18 @@ def test_send_oa_search_alert_webhooks(self, mock_abort_audio):
content["payload"]["results"][0]["caseName"],
rt_oral_argument.case_name,
)
self.assertEqual(
content["payload"]["results"][0]["docketNumber"],
rt_oral_argument.docket.docket_number,
)
self.assertEqual(
content["payload"]["results"][0]["snippet"],
rt_oral_argument.transcript,
)
self.assertEqual(
content["payload"]["results"][0]["judge"],
rt_oral_argument.judges,
)
self.assertEqual(
content["payload"]["results"][0]["court"],
rt_oral_argument.docket.court.full_name,
Expand All @@ -2054,8 +2077,74 @@ def test_send_oa_search_alert_webhooks(self, mock_abort_audio):
content["payload"]["results"][0]["source"],
rt_oral_argument.source,
)

# Confirm no HL fields are properly displayed.
with mock.patch(
"cl.api.webhooks.requests.post",
side_effect=lambda *args, **kwargs: MockResponse(
200, mock_raw=True
),
):
mock_date = now().replace(day=1, hour=5)
with time_machine.travel(
mock_date, tick=False
), self.captureOnCommitCallbacks(execute=True):
# When the Audio object is created it should trigger an alert.
transcript_response = {
"response": {
"results": [
{
"alternatives": [
{
"transcript": "This a different transcript.",
"confidence": 0.85,
},
]
},
]
}
}
json_transcript = json.dumps(transcript_response)
rt_oral_argument_2 = AudioWithParentsFactory.create(
case_name="No HL OA Alert",
docket=self.docket,
stt_status=Audio.STT_COMPLETE,
judges="George Smith",
stt_google_response=json_transcript,
)

self.assertEqual(len(mail.outbox), 3, msg="Wrong number of emails.")
text_content = mail.outbox[2].body

# Confirm all fields are displayed in the plain text version.
self.assertIn(rt_oral_argument_2.case_name, text_content)
self.assertIn(rt_oral_argument_2.judges, text_content)
self.assertIn(rt_oral_argument_2.docket.docket_number, text_content)
self.assertIn(rt_oral_argument_2.transcript, text_content)
self.assertIn(
rt_oral_argument_2.docket.court.citation_string, text_content
)

# Extract HTML version.
html_content = None
for content, content_type in mail.outbox[2].alternatives:
if content_type == "text/html":
html_content = content
break

# Confirm all fields are displayed in the HTML version.
self.assertIn(rt_oral_argument_2.case_name, html_content)
self.assertIn(rt_oral_argument_2.judges, html_content)
self.assertIn(rt_oral_argument_2.docket.docket_number, html_content)
self.assertIn(rt_oral_argument_2.transcript, html_content)
self.assertIn(
rt_oral_argument_2.docket.court.citation_string,
html_content.replace("&nbsp;", " "),
)

webhook_events.delete()
rt_oral_argument.delete()
rt_oral_argument_2.delete()

def test_send_alert_on_document_creation(self, mock_abort_audio):
"""Avoid sending Search Alerts on document updates."""
Expand Down Expand Up @@ -2498,7 +2587,7 @@ def test_send_multiple_rt_alerts(self, mock_abort_audio):
self.assertEqual(len(memberships), 11)
total_rt_alerts = Alert.objects.filter(rate=Alert.REAL_TIME)
# 2 created in setUpTestData + 10
self.assertEqual(total_rt_alerts.count(), 12)
self.assertEqual(total_rt_alerts.count(), 13)

# Clear the outbox
mail.outbox = []
Expand Down
8 changes: 3 additions & 5 deletions cl/custom_filters/templatetags/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,11 @@ def get_highlight(result: AttrDict | dict[str, any], field: str) -> any:
"""

hl_value = None
original_value = getattr(result, field, "")
if isinstance(result, AttrDict) and hasattr(result.meta, "highlight"):
hl_value = getattr(result.meta.highlight, field, None)
elif isinstance(result, dict):
hl_value = result.get("meta", {}).get("highlight", {}).get(field)
original_value = result.get(field, "")

return (
render_string_or_list(hl_value)
if hl_value
else getattr(result, field, "")
)
return render_string_or_list(hl_value) if hl_value else original_value

0 comments on commit a993092

Please sign in to comment.