Skip to content

Commit

Permalink
Add upgrate step to migrate terms from VCGE 1 to 2
Browse files Browse the repository at this point in the history
  • Loading branch information
hvelarde committed Jan 29, 2018
1 parent ada0124 commit 5e11536
Show file tree
Hide file tree
Showing 9 changed files with 1,636 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Alterações
2.0.3 (unreleased)
^^^^^^^^^^^^^^^^^^

- Nothing changed yet.
- Adiciona upgrade step para migração dos termos do VCGE 1 para VCGE 2.
[hvelarde]


2.0.2 (2017-12-22)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
],
'test': [
'interlude',
'plone.app.robotframework', # XXX: plone.app.event
'plone.app.testing',
'unittest2',
]},
Expand Down
7 changes: 7 additions & 0 deletions src/brasil/gov/vcge/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding:utf-8 -*-
from brasil.gov.vcge.config import PROJECTNAME

import logging


logger = logging.getLogger(PROJECTNAME)
4 changes: 2 additions & 2 deletions src/brasil/gov/vcge/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<metadata>
<version>2000</version>
<version>2001</version>
<dependencies>
<dependency>profile-raptus.autocompletewidget:default</dependency>
<dependency>profile-raptus.autocompletewidget:default</dependency>
</dependencies>
</metadata>
8 changes: 7 additions & 1 deletion src/brasil/gov/vcge/testing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# -*- coding: utf-8 -*-
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PLONE_FIXTURE
from plone.app.testing import PloneSandboxLayer

import pkg_resources


try:
pkg_resources.get_distribution('plone.app.contenttypes')
except pkg_resources.DistributionNotFound:
from plone.app.testing import PLONE_FIXTURE
else:
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE as PLONE_FIXTURE

HAS_DEXTERITY = 'plone.app.dexterity' in pkg_resources.AvailableDistributions()


Expand Down
9 changes: 2 additions & 7 deletions src/brasil/gov/vcge/upgrades/configure.zcml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
i18n_domain="brasil.barra.gov">

<!-- Passos de atualizacao -->
<configure xmlns="http://namespaces.zope.org/zope">
<include package=".v1000" />
<include package=".v2000" />

<include package=".v2001" />
</configure>
1,532 changes: 1,532 additions & 0 deletions src/brasil/gov/vcge/upgrades/v2001/VCGE_DEPARA_01.csv

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions src/brasil/gov/vcge/upgrades/v2001/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding:utf-8 -*-
from brasil.gov.vcge.dx.interfaces import IVCGEDx
from brasil.gov.vcge.logger import logger
from plone import api

import csv
import os


INFO_URL = 'https://www.governoeletronico.gov.br/documentos-e-arquivos/VCGE_DEPARA_01.pdf'


class Migrator():
"""Context manager to handle term migration."""

def __init__(self):
path = os.path.dirname(__file__)
self.filename = os.path.join(path, 'VCGE_DEPARA_01.csv')

def __enter__(self):
"""Read equivalences file and create dictionary."""
self.equivalences = {}
logger.debug('Reading equivalence file')
with open(self.filename) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
new = row['termo_vcge_2B']
if new:
old = row['termo_vcge_1']
self.equivalences[old] = new

def __exit__(self, *args):
pass # update the catalog?

def migrate(self, obj):
"""Migrate existing terms on objects."""
if obj is None:
return # invalid object

if not obj.skos:
return # nothing to do (empty or None)

new = ()
# ignore terms with no equivalence
for term in obj.skos:
if term in self.equivalences:
new += self.equivalences[term]
obj.skos = new


def term_migration(context):
"""Migrate terms from VCGE 1 to 2B."""
msg = 'Some terms of VCGE 1 will be ignored. For more information see: '
logger.warning(msg + INFO_URL)
with Migrator() as migrator:
results = api.content.find(object_provides=IVCGEDx.__identifier__)
logger.info('{0} objects will be processed'.format(len(results)))
for brain in results:
try:
obj = brain.getObject()
except (AttributeError, KeyError):
continue # the object referenced is invalid
migrator.migrate(obj)

logger.info('VCGE terms migrated from 1 to 2B')
18 changes: 18 additions & 0 deletions src/brasil/gov/vcge/upgrades/v2001/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:genericsetup="http://namespaces.zope.org/genericsetup">

<genericsetup:upgradeSteps
source="2000"
destination="2001"
profile="brasil.gov.vcge:default">

<genericsetup:upgradeStep
title="Migrate terms from VCGE 1 to 2B"
description=""
handler=".term_migration"
/>

</genericsetup:upgradeSteps>

</configure>

0 comments on commit 5e11536

Please sign in to comment.