Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upgrate step to migrate terms from VCGE 1 to 2 #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case all terms would be migrated? or are there any example of term that would remain as it is from old to new version?

for example, we have 3 old terms ['term1', 'term2', 'term3'];
'term2' should be replaced by 'term4' and 'term3' by 'term5';

with the code written now, the result would be ['term4', 'term5'] because 'term1' is not into equivalences dict.

Is it fine like this?

Copy link
Member Author

@hvelarde hvelarde Jan 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, check the tests.

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>