From 921cf9d8b384f5c6fc68575db29a4d5af017b0bb Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Wed, 21 Aug 2024 11:09:15 -0400 Subject: [PATCH] [videogames] Add log data to VideoGame model --- tests/scrobbles_tests/conftest.py | 8 +++++--- vrobbler/apps/scrobbles/dataclasses.py | 9 ++++++++- vrobbler/apps/scrobbles/models.py | 10 +++++++++- vrobbler/apps/videogames/models.py | 5 +++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/tests/scrobbles_tests/conftest.py b/tests/scrobbles_tests/conftest.py index 3023062..9725ec4 100644 --- a/tests/scrobbles_tests/conftest.py +++ b/tests/scrobbles_tests/conftest.py @@ -19,9 +19,11 @@ def boardgame_scrobble(): board_game=BoardGame.objects.create(title="Test Board Game"), media_type="BoardGame", played_to_completion=True, - log='{"players": [{"user_id": ' - + str(user.id) - + ', "win": true, "score": 30, "color": "Blue"}]}', + log={ + "players": [ + {"user_id": user.id, "win": True, "score": 30, "color": "Blue"} + ] + }, ) diff --git a/vrobbler/apps/scrobbles/dataclasses.py b/vrobbler/apps/scrobbles/dataclasses.py index d46a2f2..978482b 100644 --- a/vrobbler/apps/scrobbles/dataclasses.py +++ b/vrobbler/apps/scrobbles/dataclasses.py @@ -132,7 +132,7 @@ class MoodLogData(JSONDataclass): @dataclass -class VideoMetadata(JSONDataclass): +class VideoLogData(JSONDataclass): title: str video_type: str run_time_seconds: int @@ -145,3 +145,10 @@ class VideoMetadata(JSONDataclass): cover_url = Optional[str] next_imdb_id: Optional[int] tv_series_id: Optional[str] + + +@dataclass +class VideoGameLogData(JSONDataclass): + emulated: bool = False + console: Optional[str] = None + emulator: Optional[str] = None diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index 41780b5..c190da1 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -606,7 +606,15 @@ class Scrobble(TimeStampedModel): ) return None - return self.media_obj.logdata_cls.from_dict(json.loads(self.log)) + log_dict = self.log + if isinstance(self.log, str): + # There's nothing stopping django from saving a string ina JSONField :( + logger.warning( + "[scrobbles] Received string in JSON data in log", + extra={"log": self.log}, + ) + log_dict = json.loads(self.log) + return self.media_obj.logdata_cls.from_dict(log_dict) def redirect_url(self, user_id) -> str: user = User.objects.filter(id=user_id).first() diff --git a/vrobbler/apps/videogames/models.py b/vrobbler/apps/videogames/models.py index c135818..f964046 100644 --- a/vrobbler/apps/videogames/models.py +++ b/vrobbler/apps/videogames/models.py @@ -8,6 +8,7 @@ from django.urls import reverse from django_extensions.db.models import TimeStampedModel from imagekit.models import ImageSpecField from imagekit.processors import ResizeToFit +from scrobbles.dataclasses import VideoGameLogData from scrobbles.mixins import LongPlayScrobblableMixin from scrobbles.utils import get_scrobbles_for_media from videogames.igdb import lookup_game_id_from_gdb @@ -167,6 +168,10 @@ class VideoGame(LongPlayScrobblableMixin): def get_start_url(self): return reverse("scrobbles:start", kwargs={"uuid": self.uuid}) + @property + def logdata_cls(self): + return VideoGameLogData + @property def seconds_for_completion(self) -> int: completion_time = self.run_time_ticks