Skip to content
Merged
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
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ Changelog
6.3.16 (unreleased)
-------------------

- Nothing changed yet.
- Modulo: ``mime_type`` is now indexed as the mime type of ``file_principale``
(the main attached file). The ``to_7321`` upgrade step re-aligns the catalog
index and refreshes stale ``portal_enhancedlinks`` cache entries for existing
Modulo objects.
[TheSaintSimon]


6.3.15 (2026-01-29)
Expand Down
4 changes: 4 additions & 0 deletions src/design/plone/contenttypes/indexers/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
factory=".punto_di_contatto.PuntoDiContattoMoreTextToIndex"
name="IPuntoDiContatto"
/>
<adapter
factory=".modulo.mime_type"
name="mime_type"
/>
<adapter
factory=".common.exclude_from_search"
name="exclude_from_search"
Expand Down
9 changes: 9 additions & 0 deletions src/design/plone/contenttypes/indexers/modulo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
from design.plone.contenttypes.interfaces.modulo import IModulo
from plone.indexer.decorator import indexer


@indexer(IModulo)
def mime_type(context, **kw):
value = getattr(context, "file_principale", None)
return getattr(value, "contentType", None)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<version>7320</version>
<version>7321</version>
<dependencies>
<dependency>profile-redturtle.bandi:default</dependency>
<dependency>profile-collective.venue:default</dependency>
Expand Down
90 changes: 90 additions & 0 deletions src/design/plone/contenttypes/tests/test_modulo_mime_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
from design.plone.contenttypes.testing import (
DESIGN_PLONE_CONTENTTYPES_INTEGRATION_TESTING,
)
from design.plone.contenttypes.upgrades.to_730x import to_7321
from plone import api
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.namedfile.file import NamedBlobFile

import os
import unittest


class TestModuloMimeType(unittest.TestCase):
layer = DESIGN_PLONE_CONTENTTYPES_INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer["portal"]
setRoles(self.portal, TEST_USER_ID, ["Manager"])

filename = os.path.join(os.path.dirname(__file__), "example.pdf")
with open(filename, "rb") as pdf_file:
pdf_data = pdf_file.read()

# Build a real Modulo with a PDF as main file.
self.documento = api.content.create(
container=self.portal,
type="Documento",
title="Documento",
)
self.modulo = api.content.create(
container=self.documento,
type="Modulo",
title="Modulo",
file_principale=NamedBlobFile(
data=pdf_data,
filename="example.pdf",
contentType="application/pdf",
),
)
self.modulo_2 = api.content.create(
container=self.documento,
type="Modulo",
title="Modulo_2",
file_principale=NamedBlobFile(
data=pdf_data,
filename="example.pdf",
contentType="application/pdf",
),
)

def test_to_7321_refreshes_modulo_mime_type_cache(self):
enhancedlinks_tool = api.portal.get_tool("portal_enhancedlinks")
# Simulate the stale cache entry that the upgrade has to refresh.
Comment thread
cekk marked this conversation as resolved.
enhancedlinks_tool._enhanced_links[self.modulo.UID()] = {
"getObjSize": "1 KB",
"mime_type": "text/plain",
}

# Start from the broken cached mime type.
self.assertEqual(
enhancedlinks_tool.get_enhanced_link(self.modulo.UID())["mime_type"],
"text/plain",
)

# The upgrade reindexes Modulo mime_type and refreshes enhanced links.
to_7321(self.portal)

# After the refresh the cache must reflect the PDF mime type.
self.assertEqual(
enhancedlinks_tool.get_enhanced_link(self.modulo.UID())["mime_type"],
"application/pdf",
)

def test_modulo_base_tool_mime_type(self):
# Without any stale cache, the tool should already have the correct
# mime type for the Modulo (taken from file_principale).
enhancedlinks_tool = api.portal.get_tool("portal_enhancedlinks")
result = enhancedlinks_tool.get_enhanced_link(self.modulo.UID())
self.assertEqual(result["mime_type"], "application/pdf")

def test_modulo_mime_type_indexer(self):
# The indexer should return the correct mime type for the Modulo.

catalog = api.portal.get_tool("portal_catalog")
results = catalog(UID=self.modulo_2.UID())
brain = results[0]

self.assertEqual(brain.mime_type, "application/pdf")
7 changes: 7 additions & 0 deletions src/design/plone/contenttypes/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -969,4 +969,11 @@
destination="7320"
handler=".to_730x.to_7320"
/>
<genericsetup:upgradeStep
title="Reindex Modulo mime_type"
profile="design.plone.contenttypes:default"
source="7320"
destination="7321"
handler=".to_730x.to_7321"
/>
</configure>
20 changes: 20 additions & 0 deletions src/design/plone/contenttypes/upgrades/to_730x.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,23 @@ def to_7320(context):
links = retriever.retrieveLinks()
refs = getObjectsFromLinks(servizio, links)
updateReferences(servizio, refs)


def to_7321(context):

logger.info("### RE-ALIGN MODULES MIME-TYPES ###")

brains = api.content.find(portal_type="Modulo")
logger.info("Reindexing mime_type for {} Modulo objects".format(len(brains)))
portal_enhancedlinks = api.portal.get_tool("portal_enhancedlinks")
cached_uids = portal_enhancedlinks._enhanced_links
tot = len(brains)
for i, brain in enumerate(brains, start=1):
if i % 1000 == 0:
logger.info("Progress: {}/{}".format(i, tot))
brain.getObject().reindexObject(idxs=["mime_type"])
uid = brain.UID
if uid not in cached_uids:
continue
portal_enhancedlinks.get_enhanced_link(uid, force_reload=True)
Comment thread
cekk marked this conversation as resolved.
logger.info("- {}".format(uid))
Loading