def decorator(cls):
        """A decorator to add a to_dict method to a class."""
        def to_dict(self: object):
            """A helper function to convert a class to dict."""
            dic = {}
            # Puts all columns into the dict
            for key in _get_fields(self):
                if key in ignores:
                    continue
                dic[key] = getattr(self, key)

            # Puts extra items specified by consumer
            for extra_key, func in extras.items():
                dic[extra_key] = func(self)
            # Converts type
            for key in dic:
                value = dic[key]
                if isinstance(value, datetime):
                    # If there is no timezone, we should treat it as
                    # UTC datetime,otherwise it will be calculated
                    # as local time when converting to timestamp.
                    # Context: all datetime in db is UTC datetime,
                    # see details in config.py#turn_db_timezone_to_utc
                    if value.tzinfo is None:
                        dic[key] = int(
                            value.replace(tzinfo=timezone.utc).timestamp())
                    else:
                        dic[key] = int(value.timestamp())
                elif isinstance(value, Message):
                    dic[key] = MessageToDict(
                        value,
                        preserving_proto_field_name=True,
                        including_default_value_fields=True)
                elif isinstance(value, Enum):
                    dic[key] = value.name
                elif hasattr(value, 'to_dict'):
                    dic[key] = value.to_dict()

            # remove None and emtry list and dict
            if ignore_none:
                dic = {k: v for k, v in dic.items() if v}

            return dic

        setattr(cls, 'to_dict', to_dict)
        return cls
A decorator to add a to_dict method to a class.
web_console_v2/api/fedlearner_webconsole/utils/mixins.py