Override default fetch_from_api and add a caching layer

This commit is contained in:
thec0sm0s
2020-05-14 22:35:37 +05:30
parent 7332ae9f4c
commit e70fd91424
2 changed files with 31 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
from .base import DiscordModelsBase
from .integration import Integration
from flask import current_app, session
class UserConnection(DiscordModelsBase):
@@ -53,3 +54,32 @@ class UserConnection(DiscordModelsBase):
def is_visible(self):
"""A property returning bool if this integration is visible to everyone."""
return bool(self.visibility)
@classmethod
def fetch_from_api(cls, cache=True):
"""A class method which returns an instance or list of instances of this model by implicitly making an
API call to Discord. If an instance of :py:class:`flask_discord.User` exists in the users internal cache
who are attached to these connections then, the cached property :py:attr:`flask_discord.User.connections`
is updated.
Parameters
----------
cache : bool
Determines if the :py:attr:`flask_discord.User.guilds` cache should be updated with the new guilds.
Returns
-------
list[flask_discord.UserConnection, ...]
List of instances of :py:class:`flask_discord.UserConnection` to which this user belongs.
"""
connections = super().fetch_from_api()
if cache:
user = current_app.discord.users_cache.get(session.get("DISCORD_USER_ID"))
try:
user.connections = connections
except AttributeError:
pass
return connections