Skip to content

Commit

Permalink
Remove timezone stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Sep 18, 2024
1 parent 13cb00d commit dce113f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 44 deletions.
9 changes: 8 additions & 1 deletion genshin/models/genshin/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import datetime
import typing

import pydantic

from genshin.constants import CN_TIMEZONE
from genshin.models.model import Aliased, APIModel, Unique

Expand Down Expand Up @@ -36,4 +38,9 @@ class ClaimedDailyReward(APIModel, Unique):
name: str
amount: int = Aliased("cnt")
icon: str = Aliased("img")
time: datetime.datetime = Aliased("created_at", timezone=8)
time: datetime.datetime = Aliased("created_at")

@pydantic.field_validator("time")
@classmethod
def __add_timezone(cls, value: datetime.datetime) -> datetime.datetime:
return value.replace(tzinfo=CN_TIMEZONE)
17 changes: 15 additions & 2 deletions genshin/models/genshin/diary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import enum
import typing

import pydantic

from genshin.constants import CN_TIMEZONE
from genshin.models.model import Aliased, APIModel

__all__ = [
Expand Down Expand Up @@ -88,9 +91,14 @@ class DiaryAction(APIModel):

action_id: int
action: str
time: datetime.datetime = Aliased(timezone=8)
time: datetime.datetime
amount: int = Aliased("num")

@pydantic.field_validator("time")
@classmethod
def __add_timezone(cls, value: datetime.datetime) -> datetime.datetime:
return value.replace(tzinfo=CN_TIMEZONE)


class DiaryPage(BaseDiary):
"""Page of a diary."""
Expand Down Expand Up @@ -154,9 +162,14 @@ class StarRailDiaryAction(APIModel):

action: str
action_name: str
time: datetime.datetime = Aliased(timezone=8)
time: datetime.datetime
amount: int = Aliased("num")

@pydantic.field_validator("time")
@classmethod
def __add_timezone(cls, value: datetime.datetime) -> datetime.datetime:
return value.replace(tzinfo=CN_TIMEZONE)


class StarRailDiaryPage(BaseDiary):
"""Page of a diary."""
Expand Down
41 changes: 0 additions & 41 deletions genshin/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import abc
import datetime
import typing

if typing.TYPE_CHECKING:
Expand All @@ -23,41 +22,6 @@
class APIModel(pydantic.BaseModel, abc.ABC):
"""Modified pydantic model."""

@pydantic.root_validator()
def __parse_timezones(cls, values: typing.Dict[str, typing.Any]) -> typing.Dict[str, typing.Any]:
"""Timezones are a pain to deal with so we at least allow a plain hour offset."""
for name, field in cls.__fields__.items():
if isinstance(values.get(name), datetime.datetime) and values[name].tzinfo is None:
timezone = field.field_info.extra.get("timezone", 0)
if not isinstance(timezone, datetime.timezone):
timezone = datetime.timezone(datetime.timedelta(hours=timezone))

values[name] = values[name].replace(tzinfo=timezone)

return values

def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
"""Generate a dictionary representation of the model.
Takes the liberty of also giving properties as fields.
"""
for name in dir(type(self)):
obj = getattr(type(self), name)
if isinstance(obj, property):
value = getattr(self, name, _SENTINEL)

if name[0] == "_" or value is _SENTINEL or value == "":
continue

self.__dict__[name] = value

return super().dict(**kwargs)

if not typing.TYPE_CHECKING:

class Config:
allow_mutation = False


class Unique(abc.ABC):
"""A hashable model with an id."""
Expand All @@ -74,12 +38,7 @@ def __hash__(self) -> int:
def Aliased(
alias: typing.Optional[str] = None,
default: typing.Any = pydantic.main.Undefined, # type: ignore
*,
timezone: typing.Optional[typing.Union[int, datetime.datetime]] = None,
**kwargs: typing.Any,
) -> typing.Any:
"""Create an aliased field."""
if timezone is not None:
kwargs.update(timezone=timezone)

return pydantic.Field(default, alias=alias, **kwargs)

0 comments on commit dce113f

Please sign in to comment.