| 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'([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 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
if url in result.keys(): |
|---|
| 36 |
continue |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 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(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 |
|
|---|
| 94 |
after_script = config.ping_googlesitemap |
|---|
| 95 |
elif (tras == config.ping_googlesitemap) and not (trid in transitions_set): |
|---|
| 96 |
|
|---|
| 97 |
after_script = '' |
|---|
| 98 |
else: |
|---|
| 99 |
|
|---|
| 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 |
|
|---|