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