from core.utils import get_user_id class BaseComponent: """ Base class for all components that need to have a session and an id """ def __init__(self, session, _id=None, **kwargs): self._session = session self._id = _id or self.create_component_id(session) def get_id(self): return self._id def get_session(self): return self._session def get_user_id(self): return get_user_id(self._session) def __repr__(self): return self._id def __eq__(self, other): if type(other) is type(self): return self._id == other.get_id() else: return False def __hash__(self): return hash(self._id) @staticmethod def create_component_id(session): pass class BaseComponentSingleton(BaseComponent): """ Base class for components that will have a single instance per user """ COMPONENT_INSTANCE_ID = None def __init__(self, session, _id=None, settings_manager=None, tabs_manager=None, **kwargs): super().__init__(session, _id, **kwargs) self._settings_manager = settings_manager self.tabs_manager = tabs_manager @classmethod def create_component_id(cls, session): return f"{cls.COMPONENT_INSTANCE_ID}{session['user_id']}"