diff --git a/tests/scrobbles_tests/test_views.py b/tests/scrobbles_tests/test_views.py index 2b2e701..1c3c057 100644 --- a/tests/scrobbles_tests/test_views.py +++ b/tests/scrobbles_tests/test_views.py @@ -1,6 +1,7 @@ from datetime import datetime, timedelta from unittest.mock import patch, MagicMock from django.utils import timezone +from django.contrib.auth import get_user_model import pytest import time_machine @@ -8,6 +9,7 @@ from django.urls import reverse from music.models import Track, Artist, Album from podcasts.models import PodcastEpisode from scrobbles.models import Scrobble +from tasks.models import Task @pytest.mark.django_db @@ -405,9 +407,117 @@ def test_scrobble_jellyfin_track( assert response.status_code == 200 assert response.data == {"scrobble_id": 1} - scrobble = Scrobble.objects.get(id=1) - assert scrobble.media_obj.__class__ == Track - assert scrobble.media_obj.title == "Emotion" + scrobble = Scrobble.objects.get(id=1) + assert scrobble.media_obj.__class__ == Track + assert scrobble.media_obj.title == "Emotion" + + +@pytest.mark.django_db +def test_scrobble_detail_view_with_notes_as_flat_list(client): + user = get_user_model().objects.create_user( + username="testuser", email="test@example.com", password="testpass" + ) + task = Task.objects.create(title="Test Task", description="Test description") + scrobble = Scrobble.objects.create( + task=task, + media_type="Task", + user=user, + log={ + "notes": ["First note", "Second note"], + "description": "Test description", + }, + ) + url = reverse("scrobbles:detail", kwargs={"uuid": scrobble.uuid}) + response = client.get(url) + assert response.status_code == 200 + assert "First note" in response.content.decode() + assert "Second note" in response.content.decode() + + +@pytest.mark.django_db +def test_scrobble_detail_view_with_notes_as_dict_timestamps(client): + user = get_user_model().objects.create_user( + username="testuser", email="test@example.com", password="testpass" + ) + task = Task.objects.create(title="Test Task", description="Test description") + scrobble = Scrobble.objects.create( + task=task, + media_type="Task", + user=user, + log={ + "notes": [ + {"2024-01-01 10:00:00": "Note at first timestamp"}, + {"2024-01-02 11:30:00": "Note at second timestamp"}, + ], + "description": "Test description", + }, + ) + url = reverse("scrobbles:detail", kwargs={"uuid": scrobble.uuid}) + response = client.get(url) + assert response.status_code == 200 + content = response.content.decode() + assert "2024-01-01 10:00:00" in content + assert "Note at first timestamp" in content + assert "2024-01-02 11:30:00" in content + assert "Note at second timestamp" in content + + +@pytest.mark.django_db +def test_scrobble_detail_view_with_notes_and_labels(client): + user = get_user_model().objects.create_user( + username="testuser", email="test@example.com", password="testpass" + ) + task = Task.objects.create(title="Test Task", description="Test description") + scrobble = Scrobble.objects.create( + task=task, + media_type="Task", + user=user, + log={ + "notes": [ + {"2024-01-01 10:00:00": "Note with label"}, + ], + "labels": ["work", "urgent"], + "description": "Test description", + }, + ) + url = reverse("scrobbles:detail", kwargs={"uuid": scrobble.uuid}) + response = client.get(url) + assert response.status_code == 200 + content = response.content.decode() + assert "work" in content + assert "urgent" in content + + +@pytest.mark.django_db +def test_scrobble_detail_view_post_updates_log(client): + user = get_user_model().objects.create_user( + username="testuser", email="test@example.com", password="testpass" + ) + task = Task.objects.create(title="Test Task", description="Test description") + scrobble = Scrobble.objects.create( + task=task, + media_type="Task", + user=user, + log={ + "notes": ["Original note"], + "description": "Original description", + }, + ) + url = reverse("scrobbles:detail", kwargs={"uuid": scrobble.uuid}) + + client.force_login(user) + response = client.post( + url, + { + "description": "Updated description", + "notes": "Updated note", + }, + ) + assert response.status_code == 302 + + scrobble.refresh_from_db() + assert scrobble.log["description"] == "Updated description" + assert scrobble.log["notes"] == ["Updated note"] @pytest.mark.skip("Need to refactor") diff --git a/vrobbler/apps/scrobbles/views.py b/vrobbler/apps/scrobbles/views.py index 4d2d1fe..6563edf 100644 --- a/vrobbler/apps/scrobbles/views.py +++ b/vrobbler/apps/scrobbles/views.py @@ -950,18 +950,7 @@ class ScrobbleDetailView(DetailView): FormClass = self.get_form_class() log = self.object.log or {} - initial_notes = log.get("notes", []) - if ( - isinstance(initial_notes, list) - and len(initial_notes) > 0 - and isinstance(initial_notes[0], dict) - ): - notes_str = note_list_to_str(notes) - else: - notes_str = "\n".join(initial_notes) if initial_notes else "" - - notes_str_fixed = notes_str.encode("utf-8").decode("unicode_escape") - log["notes"] = notes_str_fixed + log["notes"] = self.object.logdata.notes_as_str() return FormClass(initial=log) diff --git a/vrobbler/apps/tasks/models.py b/vrobbler/apps/tasks/models.py index 19761d4..c35c0c8 100644 --- a/vrobbler/apps/tasks/models.py +++ b/vrobbler/apps/tasks/models.py @@ -54,7 +54,7 @@ class TaskLogData(BaseLogData): if isinstance(note, str): lines.append(note) - return "\n".join(lines) + return "\n".join(lines).encode("utf-8").decode("unicode_escape") class Task(LongPlayScrobblableMixin):