Skip to content

Commit

Permalink
Add getObjSize info in File field serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Jan 25, 2024
1 parent 53b014d commit d5abe98
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
6.1.11 (unreleased)
-------------------

- Nothing changed yet.
- Add getObjSize info in File field serializer.
[cekk]


6.1.10 (2024-01-16)
Expand Down
11 changes: 9 additions & 2 deletions src/design/plone/contenttypes/restapi/serializers/dxfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from design.plone.contenttypes.interfaces.servizio import IServizio
from plone import api
from plone.app.contenttypes.utils import replace_link_variables_by_paths
from plone.base.utils import human_readable_size
from plone.dexterity.interfaces import IDexterityContent
from plone.namedfile.interfaces import INamedFileField
from plone.outputfilters.browser.resolveuid import uuidToURL
Expand Down Expand Up @@ -56,7 +57,11 @@ def __call__(self):

@adapter(INamedFileField, IDexterityContent, IDesignPloneContenttypesLayer)
class FileFieldViewModeSerializer(DefaultFieldSerializer):
"""Ovveride the basic DX serializer to handle the visualize file functionality"""
"""
Ovveride the basic DX serializer to:
- handle the visualize file functionality
- add getObjSize info
"""

def __call__(self):
namedfile = self.field.get(self.context)
Expand All @@ -70,10 +75,12 @@ def __call__(self):
self.field.__name__,
)
)
size = namedfile.getSize()
result = {
"filename": namedfile.filename,
"content-type": namedfile.contentType,
"size": namedfile.getSize(),
"size": size,
"getObjSize": human_readable_size(size),
"download": url,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ def test_if_visualize_files_true_so_dsiplay(self):
commit()

response = self.api_session.get(self.modulo.absolute_url()).json()

self.assertIn("@@display-file", response["file_principale"]["download"])

def test_human_readable_obj_size_in_data(self):
response = self.api_session.get(self.modulo.absolute_url()).json()
self.assertEqual("1 KB", response["file_principale"]["getObjSize"])

0 comments on commit d5abe98

Please sign in to comment.