Changeset 727 in products for qPloneGoogleSitemaps/trunk/utils.py


Ignore:
Timestamp:
Jan 5, 2007 3:11:47 PM (17 years ago)
Author:
crchemist
Message:

Added tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • qPloneGoogleSitemaps/trunk/utils.py

    r55 r727  
    11from urllib2 import urlopen 
    22from urllib  import quote as urlquote 
    3 from Products.CMFCore.utils import getToolByName 
    4 import re 
    5 import Products.qPloneGoogleSitemaps.config as config 
     3 
     4from Globals import DevelopmentMode 
    65from OFS.ObjectManager import BadRequestException 
    7 def ping_google(url): 
     6 
     7from Products.qPloneGoogleSitemaps.config import testing 
     8 
     9def ping_google(url, sitemap_id): 
    810    """Ping sitemap to Google""" 
    9     sitemap_url = urlquote(url + "/google-sitemaps") 
     11    if DevelopmentMode: 
     12        #prevent pinging in debug mode 
     13        print "Pinging Google ...", url, sitemap_id 
     14        return 0 
     15 
     16    resurl = url + "/" + sitemap_id 
     17    sitemap_url = urlquote(resurl) 
    1018    g = urlopen('http://www.google.com/webmasters/sitemaps/ping?sitemap='+sitemap_url) 
    1119    result = g.read() 
    1220    g.close() 
     21    if testing: 
     22        print "Pinged %s sitemap to google" % resurl 
    1323    return 0 
    14  
    15 def searchAndReplace(string, what, with): 
    16     """Emulate sed command s/""" 
    17     res = re.sub(what,with,string) 
    18     return res 
    19  
    20 OPERATORS = {'s': searchAndReplace,} 
    21  
    22 def applyOperations(objects, operations): 
    23     """Parse Operations """ 
    24     parse = re.compile(r"(.?[^\\])/(.*[^\\]|)/(.*[^\\]|)/") 
    25     operations=[parse.match(op).groups() for op in operations] 
    26     result={} 
    27     for ob in objects: 
    28         url = ob.getURL() 
    29         for operator, what, with in operations: 
    30             url = OPERATORS[operator](url, what, with.replace("\\", "")) 
    31         #TODO: Remove or replace following condition 
    32         #it is senseless in the case we need intelligent 
    33         #result set. Better condition would be to place 
    34         #freshest brain into result 
    35         if url in result.keys(): 
    36             continue 
    37         #TODO: replace brain with only data necessary to  
    38         #generate sitemap 
    39         result[url]=ob 
    40     return result 
    41  
    42 def additionalURLs(self): 
    43     """Add URLs to sitemap that arn't objects""" 
    44     res = [] 
    45     plone_home = getToolByName(self, 'portal_url').getPortalObject().absolute_url() 
    46     root = self.getPhysicalRoot().absolute_url() 
    47  
    48     props = getToolByName(self,'portal_properties') 
    49     try: 
    50         URLs = props.googlesitemap_properties.urls 
    51     except AttributeError: 
    52         URLs = [] 
    53  
    54     add_zope = re.compile('^/') 
    55     add_plone= re.compile('^[^http://|https://|\\\]') 
    56     for url in URLs: 
    57         if add_zope.match(url): 
    58             res.append(root+url) 
    59         elif add_plone.match(url): 
    60             res.append(plone_home+'/'+url) 
    61         else: 
    62             res.append(url) 
    63     return res 
    64  
    65 """workflows & co""" 
    66 def getWorkflowTransitions(self,workflow_id): 
    67     pw = getToolByName(self,'portal_workflow') 
    68     wf = pw.getWorkflowById(workflow_id) 
    69     if not wf: 
    70         return None 
    71     return wf.transitions.values() 
    72  
    73 def setWorkflowTransitions(self,transitions): 
    74     """set workflow transitions properties""" 
    75     portal_workflow = getToolByName(self, 'portal_workflow') 
    76     transmap = {} 
    77     for key in transitions: 
    78         if key.find('#')>0: 
    79             ids = key.split('#') 
    80             wfid = ids[0] 
    81             if not wfid in transmap.keys(): 
    82                 transmap[wfid]=[] 
    83             transmap[wfid].append(ids[1]) 
    84     for wfid in transmap.keys(): 
    85         workflow = portal_workflow.getWorkflowById(wfid) 
    86         if config.ping_googlesitemap not in workflow.scripts.objectIds(): 
    87             workflow.scripts.manage_addProduct['ExternalMethod'].manage_addExternalMethod( 
    88                                 config.ping_googlesitemap, 
    89                                 'Ping sitemap', 
    90                                 'qPloneGoogleSitemaps.ping_googlesitemap', 
    91                                 config.ping_googlesitemap) 
    92         transitions_set = transmap[wfid] 
    93         for transition in workflow.transitions.values(): 
    94             trid = transition.id 
    95             tras = transition.after_script_name 
    96             if (tras == '') and (trid in transitions_set): 
    97                 #set 
    98                 after_script = config.ping_googlesitemap 
    99             elif (tras == config.ping_googlesitemap) and not (trid in transitions_set): 
    100                 #reset 
    101                 after_script = '' 
    102             else: 
    103                 #avoid properties set 
    104                 continue 
    105             transition.setProperties(title=transition.title, 
    106                                      new_state_id=transition.new_state_id, 
    107                                      after_script_name=after_script, 
    108                                      actbox_name=transition.actbox_name) 
Note: See TracChangeset for help on using the changeset viewer.