Pārlūkot izejas kodu

[videogames] Add log data to VideoGame model

Colin Powell 8 mēneši atpakaļ
vecāks
revīzija
921cf9d8b3

+ 5 - 3
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"}
+            ]
+        },
     )
 
 

+ 8 - 1
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

+ 9 - 1
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()

+ 5 - 0
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