コード例 #1
0
    def forward(self, **kwargs):
        """ Return forward relations by category
        """
        tabs = {}
        fieldname = kwargs.get('fieldname', 'relatedItems')
        field = self.context.getField(fieldname)
        if not field:
            return tabs.items()

        accessor = field.getAccessor(self.context)
        #getRelatedItems = getattr(self.context, 'getRelatedItems', None)

        contentTypes = {}
        nonForwardRelations = set()
        relations = accessor()
        filtered_relations = self.filter_relation_translations(relations)
        for relation in filtered_relations:
            if not self.checkPermission(relation) or relation.portal_type in \
                    nonForwardRelations:
                continue
            portalType = relation.portal_type
            if portalType not in contentTypes:
                forward = getForwardRelationWith(self.context, relation)
                if not forward:
                    nonForwardRelations.add(portalType)
                    continue
                name = forward.getField('forward_label').getAccessor(forward)()
                contentTypes[portalType] = name

            name = contentTypes[portalType]
            if name not in tabs:
                tabs[name] = []
            tabs[name].append(relation)
        tabs = tabs.items()
        return tabs
コード例 #2
0
ファイル: macro.py プロジェクト: ichim-david/eea.relations
    def forward(self, **kwargs):
        """ Return forward relations by category
        """
        tabs = {}
         
        fieldname = kwargs.get('fieldname', 'relatedItems')
        field = self.context.getField(fieldname)
        if not field:
            return tabs

        accessor = field.getAccessor(self.context)
        #getRelatedItems = getattr(self.context, 'getRelatedItems', None)

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

            forward = getForwardRelationWith(self.context, relation)
            if not forward:
                continue

            name = forward.getField('forward_label').getAccessor(forward)()
            if name not in tabs:
                tabs[name] = []
            tabs[name].append(relation)
        tabs = tabs.items()
        tabs.sort()
        return tabs
コード例 #3
0
    def forward(self, **kwargs):
        """ Return forward relations by category
        """
        tabs = {}
        fieldname = kwargs.get('fieldname', 'relatedItems')
        field = self.context.getField(fieldname)
        if not field:
            return tabs.items()

        accessor = field.getAccessor(self.context)
        #getRelatedItems = getattr(self.context, 'getRelatedItems', None)

        contentTypes = {}
        nonForwardRelations = set()
        relations = accessor()
        filtered_relations = self.filter_relation_translations(relations)
        for relation in filtered_relations:
            if not self.checkPermission(relation) or relation.portal_type in \
                    nonForwardRelations:
                continue
            portalType = relation.portal_type
            if portalType not in contentTypes:
                forward = getForwardRelationWith(self.context, relation)
                if not forward:
                    nonForwardRelations.add(portalType)
                    continue
                name = forward.getField('forward_label').getAccessor(forward)()
                contentTypes[portalType] = name

            name = contentTypes[portalType]
            if name not in tabs:
                tabs[name] = []
            tabs[name].append(relation)
        tabs = tabs.items()
        return tabs
コード例 #4
0
ファイル: badrelations.py プロジェクト: eea/eea.relations
    def bad_relations_report(self):
        """ Get bad relations
        """
        res = []
        report = []

        # Get portal relations content type
        ct_type = self.request.get('ct_type', '')
        if ct_type:
            res = self.get_objects
        else:
            return []

        # Get relations
        logger.info('Start generating bad relations report.')
        count = 0

        for brain in res:
            count += 1
            bad_relations = []
            obj = brain.getObject()

            try:
                fwd = obj.getRelatedItems()
                # Check for bad relations
                for rel in fwd:
                    if not getForwardRelationWith(obj, rel):
                        bad_relations.append(rel)
                report.append((obj.Title(), obj, bad_relations))
            except (TypeError, ValueError):
                # The catalog expects AttributeErrors when
                # a value can't be found
                raise AttributeError
            except Exception:
                logger.info('ERROR getting relations for %s', brain.getURL())
            if not count % 100:
                logger.info('done %s out of %s', count, len(res))

        report.sort()
        logger.info('Done generating bad relations report.')
        return report
コード例 #5
0
    def bad_relations_report(self):
        """ Get bad relations
        """
        res = []
        report = []

        # Get portal relations content type
        ct_type = self.request.get('ct_type', '')
        if ct_type:
            res = self.get_objects
        else:
            return []

        # Get relations
        logger.info('Start generating bad relations report.')
        count = 0

        for brain in res:
            count += 1
            bad_relations = []
            obj = brain.getObject()

            try:
                fwd = obj.getRelatedItems()
                # Check for bad relations
                for rel in fwd:
                    if not getForwardRelationWith(obj, rel):
                        bad_relations.append(rel)
                report.append((obj.Title(), obj, bad_relations))
            except (TypeError, ValueError):
                # The catalog expects AttributeErrors when
                # a value can't be found
                raise AttributeError
            except:
                logger.info('ERROR getting relations for %s' % brain.getURL())
            if not (count % 100):
                logger.info('done %s out of %s' % (count, len(res)))

        report.sort()
        logger.info('Done generating bad relations report.')
        return report
コード例 #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 []
コード例 #7
0
ファイル: macro.py プロジェクト: eea/eea.relations
    def forward(self, **kwargs):
        """ Return forward relations by category
        """
        tabs = {}
        fieldname = kwargs.get('fieldname', 'relatedItems')
        field = self.context.getField(fieldname)
        if not field:
            return tabs.items()

        contentTypes = {}
        nonForwardRelations = set()
        relations = []

        # 134485 check within portal catalog for the uid that is set on the
        # raw value of relatedItems if we related a dexterity content type
        # since archetypes will not find it as such we search for the object
        # in the normal portal catalog
        relation_uids = field.getRaw(self.context)
        portal_catalog = getToolByName(self.context, 'portal_catalog')
        for relation_uid in relation_uids:
            brain = portal_catalog(UID=relation_uid)
            if brain:
                try:
                    relations.append(brain[0].getObject())
                except Exception:
                    # broken object
                    continue

        # dexterity relations
        if IDexterityContent.providedBy(self.context):
            catalog = queryUtility(ICatalog)
            intids = queryUtility(IIntIds)
            relations = catalog.findRelations(
                dict(from_id=intids.getId(aq_inner(self.context))))
            to_object = []
            for obj in relations:
                try:
                    obj = obj.to_object
                    to_object.append(obj)
                except Exception:
                    # broken relation
                    continue
            relations = to_object

        filtered_relations = self.filter_relation_translations(relations)
        for relation in filtered_relations:
            if not self.checkPermission(relation) or relation.portal_type in \
                    nonForwardRelations:
                continue
            portalType = relation.portal_type

            if portalType not in contentTypes:
                forward = getForwardRelationWith(self.context, relation)
                if not forward:
                    nonForwardRelations.add(portalType)
                    continue
                name = forward.getField('forward_label').getAccessor(forward)()
                contentTypes[portalType] = name

            name = contentTypes[portalType]
            if name not in tabs:
                tabs[name] = []
            tabs[name].append(relation)
        tabs = tabs.items()
        return tabs
コード例 #8
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 []