+ def encrypt_decrypt_fields(self, item, action, fields=None, flags=None, schema_version=None, salt=None):
+ if not fields:
+ return
+ self.get_secret_key()
+ actions = ['encrypt', 'decrypt']
+ if action.lower() not in actions:
+ raise DbException("Unknown action ({}): Must be one of {}".format(action, actions),
+ http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
+ method = self.encrypt if action.lower() == 'encrypt' else self.decrypt
+ if flags is None:
+ flags = re.I
+
+ def process(_item):
+ if isinstance(_item, list):
+ for elem in _item:
+ process(elem)
+ elif isinstance(_item, dict):
+ for key, val in _item.items():
+ if isinstance(val, str):
+ if any(re.search(f, key, flags) for f in fields):
+ _item[key] = method(val, schema_version, salt)
+ else:
+ process(val)
+ process(item)
+