diff --git a/redturtle/bandi/profiles/default/metadata.xml b/redturtle/bandi/profiles/default/metadata.xml index 0ad88e5..4d52aee 100644 --- a/redturtle/bandi/profiles/default/metadata.xml +++ b/redturtle/bandi/profiles/default/metadata.xml @@ -1,4 +1,4 @@ - 2101 + 2200 diff --git a/redturtle/bandi/upgrades.py b/redturtle/bandi/upgrades.py index 39d408a..77d31a9 100644 --- a/redturtle/bandi/upgrades.py +++ b/redturtle/bandi/upgrades.py @@ -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 @@ -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) @@ -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) diff --git a/redturtle/bandi/upgrades.zcml b/redturtle/bandi/upgrades.zcml index 8c7263e..d0af100 100644 --- a/redturtle/bandi/upgrades.zcml +++ b/redturtle/bandi/upgrades.zcml @@ -58,5 +58,12 @@ destination="2101" handler=".upgrades.migrate_to_2101" profile="redturtle.bandi:default" /> - + + diff --git a/redturtle/bandi/vocabularies.py b/redturtle/bandi/vocabularies.py index ecb8f97..35237be 100644 --- a/redturtle/bandi/vocabularies.py +++ b/redturtle/bandi/vocabularies.py @@ -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) @@ -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) @@ -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)