1 | from zope.interface import classProvides, implements |
---|
2 | |
---|
3 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
4 | from collective.transmogrifier.utils import defaultMatcher |
---|
5 | |
---|
6 | from Products.CMFCore.utils import getToolByName |
---|
7 | from Products.Archetypes import config as atcfg |
---|
8 | |
---|
9 | # most reference importing actions are done in adapter, what can't be done is saved in |
---|
10 | # next global variables (this must be changed) |
---|
11 | from quintagroup.transmogrifier.adapters.importing import EXISTING_UIDS, REFERENCE_QUEUE |
---|
12 | |
---|
13 | class ReferencesImporterSection(object): |
---|
14 | classProvides(ISectionBlueprint) |
---|
15 | implements(ISection) |
---|
16 | |
---|
17 | def __init__(self, transmogrifier, name, options, previous): |
---|
18 | self.previous = previous |
---|
19 | self.context = transmogrifier.context |
---|
20 | |
---|
21 | def __iter__(self): |
---|
22 | for item in self.previous: |
---|
23 | yield item |
---|
24 | # finalization of importing references |
---|
25 | rc = getToolByName(self.context, atcfg.REFERENCE_CATALOG) |
---|
26 | uc = getToolByName(self.context, atcfg.UID_CATALOG) |
---|
27 | uids = uc.uniqueValuesFor('UID') |
---|
28 | existing = set(uids) |
---|
29 | for suid, rel_fields in REFERENCE_QUEUE.items(): |
---|
30 | instance = rc.lookupObject(suid) |
---|
31 | for fname, tuids in rel_fields.items(): |
---|
32 | # now we update reference field only if all target UIDs are valid |
---|
33 | # but may be it is better to update with as much as possible valid |
---|
34 | # target UIDs (do existing.intersection(set(tuids))) |
---|
35 | if set(tuids).issubset(existing): |
---|
36 | mutator = instance.Schema()[fname].getMutator(instance) |
---|
37 | mutator(tuids) |
---|
38 | EXISTING_UIDS.clear() |
---|
39 | REFERENCE_QUEUE.clear() |
---|