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

Store language references from old plone.multilingual implementation on inline migrated objects #215

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 24 additions & 3 deletions ftw/upgrade/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
import pkg_resources


HAS_LANG_REFS = False
try:
from plone.multilingual.interfaces import ITranslationManager
HAS_LANG_REFS = True
except ImportError:
pass


try:
pkg_resources.get_distribution('archetypes.referencebrowserwidget')
except pkg_resources.DistributionNotFound:
Expand All @@ -56,6 +64,7 @@ class IATReferenceField(Interface):
SKIP_MODIFIED_EVENT = 32

UNMAPPED_FIELDS_BACKUP_ANN_KEY = 'ftw.upgrade.migration:fields_backup'
LANGUAGE_INFORMATIONS_ANN_KEY = 'ftw.upgrade.migration:language_info'

LOG = logging.getLogger('ftw.upgrade.migration')

Expand All @@ -74,12 +83,9 @@ class IATReferenceField(Interface):

DUBLIN_CORE_IGNORES = (
'allowDiscussion',
'contributors',
'creators',
'nextPreviousEnabled',
'rights',
'language',
'relatedItems',
)


Expand Down Expand Up @@ -188,6 +194,7 @@ def __init__(self,
self.dump_and_remove_references,
)
self.steps_after_clone = (
self.store_language_informations,
self.migrate_intid,
self.migrate_field_values,
self.add_relations_to_relation_catalog,
Expand All @@ -213,6 +220,15 @@ def migrate_object(self, old_object):
list(self.final_steps))
return new_object

def store_language_informations(self, old_object, new_object):
if HAS_LANG_REFS:
languages = ITranslationManager(old_object, None)
if languages:
annotations = IAnnotations(new_object)
storage = annotations[LANGUAGE_INFORMATIONS_ANN_KEY] = PersistentMapping()
for language, obj in languages.get_translations().items():
storage[language] = '/'.join(obj.getPhysicalPath())

def dump_and_remove_references(self, old_object):
"""We can only remove the relations from the reference_catalog
as long as we did not replace the object (clone_object()),
Expand Down Expand Up @@ -379,6 +395,11 @@ def get_at_field_values(self, old_object):

value = self.removed_field_values.get(
fieldname, field.getRaw(old_object))

if field.widget.__class__.__name__ == 'LinesWidget' and len(value):
# LinesField/Widget returns a weird raw value
# Example: (u'line1\nline2', )
value = tuple(value[0].split('\n'))
value = self.normalize_at_field_value(field, fieldname, value)
yield fieldname, value

Expand Down