#-*-coding utf-8-*- # Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) # Работа с подписками from enum import Enum from enum import auto # Тип поля в таблице class SubscribeType(Enum): ANY_ITEM_ADD = auto() ANY_ITEM_DEL = auto() ANY_ITEM_EDIT = auto() ITEM_DEL = auto() ITEM_EDIT = auto() ANY_ITEM_ADD_WITH_PARENT = auto() ANY_ITEM_DEL_WITH_PARENT = auto() ANY_ITEM_EDIT_WITH_PARENT = auto() class BotSubscribes: def __init__(self): self.Clear() def GetSubscribes(self): return self.m_Subscribes def Clear(self): self.m_Subscribes = {} def GetUserIDs(self, a_ModuleName, a_Type, a_ItemID = -1): s = self.GetSubscribes() ids = set() for user_id, su in s.items(): sub_um = su.get(a_ModuleName, None) if sub_um: i = sub_um.get(str(a_Type), None) if i == str(a_ItemID): ids.add(user_id) return ids def AddSubscribe(self, a_UserID, a_ModuleName, a_Type, a_ItemID = -1): s = self.GetSubscribes() if not s.get(a_UserID, None): s[a_UserID] = {} if not s[a_UserID].get(a_ModuleName, None): s[a_UserID][a_ModuleName] = {} s[a_UserID][a_ModuleName][str(a_Type)] = str(a_ItemID) def Test(): a = set() a.add(1) a.add(2) a.add(1) assert 1 in a assert not 3 in a user_id_1 = '123' user_id_2 = '34234' user_id_3 = '4234' mod_1 = 'proj' mod_2 = 'backup' s = BotSubscribes() s.AddSubscribe(user_id_1, mod_1, SubscribeType.ANY_ITEM_ADD) s.AddSubscribe(user_id_2, mod_2, SubscribeType.ITEM_DEL) assert len(s.GetUserIDs(mod_1, SubscribeType.ANY_ITEM_ADD)) == 1 assert len(s.GetUserIDs(mod_1, SubscribeType.ANY_ITEM_DEL)) == 0 assert user_id_1 in s.GetUserIDs(mod_1, SubscribeType.ANY_ITEM_ADD) assert not user_id_2 in s.GetUserIDs(mod_1, SubscribeType.ANY_ITEM_ADD) assert not user_id_3 in s.GetUserIDs(mod_1, SubscribeType.ANY_ITEM_ADD) assert len(s.GetUserIDs(mod_2, SubscribeType.ITEM_DEL)) == 1 assert len(s.GetUserIDs(mod_2, SubscribeType.ANY_ITEM_ADD)) == 0 assert user_id_2 in s.GetUserIDs(mod_2, SubscribeType.ITEM_DEL) assert not user_id_1 in s.GetUserIDs(mod_2, SubscribeType.ITEM_DEL) assert not user_id_3 in s.GetUserIDs(mod_2, SubscribeType.ITEM_DEL)