d. h.
Code: Select all
# This is what's stored in the DB
{
"nonce": "abcdefg",
"data": "wefgsafawfasdadsfsa"
}
Code: Select all
import json
from encryption_lib import decrypt
class User(Model):
__tablename__ == "user"
id = Column(Integer, primary_key=True)
_private1 = Column("private1", Text)
_private2 = Column("private2", Text)
@hybrid_property
def private1(self):
json_data = json.loads(self._private1)
nonce = json_data["nonce"]
data = json_data["data"]
return decrypt(nonce, data) # Decrypt the value so the application can use it when queried
@private1.setter
def private1(self, encrypted):
nonce = encrypted[0]
data = encrypted[1]
self._private1 = json.dumps({"nonce": nonce, "data": data}) # Save the pre-encrypted value to database
# private2 is set up the same way as above
Code: Select all
# ilike (errors)
db.session.query(User).filter(User.private1.ilike(f"%{searchterm}%")).all()
# func.sum (errors)
db.session.query(func.sum(User.private2)).scalar()
Das Problem besteht darin, dass sqlalchemy User.private1 als InstrumentedAttribute anstelle der tatsächlichen Verschlüsselungszeichenfolge aus der Datenbank zurückgibt. Wenn also json.loads() ausgeführt wird, schlägt die Ausführung aufgrund der Daten fehl ist eigentlich nicht json.
Wie kann ich mit ilike und func Suchen nach diesen Hybrideigenschaften ausführen?