From 71a9e410294d2d3146a9ddb7f2bd7c6d68fa4971 Mon Sep 17 00:00:00 2001
From: Colin Powell
Date: Wed, 26 Aug 2026 13:28:20 -0400
Subject: [PATCH] [scrobbles] Fix sentiment badge always rendering Negative
---
tests/scrobbles_tests/test_utils.py | 73 +++++++++++++++++++
.../templates/scrobbles/scrobble_detail.html | 11 +--
.../templates/scrobbles/scrobble_share.html | 11 +--
3 files changed, 79 insertions(+), 16 deletions(-)
diff --git a/tests/scrobbles_tests/test_utils.py b/tests/scrobbles_tests/test_utils.py
index 4b245cd..24be6d6 100644
--- a/tests/scrobbles_tests/test_utils.py
+++ b/tests/scrobbles_tests/test_utils.py
@@ -7,6 +7,7 @@ from music.models import Artist, Track
from scrobbles.models import Scrobble
from vrobbler.apps.scrobbles.utils import (
+ analyze_scrobble_sentiment,
deduplicate_scrobbles,
timestamp_user_tz_to_utc,
)
@@ -143,3 +144,75 @@ def test_deduplicate_scrobbles_filters_by_media_type(db):
assert count == 0
assert Scrobble.objects.filter(user=user, track=track).count() == 2
+
+
+def _make_boardgame_scrobble_with_notes(notes):
+ from boardgames.models import BoardGame
+
+ user = User.objects.create(email="notes@example.com")
+ board_game = BoardGame.objects.create(title="Test Board Game")
+ return Scrobble.objects.create(
+ user=user,
+ board_game=board_game,
+ media_type="BoardGame",
+ log={"notes": notes},
+ )
+
+
+def test_analyze_scrobble_sentiment_positive(db):
+ scrobble = _make_boardgame_scrobble_with_notes(
+ {"1774476716": "This was amazing and delightful, I loved every minute."}
+ )
+
+ analyzed = analyze_scrobble_sentiment(scrobble)
+
+ assert analyzed is True
+ sentiment = scrobble.log["sentiment"]
+ assert sentiment["compound"] > 0
+ assert set(sentiment) == {"neg", "neu", "pos", "compound"}
+
+
+def test_analyze_scrobble_sentiment_negative(db):
+ scrobble = _make_boardgame_scrobble_with_notes(
+ {"1774476716": "This was terrible, I hated every minute of it."}
+ )
+
+ analyzed = analyze_scrobble_sentiment(scrobble)
+
+ assert analyzed is True
+ assert scrobble.log["sentiment"]["compound"] < 0
+
+
+def test_analyze_scrobble_sentiment_no_notes_skips(db):
+ scrobble = _make_boardgame_scrobble_with_notes({})
+
+ analyzed = analyze_scrobble_sentiment(scrobble)
+
+ assert analyzed is False
+ assert scrobble.log.get("sentiment") is None
+
+
+def test_analyze_scrobble_sentiment_skips_when_already_done(db):
+ scrobble = _make_boardgame_scrobble_with_notes(
+ {"1774476716": "This was amazing and delightful."}
+ )
+ scrobble.log["sentiment"] = {"neg": 0, "neu": 1, "pos": 0, "compound": 0.0}
+ scrobble.save()
+
+ analyzed = analyze_scrobble_sentiment(scrobble)
+
+ assert analyzed is False
+ assert scrobble.log["sentiment"]["compound"] == 0.0
+
+
+def test_analyze_scrobble_sentiment_overwrite(db):
+ scrobble = _make_boardgame_scrobble_with_notes(
+ {"1774476716": "This was amazing and delightful."}
+ )
+ scrobble.log["sentiment"] = {"neg": 0, "neu": 1, "pos": 0, "compound": 0.0}
+ scrobble.save()
+
+ analyzed = analyze_scrobble_sentiment(scrobble, overwrite=True)
+
+ assert analyzed is True
+ assert scrobble.log["sentiment"]["compound"] > 0
diff --git a/vrobbler/templates/scrobbles/scrobble_detail.html b/vrobbler/templates/scrobbles/scrobble_detail.html
index 3211ba9..5d7195a 100644
--- a/vrobbler/templates/scrobbles/scrobble_detail.html
+++ b/vrobbler/templates/scrobbles/scrobble_detail.html
@@ -248,10 +248,11 @@
{% endif %}
-{% with notes_html=object.logdata.notes_as_html %}
+{% with notes_html=object.logdata.notes_as_html sentiment=object.log.sentiment %}
{% if notes_html %}
Notes
+ {% if sentiment %}
{{ notes_html|safe }}
@@ -273,13 +275,6 @@
{% endif %}
{% endwith %}
-{% with sentiment=object.log.sentiment %}
-{% if sentiment %}
-
-
-{% endif %}
-{% endwith %}
-
{% if object.logdata.avg_seconds_per_page %}
Rate: {{object.logdata.avg_seconds_per_page}}s per page
{% endif %}
diff --git a/vrobbler/templates/scrobbles/scrobble_share.html b/vrobbler/templates/scrobbles/scrobble_share.html
index 70b8329..ed1df8a 100644
--- a/vrobbler/templates/scrobbles/scrobble_share.html
+++ b/vrobbler/templates/scrobbles/scrobble_share.html
@@ -164,10 +164,11 @@
{% endif %}
-{% with notes_html=object.public_logdata.notes_as_html %}
+{% with notes_html=object.public_logdata.notes_as_html sentiment=object.public_log.sentiment %}
{% if notes_html %}
Notes
+ {% if sentiment %}
{{ notes_html|safe }}
@@ -189,13 +191,6 @@
{% endif %}
{% endwith %}
-{% with sentiment=object.public_log.sentiment %}
-{% if sentiment %}
-
-
-{% endif %}
-{% endwith %}
-
{% if object.public_logdata.avg_seconds_per_page %}
Rate: {{object.public_logdata.avg_seconds_per_page}}s per page
{% endif %}