Skip to content

Commit

Permalink
added serializer to show PDF files preview
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-andreotti committed Sep 26, 2024
1 parent 508c5c5 commit 3354127
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<adapter factory=".summary.IOSanitaJSONSummarySerializer" />
<adapter factory=".summary.PuntoDiContattoJSONSummarySerializer" />
<adapter factory=".modulo.SerializeModuloToJsonSummary" />

<adapter factory=".dxfields.FileFieldViewModeSerializer" />

<!-- Summary Serializer Metadata -->
<utility
Expand Down
56 changes: 56 additions & 0 deletions src/iosanita/contenttypes/restapi/serializers/dxfields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
from Acquisition import aq_inner
from collective.volto.enhancedlinks.interfaces import IEnhancedLinksEnabled
from iosanita.contenttypes.interfaces import IIosanitaContenttypesLayer
from plone.base.utils import human_readable_size
from plone.dexterity.interfaces import IDexterityContent
from plone.namedfile.interfaces import INamedFileField
from plone.restapi.serializer.converters import json_compatible
from plone.restapi.serializer.dxfields import DefaultFieldSerializer
from zope.component import adapter


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

def __call__(self):
namedfile = self.field.get(self.context)
if namedfile is None:
return

url = "/".join(
(
self.context.absolute_url(),
self.get_file_view_mode(namedfile.contentType),
self.field.__name__,
)
)
size = namedfile.getSize()
result = {
"filename": namedfile.filename,
"content-type": namedfile.contentType,
"size": size,
"download": url,
}
if IEnhancedLinksEnabled.providedBy(self.context):
result.update(
{
"getObjSize": human_readable_size(size),
"enhanced_links_enabled": True,
}
)

return json_compatible(result)

def get_file_view_mode(self, content_type):
"""Pdf view depends on the anteprima_file property in thq aq_chain"""
if self.context and "pdf" in content_type:
if getattr(aq_inner(self.context), "anteprima_file", None):
return "@@display-file"

return "@@download"
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
from design.plone.contenttypes.testing import (
DESIGN_PLONE_CONTENTTYPES_API_FUNCTIONAL_TESTING,
)
from plone import api
from plone.app.testing import setRoles
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.app.testing import TEST_USER_ID
from plone.namedfile.file import NamedBlobFile
from plone.restapi.testing import RelativeSession
from transaction import commit

import unittest


class FileFieldSerializerTest(unittest.TestCase):
layer = DESIGN_PLONE_CONTENTTYPES_API_FUNCTIONAL_TESTING

def setUp(self):
self.app = self.layer["app"]
self.portal = self.layer["portal"]
self.request = self.layer["request"]
self.portal_url = self.portal.absolute_url()

self.api_session = RelativeSession(self.portal_url)
self.api_session.headers.update({"Accept": "application/json"})
self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

setRoles(self.portal, TEST_USER_ID, ["Manager"])

self.cartella_modulistica = api.content.create(
container=self.portal,
type="CartellaModulistica",
title="Cartella Modulistica",
)
self.document = api.content.create(
container=self.cartella_modulistica, type="Documento", title="Document"
)
self.modulo = api.content.create(
container=self.document,
type="Modulo",
title="Modulo",
file_principale=NamedBlobFile("some data", filename="file.pdf"),
)
commit()

def tearDown(self):
self.api_session.close()

def test_if_anteprima_file_false_so_download(self):
response = self.api_session.get(self.modulo.absolute_url()).json()

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

def test_if_anteprima_file_true_so_dsiplay(self):
self.cartella_modulistica.anteprima_file = True

commit()

response = self.api_session.get(self.modulo.absolute_url()).json()
self.assertIn("@@display-file", response["file_principale"]["download"])

def test_if_enhancedlinks_behavior_active_has_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"])

def test_if_enhancedlinks_behavior_active_has_flag_in_data(self):
response = self.api_session.get(self.modulo.absolute_url()).json()
self.assertIn("enhanced_links_enabled", response["file_principale"])
self.assertTrue(response["file_principale"]["enhanced_links_enabled"])

0 comments on commit 3354127

Please sign in to comment.