Worked on #13 and #4. There are some caching issues left, though. Introduced abstract class Cacheable. Replaced async_pymongo with pymongo
This commit is contained in:
@@ -17,6 +17,7 @@ from discord.abc import GuildChannel
|
||||
from libbot.cache.classes import Cache
|
||||
from pymongo.results import InsertOneResult
|
||||
|
||||
from classes.abstract import Cacheable
|
||||
from classes.errors import (
|
||||
DiscordCategoryNotFoundError,
|
||||
DiscordChannelNotFoundError,
|
||||
@@ -33,9 +34,17 @@ logger: Logger = get_logger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class PycordUser:
|
||||
class PycordUser(Cacheable):
|
||||
"""Dataclass of DB entry of a user"""
|
||||
|
||||
# TODO Implement this
|
||||
async def update(self, cache: Optional[Cache] = None, **kwargs: Any) -> None:
|
||||
pass
|
||||
|
||||
# TODO Implement this
|
||||
async def reset(self, *args: str, cache: Optional[Cache] = None) -> None:
|
||||
pass
|
||||
|
||||
__slots__ = (
|
||||
"_id",
|
||||
"id",
|
||||
@@ -83,7 +92,7 @@ class PycordUser:
|
||||
)
|
||||
|
||||
if cached_entry is not None:
|
||||
return cls(**cached_entry)
|
||||
return cls(**cls._entry_from_cache(cached_entry))
|
||||
|
||||
db_entry = await cls.__collection__.find_one({"id": user_id, "guild_id": guild_id})
|
||||
|
||||
@@ -98,7 +107,7 @@ class PycordUser:
|
||||
db_entry["_id"] = insert_result.inserted_id
|
||||
|
||||
if cache is not None:
|
||||
cache.set_json(f"{cls.__short_name__}_{user_id}_{guild_id}", db_entry)
|
||||
cache.set_json(f"{cls.__short_name__}_{user_id}_{guild_id}", cls._entry_to_cache(db_entry))
|
||||
|
||||
return cls(**db_entry)
|
||||
|
||||
@@ -186,10 +195,10 @@ class PycordUser:
|
||||
if cache is None:
|
||||
return
|
||||
|
||||
user_dict: Dict[str, Any] = self.to_dict()
|
||||
object_dict: Dict[str, Any] = self.to_dict(json_compatible=True)
|
||||
|
||||
if user_dict is not None:
|
||||
cache.set_json(self._get_cache_key(), user_dict)
|
||||
if object_dict is not None:
|
||||
cache.set_json(self._get_cache_key(), object_dict)
|
||||
else:
|
||||
self._delete_cache(cache)
|
||||
|
||||
@@ -199,6 +208,46 @@ class PycordUser:
|
||||
|
||||
cache.delete(self._get_cache_key())
|
||||
|
||||
@staticmethod
|
||||
def _entry_to_cache(db_entry: Dict[str, Any]) -> Dict[str, Any]:
|
||||
cache_entry: Dict[str, Any] = db_entry.copy()
|
||||
|
||||
cache_entry["_id"] = str(cache_entry["_id"])
|
||||
cache_entry["current_event_id"] = (
|
||||
None if cache_entry["current_event_id"] is None else str(cache_entry["current_event_id"])
|
||||
)
|
||||
cache_entry["current_stage_id"] = (
|
||||
None if cache_entry["current_stage_id"] is None else str(cache_entry["current_stage_id"])
|
||||
)
|
||||
cache_entry["registered_event_ids"] = [
|
||||
str(event_id) for event_id in cache_entry["registered_event_ids"]
|
||||
]
|
||||
cache_entry["completed_event_ids"] = [
|
||||
str(event_id) for event_id in cache_entry["completed_event_ids"]
|
||||
]
|
||||
|
||||
return cache_entry
|
||||
|
||||
@staticmethod
|
||||
def _entry_from_cache(cache_entry: Dict[str, Any]) -> Dict[str, Any]:
|
||||
db_entry: Dict[str, Any] = cache_entry.copy()
|
||||
|
||||
db_entry["_id"] = ObjectId(db_entry["_id"])
|
||||
db_entry["current_event_id"] = (
|
||||
None if db_entry["current_event_id"] is None else ObjectId(db_entry["current_event_id"])
|
||||
)
|
||||
db_entry["current_stage_id"] = (
|
||||
None if db_entry["current_stage_id"] is None else ObjectId(db_entry["current_stage_id"])
|
||||
)
|
||||
db_entry["registered_event_ids"] = [
|
||||
ObjectId(event_id) for event_id in db_entry["registered_event_ids"]
|
||||
]
|
||||
db_entry["completed_event_ids"] = [
|
||||
ObjectId(event_id) for event_id in db_entry["completed_event_ids"]
|
||||
]
|
||||
|
||||
return db_entry
|
||||
|
||||
# TODO Add documentation
|
||||
@staticmethod
|
||||
def get_defaults(user_id: Optional[int] = None, guild_id: Optional[int] = None) -> Dict[str, Any]:
|
||||
|
Reference in New Issue
Block a user