Changeset 3065 in products


Ignore:
Timestamp:
Jan 27, 2011 5:06:39 PM (12 years ago)
Author:
kroman0
Message:

vmaksymiv: Fixed catalogsource path-based exports

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.transmogrifier/branches/plone-2.1/quintagroup.transmogrifier/quintagroup/transmogrifier/catalogsource.py

    r436 r3065  
     1import copy 
     2import logging 
     3 
    14from zope.interface import classProvides, implements 
    25from zope.app.annotation.interfaces import IAnnotations 
     
    710 
    811from quintagroup.transmogrifier.logger import VALIDATIONKEY 
     12 
     13logger = logging.getLogger("CatalogSourceSection") 
    914 
    1015class CatalogSourceSection(object): 
     
    2227        self.pathkey = options.pop('path-key', '_path') 
    2328        self.entrieskey = options.pop('entries-key', '_entries') 
     29 
     30        # handle exclude-contained parameter 
     31        if "exclude-contained" in options.keys(): 
     32            self.exclude_contained = options.pop('exclude-contained') 
     33            self.exclude_contained = self.exclude_contained == "true" 
     34        else: 
     35            self.exclude_contained = False 
    2436 
    2537        # remove 'blueprint' option - it cannot be a query 
     
    4355 
    4456        exported = [] 
     57        exported_parents = [] 
    4558 
    4659        results = list(self.catalog(**self.query)) 
    4760        results.sort(key=lambda x: x.getPath()) 
     61 
    4862        for brain in results: 
    4963            # discussion items are indexed and they must be replaced to 
     
    6882            container_path = path.rsplit('/', 1)[0] 
    6983            while container_path: 
     84 
    7085                if container_path in exported: 
    7186                    container_path = container_path.rsplit('/', 1)[0] 
    7287                    continue 
    73                 contained = self.getContained(container_path) 
     88 
     89                exported_parents.append(container_path) 
     90 
     91                contained = self.getContained(container_path, results, exported_parents) 
     92 
    7493                if contained: 
    7594                    exported.append(container_path) 
     
    7998                    }) 
    8099                container_path = container_path.rsplit('/', 1)[0] 
     100 
    81101            containers.reverse() 
    82102            # order metter for us 
     
    89109            } 
    90110            if brain.is_folderish: 
    91                 contained = self.getContained(path) 
     111                contained = self.getContained(path, results, exported_parents) 
    92112                if contained: 
    93113                    item[self.entrieskey] = contained 
     
    100120            del self.anno[VALIDATIONKEY] 
    101121 
    102     def getContained(self, path): 
     122    def getContained(self, path, orignal_results, parents): 
    103123        """ Return list of (object_id, portal_type) for objects that are returned by catalog 
    104124            and contained in folder with given 'path'. 
     
    106126        results = [] 
    107127        seen = [] 
    108         raw_results = self.catalog(path=path, **self.query) 
     128 
     129 
     130        # Remove the orignal path element from the query if there was one 
     131        query = copy.deepcopy(self.query) 
     132        if "path" in query: 
     133            del query["path"] 
     134 
     135        raw_results = self.catalog(path=path, **query) 
     136 
    109137        for brain in raw_results: 
    110138            current = brain.getPath() 
     
    117145                # object stored in subfolders, we need append to results their parent folder 
    118146                parent_path = '/'.join([path, relative.split('/', 1)[0]]) 
    119                 if parent_path not in seen:                     
     147                if parent_path not in seen: 
    120148                    res = self.catalog(path=path) #, meta_type='Folder') 
    121149                    for i in res: 
     
    128156                seen.append(current) 
    129157                results.append(brain) 
    130         contained = [(i.getId, str(i.portal_type)) for i in results] 
     158 
     159        def filter(r): 
     160 
     161            # Parent objects must be allowed always 
     162            for parent in parents: 
     163                if r.getPath() == parent: 
     164                    return True 
     165 
     166            if r["UID"] in allowed_uids: 
     167                return True 
     168            else: 
     169                logger.info("Excluded contained item as it did not match the orignal catalog query:" + str(r.getPath())) 
     170 
     171        if self.exclude_contained and orignal_results is not None: 
     172            # Filter contained results against our query, so that 
     173            # we do not export results from parent objects which did not match 
     174            # Build list of allowed object UIDs - 
     175            allowed_uids = [ r["UID"] for r in orignal_results ] 
     176 
     177            # All parents must be allowed always 
     178            filtered_results = [ r for r in results if filter(r) == True  ] 
     179        else: 
     180            # Don't filter child items 
     181            filtered_results = results 
     182 
     183        contained = [(i.getId, str(i.portal_type)) for i in filtered_results ] 
     184 
    131185        return tuple(contained) 
Note: See TracChangeset for help on using the changeset viewer.