コード例 #1
0
    def __iter__(self):

        for item in self.previous:

            keys = list(item.keys())
            pathKey = self.pathKey(*keys)[0]

            if not pathKey:  # not enough info
                yield item
                continue

            newPathKey = self.newPathKey or pathKey
            newTypeKey = self.newTypeKey

            path = item[pathKey]
            elems = path.strip("/").rsplit("/", 1)
            container, id = len(elems) == 1 and ("", elems[0]) or elems

            # This may be a new container
            if container not in self.seen:

                containerPathItems = list(pathsplit(container))
                if containerPathItems:

                    checkedElements = []

                    # Check each possible parent folder
                    obj = self.context
                    for element in containerPathItems:
                        checkedElements.append(element)
                        currentPath = "/".join(checkedElements)

                        if currentPath and currentPath not in self.seen:

                            if element and traverse(obj, element) is None:
                                # We don't have this path - yield to create a
                                # skeleton folder
                                yield {
                                    newPathKey: "/" + currentPath,
                                    newTypeKey: self.folderType,
                                }
                            if self.cache:
                                self.seen.add(currentPath)

                        obj = traverse(obj, element)

            if self.cache:
                self.seen.add("%s/%s" % (container, id))

            yield item
コード例 #2
0
    def __iter__(self):

        for item in self.previous:
            pathkey = self.pathkey(*list(item.keys()))[0]
            if not pathkey:  # not enough info
                yield item
                continue
            path = item[pathkey]

            ob = traverse(self.context, str(path).lstrip("/"), None)
            if ob is None:
                yield item
                continue  # object not found

            if not isinstance(ob, CatalogAware):
                yield item
                continue  # can't notify portal_catalog

            if self.verbose:  # add a log to display reindexation progess
                self.counter += 1
                logger.info("Reindex object %s (%s)", path, self.counter)

            # update catalog
            if self.indexes:
                self.portal_catalog.reindexObject(ob, idxs=self.indexes)
            else:
                self.portal_catalog.reindexObject(ob)

            yield item
コード例 #3
0
    def __iter__(self):

        for item in self.previous:
            pathkey = self.pathkey(*list(item.keys()))[0]
            if not pathkey:  # not enough info
                yield item
                continue
            path = item[pathkey]

            ob = traverse(self.context, str(path).lstrip("/"), None)
            if ob is None:
                yield item
                continue  # object not found

            creationdate = item.get(self.creationkey, None)
            if creationdate and hasattr(ob, "creation_date"):
                ob.creation_date = DateTime(creationdate)

            modificationdate = item.get(self.modificationkey, None)
            if modificationdate and hasattr(ob, "modification_date"):
                ob.modification_date = DateTime(modificationdate)

            effectivedate = item.get(self.effectivekey, None)
            if not effectivedate:
                # dexterity one
                effectivedate = item.get("effective", None)
            if (effectivedate and effectivedate != EMPTY_VALUE
                    and hasattr(ob, "effective_date")):
                ob.effective_date = DateTime(effectivedate)

            expirationdate = item.get(self.expirationkey, None)
            if expirationdate and hasattr(ob, "expiration_date"):
                ob.expiration_date = DateTime(expirationdate)

            yield item
コード例 #4
0
    def __iter__(self):
        for item in self.previous:
            pathkey = self.pathkey(*list(item.keys()))[0]
            if not pathkey:
                yield item
                continue

            layoutkey = self.layoutkey(*list(item.keys()))[0]
            defaultpagekey = self.defaultpagekey(*list(item.keys()))[0]

            path = item[pathkey]

            obj = traverse(self.context, str(path).lstrip("/"), None)
            if obj is None:
                yield item
                continue

            if not ISelectableBrowserDefault.providedBy(obj):
                yield item
                continue

            if layoutkey:
                layout = item[layoutkey]
                if layout:
                    obj.setLayout(str(layout))

            if defaultpagekey:
                defaultpage = item[defaultpagekey]
                if defaultpage:
                    obj.setDefaultPage(str(defaultpage))

            yield item
コード例 #5
0
    def __iter__(self):
        # Store positions in a mapping containing an id to position mapping for
        # each parent path {parent_path: {item_id: item_pos}}.
        positions_mapping = {}
        for item in self.previous:
            keys = list(item.keys())
            pathkey = self.pathkey(*keys)[0]
            poskey = self.poskey(*keys)[0]

            if not (pathkey and poskey):
                yield item
                continue

            item_id = item[pathkey].split("/")[-1]
            parent_path = "/".join(item[pathkey].split("/")[:-1])
            if parent_path not in positions_mapping:
                positions_mapping[parent_path] = {}
            positions_mapping[parent_path][item_id] = item[poskey]

            yield item

        # Set positions on every parent
        for path, positions in positions_mapping.items():

            # Normalize positions
            ordered_keys = sorted(list(positions.keys()),
                                  key=lambda x: positions[x])
            normalized_positions = {}
            for pos, key in enumerate(ordered_keys):
                normalized_positions[key] = pos

            # TODO: After the new redturtle.importer.base release (>1.4), the
            # utils.py provides a traverse method.
            from redturtle.importer.base.transmogrifier.utils import traverse

            parent = traverse(self.context, path)
            # parent = self.context.unrestrictedTraverse(path.lstrip('/'))
            if not parent:
                continue

            parent_base = aq_base(parent)

            if getattr(parent_base, "getOrdering", None):
                ordering = parent.getOrdering()
                # Only DefaultOrdering of p.folder is supported
                if not getattr(ordering, "_order", None) and not getattr(
                        ordering, "_pos", None):
                    continue
                order = ordering._order()
                pos = ordering._pos()
                order.sort(key=lambda x: normalized_positions.get(
                    x, pos.get(x, self.default_pos)))
                for i, id_ in enumerate(order):
                    pos[id_] = i

                notifyContainerModified(parent)
コード例 #6
0
    def __iter__(self):
        for item in self.previous:
            pathkey = self.pathkey(*list(item.keys()))[0]
            propertieskey = self.propertieskey(*list(item.keys()))[0]

            if not pathkey or not propertieskey or propertieskey not in item:
                # not enough info
                yield item
                continue

            path = safe_unicode(item[pathkey].lstrip("/"))
            if six.PY2 and isinstance(path, six.text_type):
                path = path.encode("ascii")
            obj = traverse(self.context, path, None)

            if obj is None:
                # path doesn't exist
                yield item
                continue

            if not getattr(aq_base(obj), "_setProperty", False):
                yield item
                continue

            for pid, pvalue, ptype in item[propertieskey]:
                if getattr(aq_base(obj), pid, None) is not None:
                    # if object have a attribute equal to property, do nothing
                    continue

                if ptype == "string" and six.PY2:
                    pvalue = safe_unicode(pvalue).encode("utf-8")

                try:
                    if obj.hasProperty(pid):
                        obj._updateProperty(pid, pvalue)
                    else:
                        obj._setProperty(pid, pvalue, ptype)
                except ConflictError:
                    raise
                except Exception as e:
                    raise Exception(
                        'Failed to set property "%s" type "%s"'
                        ' to "%s" at object %s. ERROR: %s'
                        % (pid, ptype, pvalue, str(obj), str(e))
                    )

            yield item
コード例 #7
0
    def __iter__(self):
        for item in self.previous:
            pathkey = self.pathkey(*list(item.keys()))[0]
            ownerkey = self.ownerkey(*list(item.keys()))[0]

            if (not pathkey or not ownerkey
                    or ownerkey not in item):  # not enough info
                yield item
                continue

            if item[ownerkey] is None or len(item[ownerkey]) != 2:
                # owner is None or something else went wrong
                yield item
                continue

            path = safe_unicode(item[pathkey].lstrip("/"))
            if six.PY2 and isinstance(path, six.text_type):
                path = path.encode("ascii")
            obj = traverse(self.context, path, None)

            if obj is None:
                yield item
                continue

            if item[ownerkey][0] and item[ownerkey][1]:
                try:
                    obj.changeOwnership(
                        self.memtool.getMemberById(item[ownerkey][1]))
                except Exception as e:
                    raise Exception("ERROR: %s SETTING OWNERSHIP TO %s" %
                                    (str(e), item[pathkey]))

                try:
                    obj.manage_setLocalRoles(item[ownerkey][1], ["Owner"])
                except Exception as e:
                    raise Exception("ERROR: %s SETTING OWNERSHIP2 TO %s" %
                                    (str(e), item[pathkey]))

            elif not item[ownerkey][0] and item[ownerkey][1]:
                try:
                    obj._owner = item[ownerkey][1]
                except Exception as e:
                    raise Exception("ERROR: %s SETTING __OWNERSHIP TO %s" %
                                    (str(e), item[pathkey]))

            yield item
コード例 #8
0
    def __iter__(self):
        for item in self.previous:
            keys = list(item.keys())
            typekey = self.typekey(*keys)[0]
            pathkey = self.pathkey(*keys)[0]
            if not (typekey and pathkey):
                logger.warn("Not enough info for item: {0}".format(item))
                yield item
                continue
            type_, path = item[typekey], item[pathkey]

            # in generale se ce questa cosa qua non va bene
            if type_ in ("Plone Site", ):
                continue

            fti = self.ttool.getTypeInfo(type_)
            if fti is None:
                raise Exception("Missing {0} content type".format(type_))

            container, id = posixpath.split(path.strip("/"))
            context = traverse(self.context, container, None)
            if context is None:
                error = "Container {0} does not exist for item {1}".format(
                    container, path)
                if self.required:
                    raise KeyError(error)
                logger.warn(error)
                yield item
                continue

            if id.startswith("++"):
                continue

            if getattr(aq_base(context), id, None) is not None:  # item exists
                old = context[id]
                if old.portal_type == "LRF":
                    # it's a Language root folder: don't touch it
                    continue
                if self.overwrite:
                    logger.info("[overwrite] remove object %s",
                                old.absolute_url())
                    del context[id]

                else:
                    yield item
                    continue

            try:
                obj = fti._constructInstance(context, id)
            except (AttributeError, ValueError):
                logger.exception("item:%s id:%s", item, id)
                yield item

            # For CMF <= 2.1 (aka Plone 3)
            if getattr(fti, "_finishConstruction", None):
                obj = fti._finishConstruction(obj)

            if obj.getId() != id:
                item[pathkey] = posixpath.join(container, obj.getId())

            yield item