ilyasmirnov

Untitled

Apr 7th, 2022
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. from aiogram_dialog import DialogManager
  2. from contextlib import suppress
  3. from dataclasses import dataclass
  4.  
  5. @dataclass
  6. class HlamData:
  7.     ad_id = None
  8.     ad_type = None
  9.     hlam_ad = None
  10.     dialog_error = None
  11.     ad_message_id = None
  12.     old_hlam_ad = None
  13.     num_photos = None
  14.     changed = None
  15.     dialog_manager: DialogManager = None
  16.  
  17.     @classmethod
  18.     def parse(cls, dialog_manager: DialogManager) -> 'HlamData':
  19.         inst = HlamData()
  20.         inst.dialog_manager = dialog_manager
  21.         inst.fetch()
  22.         return inst
  23.  
  24.     def fetch(self):
  25.         dialog_data = self.dialog_manager.current_context().dialog_data
  26.         for k, v in dialog_data.items():
  27.             with suppress(Exception):
  28.                 super(HlamData, self).__setattr__(k, v)
  29.  
  30.     def __getattr__(self, item):
  31.         self.fetch()
  32.         super(HlamData, self).__getattribute__(self, item)
  33.  
  34.     def __setattr__(self, key, value):
  35.         if key != 'dialog_manager':
  36.             self.fetch()
  37.             self.dialog_manager.current_context().dialog_data[key] = value
  38.         super(HlamData, self).__setattr__(key, value)
  39.  
  40.     def force_dict(self) -> typing.Dict:
  41.         return {k: v for k, v in self.__dict__.items() if v is not None and k != 'dialog_manager'}
  42.  
Advertisement
Add Comment
Please, Sign In to add comment