Skip to content

Commit

Permalink
upgrade step
Browse files Browse the repository at this point in the history
  • Loading branch information
mamico committed Apr 13, 2024
1 parent eefa087 commit 940e4da
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
profile = plone
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<version>7200</version>
<version>7300</version>
<dependencies>
<dependency>profile-redturtle.bandi:default</dependency>
<dependency>profile-collective.venue:default</dependency>
Expand Down
9 changes: 9 additions & 0 deletions src/design/plone/contenttypes/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -848,4 +848,13 @@
handler=".upgrades.to_7200"
/>
</genericsetup:upgradeSteps>

<genericsetup:upgradeStep
title="Migrate from File/Image Field to BlobField"
profile="design.plone.contenttypes:default"
source="7200"
destination="7300"
handler=".to_7300.to_7300"
/>

</configure>
55 changes: 55 additions & 0 deletions src/design/plone/contenttypes/upgrades/to_7300.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
from .upgrades import logger
from Acquisition import aq_base
from plone import api
from plone.namedfile import file


def to_7300(context):

mapping = {
# portal_type
"Documento Personale": {
# field: "type"
"immagine": "image",
"pratica_associata": "file",
},
"Messaggio": {
"documenti _allegati": "file",
},
"Persona": {
"foto_persona": "image",
},
"RicevutaPagamento": {
"stampa_ricevuta": "file",
"pratica_associata": "file",
"allegato": "file",
},
}

mapping_types = {
"image": (file.NamedImage, file.NamedBlobImage),
"file": (file.NamedFile, file.NamedBlobFile),
}

for portal_type, fields in mapping.items():
brains = api.content.find(unrestricted=True, portal_type=portal_type)
logger.info("Updating fields for %s %s objects", portal_type, len(brains))
for brain in brains:
obj = aq_base(brain.getObject())
for fieldname, _type in fields.items():
value = getattr(obj, fieldname, None)
# if value:
# import pdb; pdb.set_trace()
if value and isinstance(value, mapping_types[_type][0]):
logger.info("Updated %s for %s", fieldname, brain.getPath())
setattr(
obj,
fieldname,
mapping_types[_type][1](
data=value.data,
contentType=value.contentType,
filename=value.filename,
),
)
logger.info("Finished updating fields")

0 comments on commit 940e4da

Please sign in to comment.