[scrobbles] Fix note str output
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from unittest.mock import patch, MagicMock
|
from unittest.mock import patch, MagicMock
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import time_machine
|
import time_machine
|
||||||
@ -8,6 +9,7 @@ from django.urls import reverse
|
|||||||
from music.models import Track, Artist, Album
|
from music.models import Track, Artist, Album
|
||||||
from podcasts.models import PodcastEpisode
|
from podcasts.models import PodcastEpisode
|
||||||
from scrobbles.models import Scrobble
|
from scrobbles.models import Scrobble
|
||||||
|
from tasks.models import Task
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
@ -405,9 +407,117 @@ def test_scrobble_jellyfin_track(
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.data == {"scrobble_id": 1}
|
assert response.data == {"scrobble_id": 1}
|
||||||
|
|
||||||
scrobble = Scrobble.objects.get(id=1)
|
scrobble = Scrobble.objects.get(id=1)
|
||||||
assert scrobble.media_obj.__class__ == Track
|
assert scrobble.media_obj.__class__ == Track
|
||||||
assert scrobble.media_obj.title == "Emotion"
|
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")
|
@pytest.mark.skip("Need to refactor")
|
||||||
|
|||||||
@ -950,18 +950,7 @@ class ScrobbleDetailView(DetailView):
|
|||||||
FormClass = self.get_form_class()
|
FormClass = self.get_form_class()
|
||||||
|
|
||||||
log = self.object.log or {}
|
log = self.object.log or {}
|
||||||
initial_notes = log.get("notes", [])
|
log["notes"] = self.object.logdata.notes_as_str()
|
||||||
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
|
|
||||||
|
|
||||||
return FormClass(initial=log)
|
return FormClass(initial=log)
|
||||||
|
|
||||||
|
|||||||
@ -54,7 +54,7 @@ class TaskLogData(BaseLogData):
|
|||||||
if isinstance(note, str):
|
if isinstance(note, str):
|
||||||
lines.append(note)
|
lines.append(note)
|
||||||
|
|
||||||
return "\n".join(lines)
|
return "\n".join(lines).encode("utf-8").decode("unicode_escape")
|
||||||
|
|
||||||
|
|
||||||
class Task(LongPlayScrobblableMixin):
|
class Task(LongPlayScrobblableMixin):
|
||||||
|
|||||||
Reference in New Issue
Block a user