From e2dca9da0db0c2d4b4dab4e5210c8dc677fa210b Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Tue, 30 May 2017 21:42:12 +0300 Subject: [PATCH] Set parent object --- aiogram/bot.py | 3 ++- aiogram/types/__init__.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/aiogram/bot.py b/aiogram/bot.py index 278b96c2..3f4dba2f 100644 --- a/aiogram/bot.py +++ b/aiogram/bot.py @@ -35,8 +35,9 @@ class AIOGramBot: if self.session and not self.session.closed: self.session.close() - def prepare_object(self, obj): + def prepare_object(self, obj, parent=None): obj.bot = self + obj.parent = parent return obj @property diff --git a/aiogram/types/__init__.py b/aiogram/types/__init__.py index a0d0982a..e9350e99 100644 --- a/aiogram/types/__init__.py +++ b/aiogram/types/__init__.py @@ -55,6 +55,18 @@ class Deserializable: if hasattr(attr, 'de_json'): attr.bot = bot + @property + def parent(self): + return getattr(self, '_parent', None) + + @parent.setter + def parent(self, value): + setattr(self, '_parent', value) + for name, attr in self.__dict__.items(): + if hasattr(attr, 'de_json'): + attr.parent = self + + @classmethod def de_json(cls, raw_data): """