SeaCogs/aurora/models/base.py
Seaswimmer db477c4744
All checks were successful
Actions / Build Documentation (MkDocs) (pull_request) Successful in 47s
Actions / Lint Code (Ruff & Pylint) (pull_request) Successful in 1m10s
fix(aurora): fixed an issue with json encoding
2024-06-03 00:07:52 -04:00

28 lines
1.2 KiB
Python

from typing import Any
from pydantic import BaseModel, ConfigDict
from redbot.core.bot import Red
class AuroraBaseModel(BaseModel):
"""Base class for all models in Aurora."""
model_config = ConfigDict(ignored_types=(Red,), arbitrary_types_allowed=True)
bot: Red
def dump(self) -> dict:
return self.model_dump(exclude={"bot"})
def to_json(self, indent: int | None = None, file: Any | None = None, **kwargs):
from ..utilities.json import dump, dumps # pylint: disable=cyclic-import
return dump(self.dump(), file, indent=indent, **kwargs) if file else dumps(self.model_dump(exclude={"bot"}), indent=indent, **kwargs)
class AuroraGuildModel(AuroraBaseModel):
"""Subclass of AuroraBaseModel that includes a guild_id attribute, and a modified to_json() method to match."""
guild_id: int
def dump(self) -> dict:
return self.model_dump(exclude={"bot", "guild_id"})
def to_json(self, indent: int | None = None, file: Any | None = None, **kwargs):
from ..utilities.json import dump, dumps # pylint: disable=cyclic-import
return dump(self.dump(), file, indent=indent, **kwargs) if file else dumps(self.model_dump(exclude={"bot", "guild_id"}), indent=indent, **kwargs)