root/qPloneGoogleSitemaps/tags/0.6/utils.py

Revision 849 (checked in by crchemist, 2 years ago)

Fixed bug in prefs_gsm_pinging

Line 
1 from urllib2 import urlopen
2 from urllib  import quote as urlquote
3 from Products.CMFCore.utils import getToolByName
4 import re
5 import Products.qPloneGoogleSitemaps.config as config
6 from OFS.ObjectManager import BadRequestException
7 def 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
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 TracBrowser for help on using the browser.