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

Fix import content Decimal json.dumps #215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Changelog

- Don't re-use `mapping` variable when migrating portlet data.
[witsch]
- Fix import content Decimal json.dumps - refs #212
[avoinea]


1.9 (2023-05-18)
Expand Down
11 changes: 10 additions & 1 deletion src/collective/exportimport/import_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from zope.interface import alsoProvides
from ZPublisher.HTTPRequest import FileUpload

from decimal import Decimal
import dateutil
import ijson
import json
Expand All @@ -47,6 +48,14 @@
BLOB_HOME = os.getenv("COLLECTIVE_EXPORTIMPORT_BLOB_HOME", "")


class JSONEncoder(json.JSONEncoder):
"""JSON encoder that can handle Decimals."""
def default(self, obj):
if isinstance(obj, Decimal):
return float(obj)
return json.JSONEncoder.default(self, obj)


def get_absolute_blob_path(obj, blob_path):
"""Get absolute path to a blob.

Expand Down Expand Up @@ -428,7 +437,7 @@ def handle_new_object(self, item, index, new):

# import using plone.restapi deserializers
deserializer = getMultiAdapter((new, self.request), IDeserializeFromJson)
self.request["BODY"] = json.dumps(item)
self.request["BODY"] = json.dumps(item, cls=JSONEncoder)
try:
new = deserializer(validate_all=False)
except Exception:
Expand Down