Skip to content

Commit

Permalink
do not use key/value pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Nov 7, 2023
1 parent b74c833 commit 1c582b8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 26 deletions.
2 changes: 1 addition & 1 deletion redturtle/bandi/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<metadata>
<version>2101</version>
<version>2200</version>
</metadata>
55 changes: 50 additions & 5 deletions redturtle/bandi/upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from plone import api
from plone.app.event.base import default_timezone
from redturtle.bandi import logger
from redturtle.bandi.interfaces.settings import IBandoSettings
from zope.component import getUtility
from zope.schema.interfaces import IVocabularyFactory

import pytz

Expand Down Expand Up @@ -48,10 +51,10 @@ def migrate_to_1100(context):
)

criteria_mapping = {
u"getTipologia_bando": u"tipologia_bando",
u"getChiusura_procedimento_bando": u"chiusura_procedimento_bando",
u"getScadenza_bando": u"scadenza_bando",
u"getDestinatariBando": u"destinatari_bando",
"getTipologia_bando": "tipologia_bando",
"getChiusura_procedimento_bando": "chiusura_procedimento_bando",
"getScadenza_bando": "scadenza_bando",
"getDestinatariBando": "destinatari_bando",
}
collections = api.content.find(portal_type="Collection")
tot_results = len(collections)
Expand Down Expand Up @@ -169,4 +172,46 @@ def migrate_to_2101(context):
)
)
bando = brain.getObject()
bando.reindexObject(idxs=['scadenza_bando'])
bando.reindexObject(idxs=["scadenza_bando"])


def migrate_to_2200(context):
bandi = api.content.find(portal_type="Bando")
tot_results = len(bandi)
logger.info("### Fixing {tot} Bandi ###".format(tot=tot_results))

def get_value(key, value):
for entry in api.portal.get_registry_record(
key, interface=IBandoSettings, default=[]
):
id, label = entry.split("|")
if id == value:
return label

for counter, brain in enumerate(bandi):
logger.info(
"[{counter}/{tot}] - {bando}".format(
counter=counter + 1, tot=tot_results, bando=brain.getPath()
)
)
bando = brain.getObject()
tipologia = getattr(bando, "tipologia_bando", "")
destinatari = getattr(bando, "destinatari", "")
if tipologia:
value = get_value(key="tipologie_bando", value=tipologia)
setattr(bando, "tipologia_bando", value)
if destinatari:
value = [get_value(key="default_destinatari", value=x) for x in destinatari]
setattr(bando, "destinatari", value)
bando.reindexObject(idxs=["tipologia_bando", "destinatari_bando"])

# cleanup vocabs
for key in ["tipologie_bando", "default_destinatari"]:
values = []
for old_val in api.portal.get_registry_record(
key, interface=IBandoSettings, default=[]
):
id, label = old_val.split("|")
values.append(label)

api.portal.set_registry_record(key, tuple(values), interface=IBandoSettings)
9 changes: 8 additions & 1 deletion redturtle/bandi/upgrades.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,12 @@
destination="2101"
handler=".upgrades.migrate_to_2101"
profile="redturtle.bandi:default" />


<gs:upgradeStep
title="Do not use key/token pairs in vocabs"
description=""
source="2101"
destination="2200"
handler=".upgrades.migrate_to_2200"
profile="redturtle.bandi:default" />
</configure>
22 changes: 3 additions & 19 deletions redturtle/bandi/vocabularies.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ def __call__(self, context):
values = api.portal.get_registry_record(
"tipologie_bando", interface=IBandoSettings, default=[]
)
terms = []
for tipologia in values:
if tipologia and "|" in tipologia:
key, value = tipologia.split("|", 1)
terms.append(SimpleTerm(value=key, token=key, title=value))
else:
logger.error("invalid tipologia bando %s", tipologia)
terms = [SimpleTerm(value=x, token=x, title=x) for x in values if x]
return SimpleVocabulary(terms)


Expand All @@ -33,15 +27,7 @@ def __call__(self, context):
values = api.portal.get_registry_record(
"default_destinatari", interface=IBandoSettings, default=[]
)

l = []
for i in range(len(values)):
l.append(tuple(values[i].split("|")))

terms = [
SimpleTerm(value=pair[0], token=pair[0], title=pair[1])
for pair in l
]
terms = [SimpleTerm(value=x, token=x, title=x) for x in values if x]
return SimpleVocabulary(terms)


Expand All @@ -53,9 +39,7 @@ class EnteVocabularyFactory(object):
def __call__(self, context):
catalog = api.portal.get_tool("portal_catalog")
enti = list(catalog._catalog.uniqueValuesFor("ente_bando"))
terms = [
SimpleTerm(value=ente, token=ente, title=ente) for ente in enti
]
terms = [SimpleTerm(value=ente, token=ente, title=ente) for ente in enti]

return SimpleVocabulary(terms)

Expand Down

0 comments on commit 1c582b8

Please sign in to comment.