Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATV-172 Fix content formatting when saving to db #84

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions documents/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ast import literal_eval
import json
from io import BytesIO

from Cryptodome.Cipher import AES
Expand All @@ -11,14 +11,16 @@
class EncryptedJSONField(EncryptedFieldMixin, models.JSONField):
def decrypt(self, value):
text = super().decrypt(value)
# Using literal_eval to get a dict from string, because json.loads() doesn't work here after django update
return literal_eval(text)

def encrypt(self, data_to_encrypt):
# data_to_encrypt is Json string and .adapted is a dict like before update
data_to_encrypt = data_to_encrypt.adapted
# Dict is converted into a string which is then encrypted
return super().encrypt(data_to_encrypt)
return json.loads(text)

def get_db_prep_save(self, value, connection):
if self.empty_strings_allowed and value == bytes():
value = ""
# Instead of using db_prep_value use json.dumps() to get a json formatted object
value = json.dumps(value)
if value is not None:
encrypted_value = self.encrypt(value)
return connection.Database.Binary(encrypted_value)


class EncryptedFileField(models.FileField):
Expand Down
Loading