Update metadata to use JSONWizard
This commit is contained in:
@ -3,6 +3,13 @@ import json
|
|||||||
from dataclasses import asdict, dataclass
|
from dataclasses import asdict, dataclass
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
from dataclass_wizard import JSONWizard
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
|
from vrobbler.apps.locations.models import GeoLocation
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
class ScrobbleMetadataEncoder(json.JSONEncoder):
|
class ScrobbleMetadataEncoder(json.JSONEncoder):
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
@ -14,7 +21,7 @@ class ScrobbleMetadataDecoder(json.JSONDecoder):
|
|||||||
return o.__dict__
|
return o.__dict__
|
||||||
|
|
||||||
|
|
||||||
class JSONMetadata(object):
|
class BaseJSONMetadata(JSONWizard):
|
||||||
@property
|
@property
|
||||||
def asdict(self):
|
def asdict(self):
|
||||||
return asdict(self)
|
return asdict(self)
|
||||||
@ -23,34 +30,34 @@ class JSONMetadata(object):
|
|||||||
def json(self):
|
def json(self):
|
||||||
return json.dumps(self.asdict)
|
return json.dumps(self.asdict)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_dict(cls, env):
|
|
||||||
return cls(
|
|
||||||
**{
|
|
||||||
k: v
|
|
||||||
for k, v in env.items()
|
|
||||||
if k in inspect.signature(cls).parameters
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class BoardGameScore(JSONMetadata):
|
class BoardGameScore(BaseJSONMetadata):
|
||||||
user_id: int
|
user_id: Optional[int] = None
|
||||||
|
name: Optional[str] = None
|
||||||
|
color: Optional[str] = None
|
||||||
score: Optional[int] = None
|
score: Optional[int] = None
|
||||||
win: Optional[bool] = None
|
win: Optional[bool] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class BoardGameMetadata(JSONMetadata):
|
|
||||||
players: Optional[list[BoardGameScore]] = None
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class LifeEventMetadata(JSONMetadata):
|
|
||||||
location: Optional[str] = None
|
location: Optional[str] = None
|
||||||
geo_location_id: Optional[int] = None
|
geo_location_id: Optional[int] = None
|
||||||
|
|
||||||
|
def user(self):
|
||||||
|
return User.objects.filter(id=self.user_id).first()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BoardGameMetadata(BaseJSONMetadata):
|
||||||
|
players: Optional[list[BoardGameScore]] = None
|
||||||
|
|
||||||
|
def geo_location(self):
|
||||||
|
return GeoLocation.objects.filter(id=self.geo_location_id).first()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class LifeEventMetadata(BaseJSONMetadata):
|
||||||
participant_user_ids: Optional[list[int]] = None
|
participant_user_ids: Optional[list[int]] = None
|
||||||
|
location: Optional[str] = None
|
||||||
|
geo_location_id: Optional[int] = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def __dict__(self):
|
def __dict__(self):
|
||||||
@ -60,9 +67,19 @@ class LifeEventMetadata(JSONMetadata):
|
|||||||
def json(self):
|
def json(self):
|
||||||
return json.dumps(self.__dict__)
|
return json.dumps(self.__dict__)
|
||||||
|
|
||||||
|
def participants(self) -> list[User]:
|
||||||
|
participants = []
|
||||||
|
if self.participant_user_ids:
|
||||||
|
for id in self.participant_user_ids:
|
||||||
|
participants.append(User.objects.filter(id=id).first())
|
||||||
|
return participants
|
||||||
|
|
||||||
|
def geo_location(self):
|
||||||
|
return GeoLocation.objects.filter(id=self.geo_location_id).first()
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class VideoMetadata(JSONMetadata):
|
class VideoMetadata(BaseJSONMetadata):
|
||||||
title: str
|
title: str
|
||||||
video_type: str
|
video_type: str
|
||||||
run_time_seconds: int
|
run_time_seconds: int
|
||||||
|
|||||||
Reference in New Issue
Block a user