source: products/qPloneGoogleSitemaps/tags/0.5/utils.py

Last change on this file was 1, checked in by myroslav, 14 years ago

Building directory structure

File size: 3.8 KB
Line 
1from urllib2 import urlopen
2from urllib  import quote as urlquote
3from Products.CMFCore.utils import getToolByName
4import re
5import Products.qPloneGoogleSitemaps.config as config
6from OFS.ObjectManager import BadRequestException
7def ping_google(url):
8    """Ping sitemap to Google"""
9    sitemap_url = urlquote(url + "/google-sitemaps")
10    g = urlopen('http://www.google.com/webmasters/sitemaps/ping?sitemap='+sitemap_url)
11    result = g.read()
12    g.close()
13    return 0
14
15def searchAndReplace(string, what, with):
16    """Emulate sed command s/"""
17    res = re.sub(what,with,string)
18    return res
19
20OPERATORS = {'s': searchAndReplace,}
21
22def applyOperations(objects, operations):
23    """Parse Operations """
24    parse = re.compile(r'([a-zA-Z])/(.*?[^\\])/(.*?[^\\]?)/' )
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)
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
42def 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"""
66def 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
73def 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(config.ping_googlesitemap, 'Ping sitemap', 'qPloneGoogleSitemaps.ping_googlesitemap', config.ping_googlesitemap)
88        transitions_set = transmap[wfid]
89        for transition in workflow.transitions.values():
90            trid = transition.id
91            tras = transition.after_script_name
92            if (tras == '') and (trid in transitions_set):
93                #set
94                after_script = config.ping_googlesitemap
95            elif (tras == config.ping_googlesitemap) and not (trid in transitions_set):
96                #reset
97                after_script = ''
98            else:
99                #avoid properties set
100                continue
101            transition.setProperties(title=transition.title,
102                                     new_state_id=transition.new_state_id,
103                                     after_script_name=after_script,
104                                     actbox_name=transition.actbox_name)
105
Note: See TracBrowser for help on using the repository browser.