Changeset 3480 in products


Ignore:
Timestamp:
Jul 27, 2012 2:40:05 PM (12 years ago)
Author:
potar
Message:

added two request into portal_catalog

File:
1 copied

Legend:

Unmodified
Added
Removed
  • quintagroup.plonegooglesitemaps/branches/two_request/commonview.py

    r3460 r3480  
    1010from quintagroup.plonegooglesitemaps.interfaces import IBlackoutFilter 
    1111from quintagroup.plonegooglesitemaps.browser.utils import additionalURLs, \ 
    12     applyOperations 
     12     getUrlsObjects, urlFilter, OPERATIONS_PARSE  
    1313 
     14from itertools import chain, starmap 
     15 
     16 
     17SITEMAP_SIZE = 50000 
    1418 
    1519class ISitemapView(Interface): 
     
    6670        return [] 
    6771 
     72    def defaultPagesInfo(self, func, url_filter=lambda x: x[0:x.rfind('/')]): 
     73        """  Method gets default page.  
     74            It dedicated to generate {'http://...': func(brain),...} 
     75        """ 
     76        objects = self.portal_catalog.searchResults(is_default_page=True) 
     77        # func - this fuction gets info from brain 
     78        # eg: func = lambda x: DateTime(x.ModificationDate))  
     79        # url_filter - get parent object (url) 
     80        return dict(  
     81                     (url_filter(url), func(brain)) 
     82               for  
     83                   url, brain  
     84               in  
     85                   getUrlsObjects(objects)) 
     86 
     87    def getObjectsInfo(self, modification_date): 
     88        """ Gets info (list of tuples) for sitemap """ 
     89        # get all brains 
     90        objects = self.getBOFiltered(self.getFilteredObjects()) 
     91        for url, brain in getUrlsObjects(objects): 
     92            yield url, modification_date(brain), \ 
     93                  [(key, func(brain))  
     94                       for  
     95                         key, func  
     96                       in  
     97                         self.additional_maps.iteritems() 
     98                       if key != modification_date.__name__] 
     99         
    68100    def results(self): 
    69101        """ Prepare mapping for template 
    70102        """ 
    71         result = [] 
    72         objects = self.getFilteredObjects() 
    73         reg_exps = self.context.getReg_exp() 
     103        operations = [OPERATIONS_PARSE.match(op).groups()  
     104                      for op in self.context.getReg_exp()] 
    74105 
    75         brain_url_map = applyOperations(self.getBOFiltered(objects), reg_exps) 
    76         # Prepare dictionary for view 
    77         for url, b in brain_url_map.items(): 
    78             res_map = {'url': url, } 
    79             [res_map.update({k: f(b)}) for k, f in self.additional_maps] 
    80             result.append(res_map) 
    81         self.num_entries = len(result) 
    82         return result 
     106        # eg: additional_maps == {'modification_date': lambda x: xxx)} 
     107        modification_date = self.additional_maps['modification_date'] 
     108        urls_info = self.defaultPagesInfo(modification_date) 
     109        # after changing 'modification date' we'll change  
     110        # url according with filter 
     111        num_entries = 0 
     112        for url, date, additional_info in self.getObjectsInfo(modification_date): 
     113 
     114            # TODO: check number of result.  
     115            # A Sitemap file can contain no more than 50,000 URLs. 
     116            if num_entries > SITEMAP_SIZE: 
     117               break  
     118 
     119            if url in urls_info: 
     120                default_page_modification_date = urls_info.get(url)  
     121 
     122                # trying to update modification date 
     123                date = date if date > default_page_modification_date \ 
     124                            else default_page_modification_date 
     125 
     126            result = dict(additional_info) 
     127            result.update({'modification_date': date.HTML4(), 
     128                           'url': urlFilter(url, operations)}) 
     129             
     130            num_entries += 1 
     131            yield result 
    83132 
    84133    def getBOFiltered(self, objects): 
Note: See TracChangeset for help on using the changeset viewer.