import json from sqlalchemy.types import TypeDecorator, LargeBinary from community_qa_bot.settings import SECRET_KEY from community_qa_bot.aes import AESCipher def get_aes_key() -> bytes: return SECRET_KEY.encode()[:32] class AESEncryptedColumn(TypeDecorator): impl = LargeBinary def process_bind_param(self, value, dialect): if value is not None: json_str = json.dumps(value) return AESCipher(get_aes_key()).encrypt(json_str) return value def process_result_value(self, value, dialect): if value is not None: json_str = AESCipher(get_aes_key()).decrypt(value) return json.loads(json_str) return value