Пример #1
0
    def backward(self, **kwargs):
        """ Return backward relations by category
        """
        tabs = {}
        getBRefs = getattr(self.context, 'getBRefs', None)
        if not getBRefs:
            return tabs

        relation = kwargs.get('relation', 'relatesTo')

        relations = getBRefs(relation)
        for relation in relations:
            if not self.checkPermission(relation):
                continue

            backward = getBackwardRelationWith(self.context, relation)
            if not backward:
                continue

            name = backward.getField('backward_label').getAccessor(backward)()
            if name not in tabs:
                tabs[name] = []
            tabs[name].append(relation)
        tabs = tabs.items()
        tabs.sort()
        return tabs
Пример #2
0
    def backward(self, **kwargs):
        """ Return backward relations by category
        """
        tabs = {}
        getBRefs = getattr(self.context, 'getBRefs', None)
        if not getBRefs:
            return tabs

        relation = kwargs.get('relation', 'relatesTo')

        relations = getBRefs(relation) or []
        contentTypes = {}
        nonBackwardRelations = set()

        filtered_relations = self.filter_relation_translations(relations)
        for relation in filtered_relations:
            # save the name and the portal type of the first relation that we
            # have permission to use.
            # this way we can check if other relations are of same portal_type
            # if they are then we don't need to check if it's a backward
            # relation and what is it's name, we can just add it to the tabs
            # for that relation name the relation item
            if not self.checkPermission(relation) or relation.portal_type in \
                    nonBackwardRelations:
                continue
            portalType = relation.portal_type
            # if the portal_type of the relation is not already in
            # contentTypes than we are dealing with a backward relation that
            # is different from the ones we had before therefore we need
            if portalType not in contentTypes:
                backward = getBackwardRelationWith(self.context, relation)
                if not backward:
                    nonBackwardRelations.add(portalType)
                    continue
                name = backward.getField('backward_label').getAccessor(
                    backward)()
                contentTypes[portalType] = name

            name = contentTypes[portalType]
            if name not in tabs:
                tabs[name] = []
            tabs[name].append(relation)
        tabs = tabs.items()
        return tabs
Пример #3
0
    def backward(self, **kwargs):
        """ Return backward relations by category
        """
        tabs = {}
        getBRefs = getattr(self.context, 'getBRefs', None)
        if not getBRefs:
            return tabs

        relation = kwargs.get('relation', 'relatesTo')

        relations = getBRefs(relation) or []
        contentTypes = {}
        nonBackwardRelations = set()

        filtered_relations = self.filter_relation_translations(relations)
        for relation in filtered_relations:
            # save the name and the portal type of the first relation that we
            # have permission to use.
            # this way we can check if other relations are of same portal_type
            # if they are then we don't need to check if it's a backward
            # relation and what is it's name, we can just add it to the tabs
            # for that relation name the relation item
            if not self.checkPermission(relation) or relation.portal_type in \
                    nonBackwardRelations:
                continue
            portalType = relation.portal_type
            # if the portal_type of the relation is not already in
            # contentTypes than we are dealing with a backward relation that
            # is different from the ones we had before therefore we need
            if portalType not in contentTypes:
                backward = getBackwardRelationWith(self.context, relation)
                if not backward:
                    nonBackwardRelations.add(portalType)
                    continue
                name = backward.getField('backward_label').getAccessor(
                                                                   backward)()
                contentTypes[portalType] = name

            name = contentTypes[portalType]
            if name not in tabs:
                tabs[name] = []
            tabs[name].append(relation)
        tabs = tabs.items()
        return tabs
    def __call__(self, **kwargs):
        tabs = {}
        if not getattr(self.context, 'isCanonical', None):
            return tabs.items()
        if self.context.isCanonical():
            # Canonical object, we return nothing
            return []
        else:
            lang = self.context.Language()
            canonical = self.context.getCanonical()
            # Get canonical relations forward and backward.
            # As translations are related to the canonical object,
            # we specify the parameters in the backward not to list them.

            # Used in relations/browser/app/macro.py, but doesn't work
            # when there are no relations on the object
            #
            # fieldname = kwargs.get('fieldname', 'relatedItems')
            # field = canonical.getField(fieldname)
            # if field:
            #    accessor = field.getAccessor(self.context)
            #    rel_forwards = accessor()
            rel_forwards = canonical.getRefs(
                kwargs.get('relation', 'relatesTo'))
            if rel_forwards:
                # Get translations of forward relations, if
                # translations don't exist, return canonical

                contentTypes = {}
                nonForwardRelations = set()
                for relation in rel_forwards:
                    # Get the relation type name
                    if not relation:
                        continue
                    portalType = relation.portal_type
                    if portalType in nonForwardRelations:
                        nonForwardRelations.add(portalType)
                        continue
                    if portalType not in contentTypes:
                        forward = getForwardRelationWith(
                            self.context, relation)
                        if not forward:
                            continue
                        name = forward.getField('forward_label').getAccessor(
                            forward)()
                        contentTypes[portalType] = name
                        tabs[name] = []
                    name = contentTypes[portalType]
                    # #14831 check if relations isn't already added since you
                    # could receive a bunch of translations of a single object
                    # which would result in duplication of relations
                    context_relation = relation.getTranslation(lang)
                    tab = tabs[name]
                    if context_relation:
                        if context_relation not in tab:
                            tab.append(context_relation)
                    else:
                        if relation not in tab:
                            tab.append(relation)

            rel_backwards = canonical.getBRefs(
                kwargs.get('relation', 'relatesTo'))
            if rel_backwards:
                # Get translations of backward relations, if
                # translations don't exist, return canonical
                contentTypes = {}
                nonBackwardRelations = set()
                for relation in rel_backwards:
                    if not relation:
                        continue
                    portalType = relation.portal_type
                    if portalType in nonBackwardRelations:
                        nonBackwardRelations.add(portalType)
                        continue
                    if portalType not in contentTypes:
                        backward = getBackwardRelationWith(
                            self.context, relation)
                        if not backward:
                            continue
                        name = backward.getField('backward_label').getAccessor(
                            backward)()
                        contentTypes[portalType] = name
                        tabs[name] = []
                    name = contentTypes[portalType]

                    context_relation = relation.getTranslation(lang)
                    tab = tabs[name]
                    if context_relation:
                        if context_relation not in tab:
                            tab.append(context_relation)
                    else:
                        if relation not in tab:
                            tab.append(relation)

            if tabs:
                return tabs.items()
            else:
                return []
Пример #5
0
    def backward(self, **kwargs):
        """ Return backward relations by category
        """
        tabs = {}
        relations = []
        context = self.context
        dexterity_context = IDexterityContent.providedBy(context)
        if not dexterity_context:
            getBRefs = getattr(context, 'getBRefs', None)
            if not getBRefs:
                return tabs
            relation = kwargs.get('relation', 'relatesTo')
            relations = getBRefs(relation) or []
        contentTypes = {}
        nonBackwardRelations = set()

        # dexterity relations
        catalog = queryUtility(ICatalog)
        intids = queryUtility(IIntIds)
        from_object = []
        try:
            if catalog:
                relations_generator = catalog.findRelations(
                    dict(to_id=intids.getId(aq_inner(context))))
                for obj in relations_generator:
                    try:
                        obj = obj.from_object
                        from_object.append(obj)
                    except Exception:
                        # broken relation
                        continue
                if dexterity_context:
                    # 134485 reference_catalog checks if isReferenceable is
                    # present as attribute on the object and dexterity needs to
                    # add it manually in order for their uuid to be added to
                    # the catalog
                    context.isReferenceable = True
                    rtool = getToolByName(context, 'reference_catalog')
                    if rtool:
                        refs = rtool.getBackReferences(context)
                        language = context.language
                        for ref in refs:
                            from_uid = ref.sourceUID
                            rel_obj = rtool.lookupObject(from_uid)
                            rel_obj_lang = getattr(aq_base(rel_obj),
                                                   'getLanguage',
                                                   lambda: None)() or \
                                           rel_obj.language
                            if language and language == rel_obj_lang:
                                from_object.append(rel_obj)
        except KeyError:
            if not relations:
                return relations

        filtered_relations = self.filter_relation_translations(relations)
        filtered_relations.extend(from_object)
        for relation in filtered_relations:
            # save the name and the portal type of the first relation that we
            # have permission to use.
            # this way we can check if other relations are of same portal_type
            # if they are then we don't need to check if it's a backward
            # relation and what is it's name, we can just add it to the tabs
            # for that relation name the relation item
            if not self.checkPermission(relation) or relation.portal_type in \
                    nonBackwardRelations:
                continue
            portalType = relation.portal_type
            # if the portal_type of the relation is not already in
            # contentTypes than we are dealing with a backward relation that
            # is different from the ones we had before therefore we need
            if portalType not in contentTypes:
                backward = getBackwardRelationWith(self.context, relation)
                if not backward:
                    nonBackwardRelations.add(portalType)
                    continue
                name = backward.getField('backward_label').getAccessor(
                    backward)()
                contentTypes[portalType] = name

            name = contentTypes[portalType]
            if name not in tabs:
                tabs[name] = []
            tabs[name].append(relation)
        tabs = tabs.items()
        return tabs
Пример #6
0
    def __call__(self, **kwargs):
        tabs = {}
        if not getattr(self.context, 'isCanonical', None):
            return tabs.items()
        if self.context.isCanonical():
            # Canonical object, we return nothing
            return []
        else:
            lang = self.context.Language()
            canonical = self.context.getCanonical()
            # Get canonical relations forward and backward.
            # As translations are related to the canonical object,
            # we specify the parameters in the backward not to list them.

            # Used in relations/browser/app/macro.py, but doesn't work
            # when there are no relations on the object
            #
            # fieldname = kwargs.get('fieldname', 'relatedItems')
            # field = canonical.getField(fieldname)
            # if field:
            #    accessor = field.getAccessor(self.context)
            #    rel_forwards = accessor()
            rel_forwards = canonical.getRefs(kwargs.get('relation',
                                                        'relatesTo'))
            if rel_forwards:
                # Get translations of forward relations, if
                # translations don't exist, return canonical

                contentTypes = {}
                nonForwardRelations = set()
                for relation in rel_forwards:
                    # Get the relation type name
                    if not relation:
                        continue
                    portalType = relation.portal_type
                    if portalType in nonForwardRelations:
                        nonForwardRelations.add(portalType)
                        continue
                    if portalType not in contentTypes:
                        forward = getForwardRelationWith(self.context, relation)
                        if not forward:
                            continue
                        name = forward.getField('forward_label').getAccessor(
                            forward)()
                        contentTypes[portalType] = name
                        tabs[name] = []
                    name = contentTypes[portalType]
                    # #14831 check if relations isn't already added since you
                    # could receive a bunch of translations of a single object
                    # which would result in duplication of relations
                    context_relation = relation.getTranslation(lang)
                    tab = tabs[name]
                    if context_relation:
                        if context_relation not in tab:
                            tab.append(context_relation)
                    else:
                        if relation not in tab:
                            tab.append(relation)

            rel_backwards = canonical.getBRefs(kwargs.get('relation',
                                                          'relatesTo'))
            if rel_backwards:
                # Get translations of backward relations, if
                # translations don't exist, return canonical
                contentTypes = {}
                nonBackwardRelations = set()
                for relation in rel_backwards:
                    if not relation:
                        continue
                    portalType = relation.portal_type
                    if portalType in nonBackwardRelations:
                        nonBackwardRelations.add(portalType)
                        continue
                    if portalType not in contentTypes:
                        backward = getBackwardRelationWith(self.context,
                                                           relation)
                        if not backward:
                            continue
                        name = backward.getField('backward_label').getAccessor(
                            backward)()
                        contentTypes[portalType] = name
                        tabs[name] = []
                    name = contentTypes[portalType]

                    context_relation = relation.getTranslation(lang)
                    tab = tabs[name]
                    if context_relation:
                        if context_relation not in tab:
                            tab.append(context_relation)
                    else:
                        if relation not in tab:
                            tab.append(relation)

            if tabs:
                return tabs.items()
            else:
                return []