Skip to content

Commit

Permalink
Use dataclasses clientside
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Feb 1, 2025
1 parent 1370fef commit 412cf5d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
15 changes: 13 additions & 2 deletions common/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,15 @@ class TimelineEvent:
type: TimelineEventType


@dataclasses.dataclass(slots=True)
class Review:
user: str
score: int
message: str
likes: int
timestamp: int


@dataclasses.dataclass(slots=True)
class Label:
id: int
Expand Down Expand Up @@ -934,7 +943,7 @@ class Game:
previews_urls : list[str]
downloads : tuple[tuple[str, list[tuple[str, str]]]]
reviews_total : int
reviews : list[dict]
reviews : list[Review]
selected : bool = False
image : "imagehelper.ImageHelper" = None
executables_valids : list[bool] = None
Expand Down Expand Up @@ -1110,7 +1119,9 @@ def __setattr__(self, name: str, value: typing.Any):
"notes",
"image_url",
"previews_urls",
"downloads"
"downloads",
"reviews_total",
"reviews",
]:
if isinstance(attr := getattr(self, name), Timestamp):
attr.update(value)
Expand Down
2 changes: 2 additions & 0 deletions modules/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
OldGame,
Os,
ProxyType,
Review,
SearchResult,
Status,
Tag,
Expand Down Expand Up @@ -776,6 +777,7 @@ async def full_check(game: Game, last_changed: int):
thread["downloads"] = tuple(thread["downloads"])
thread["reviews_total"] = int(thread.get("reviews_total", "0"))
thread["reviews"] = json.loads(thread.get("reviews", "[]"))
thread["reviews"] = [Review(**review) for review in thread["reviews"]]

old_name = game.name
old_version = game.version
Expand Down
10 changes: 9 additions & 1 deletion modules/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import configparser
import contextlib
import dataclasses
import enum
import json
import pathlib
Expand Down Expand Up @@ -388,7 +389,10 @@ def sql_to_py(value: str | int | float, data_type: typing.Type):
value = data_type([value]) if value else data_type()
if args:
content_type = args[0]
value = data_type(x for x in (content_type(x) for x in value) if x is not None)
if hasattr(content_type, "__dataclass_fields__"):
value = data_type(x for x in (content_type(**x) for x in value) if x is not None)
else:
value = data_type(x for x in (content_type(x) for x in value) if x is not None)
case _:
if isinstance(data_type, types.UnionType):
if (
Expand Down Expand Up @@ -492,12 +496,16 @@ def py_to_sql(value: enum.Enum | Timestamp | bool | list | tuple | typing.Any):
elif isinstance(value, list):
value = value.copy()
value = [getattr(item, "value", getattr(item, "id", item)) for item in value]
if value and hasattr(value[0], "__dataclass_fields__"):
value = [dataclasses.asdict(item) for item in value]
value = json.dumps(value)
elif isinstance(value, tuple):
if 3 <= len(value) <= 4 and all(type(item) in (float, int) for item in value):
value = colors.rgba_0_1_to_hex(value)
else:
value = [getattr(item, "value", getattr(item, "id", item)) for item in value]
if value and hasattr(value[0], "__dataclass_fields__"):
value = [dataclasses.asdict(item) for item in value]
value = json.dumps(value)
return value

Expand Down

0 comments on commit 412cf5d

Please sign in to comment.