Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB field changes #18

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bloxlink_lib/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,13 @@ async def update_item(domain: str, item_id: str, **aspects) -> None:

# update database
await mongo.bloxlink[domain].update_one(
{"_id": item_id}, {"$set": set_aspects, "$unset": unset_aspects}, upsert=True
{"_id": item_id},
{
"$set": set_aspects,
"$unset": unset_aspects,
"$currentDate": {"updatedAt": True},
},
upsert=True,
)


Expand Down
34 changes: 18 additions & 16 deletions bloxlink_lib/models/guilds.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Mapping, Self, Type, Literal, Annotated
from pydantic import Field, field_validator
import hikari
from datetime import datetime
from .base import PydanticList, BaseModel, PydanticDict, MemberSerializable
from ..validators import is_positive_number_as_str
from .migrators import migrate_restrictions
Expand Down Expand Up @@ -70,15 +70,8 @@ class GuildData(BaseModel):
"""Representation of the stored settings for a guild"""

id: Annotated[int, Field(alias="_id")]
binds: Annotated[list[binds_module.GuildBind], Field(default_factory=list)]

@field_validator("binds", mode="before")
@classmethod
def transform_binds(cls: Type[Self], binds: list) -> list[binds_module.GuildBind]:
if all(isinstance(b, binds_module.GuildBind) for b in binds):
return binds

return [binds_module.GuildBind(**b) for b in binds]
binds: Annotated[list[binds_module.GuildBind], Field(default_factory=list)]

verifiedRoleEnabled: bool = True
verifiedRoleName: str | None = "Verified" # deprecated
Expand Down Expand Up @@ -106,13 +99,6 @@ def transform_binds(cls: Type[Self], binds: list) -> list[binds_module.GuildBind

restrictions: PydanticList[GuildRestriction] = Field(default_factory=list)

@field_validator("restrictions", mode="before")
@classmethod
def transform_restrictions(
cls: Type[Self], restrictions: dict[str, dict[str, GuildRestriction]]
) -> list[GuildRestriction]:
return migrate_restrictions(restrictions)

webhooks: Webhooks = None

hasBot: bool = False
Expand All @@ -130,6 +116,22 @@ def transform_restrictions(
groupIDs: PydanticDict = None
migratedBindsToV4: bool = False

# field converters
@field_validator("binds", mode="before")
@classmethod
def transform_binds(cls: Type[Self], binds: list) -> list[binds_module.GuildBind]:
if all(isinstance(b, binds_module.GuildBind) for b in binds):
return binds

return [binds_module.GuildBind(**b) for b in binds]

@field_validator("restrictions", mode="before")
@classmethod
def transform_restrictions(
cls: Type[Self], restrictions: dict[str, dict[str, GuildRestriction]]
) -> list[GuildRestriction]:
return migrate_restrictions(restrictions)

def model_post_init(self, __context):
# merge verified roles into binds
if self.verifiedRole:
Expand Down
2 changes: 2 additions & 0 deletions bloxlink_lib/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class UserData(BaseModel):
"""

id: Annotated[int, Field(alias="_id")]
updatedAt: datetime

robloxID: str | None = None
robloxAccounts: dict = Field(
default_factory=lambda: {"accounts": [], "guilds": {}, "confirms": {}}
Expand Down