[3510] | 1 | from urllib import quote as urlquote |
---|
| 2 | from DateTime import DateTime |
---|
[3566] | 3 | import logging |
---|
| 4 | import urllib2 |
---|
| 5 | import socket |
---|
[1593] | 6 | |
---|
| 7 | from Globals import DevelopmentMode |
---|
[3163] | 8 | #from OFS.ObjectManager import BadRequestException |
---|
[3510] | 9 | from Products.CMFCore.utils import getToolByName |
---|
[1593] | 10 | |
---|
[2246] | 11 | from quintagroup.plonegooglesitemaps import config |
---|
[1593] | 12 | |
---|
[3152] | 13 | |
---|
[3566] | 14 | logger = logging.getLogger(__name__) |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | # BBB: 'timeout' was added to urlopen in python2.6. |
---|
| 18 | # Method dedicated to compatibility with python2.4 and python2.5 |
---|
| 19 | def urlopen(request, timeout=5.0, data=None, marker=[]): |
---|
| 20 | global_timeout = marker |
---|
| 21 | try: |
---|
| 22 | try: |
---|
| 23 | urllib2.urlopen(request, data, timeout=timeout) |
---|
| 24 | except TypeError, e: |
---|
| 25 | logger.info('TypeError: %s. You use python < 2.6. ' |
---|
| 26 | 'Ugly socket.setdefaulttimeout hack applied ' |
---|
| 27 | 'to avoid it upgrade your Plone.' % e) |
---|
| 28 | # socket.getdefaulttimeout() -> None. |
---|
| 29 | # It indicates that new socket objects have no timeout. |
---|
| 30 | global_timeout = socket.getdefaulttimeout() |
---|
| 31 | socket.setdefaulttimeout(timeout) |
---|
| 32 | urllib2.urlopen(request) |
---|
| 33 | finally: |
---|
| 34 | if global_timeout is not marker: |
---|
| 35 | socket.setdefaulttimeout(global_timeout) |
---|
| 36 | |
---|
| 37 | |
---|
[3568] | 38 | def ping_google(plone_home, sitemap_relative_path): |
---|
[1593] | 39 | """Ping sitemap to Google""" |
---|
[3569] | 40 | resurl = plone_home + '/' + sitemap_relative_path |
---|
[1622] | 41 | |
---|
[2246] | 42 | if DevelopmentMode or config.testing: |
---|
[1622] | 43 | #prevent pinging in debug or testing mode |
---|
| 44 | print "Pinged %s sitemap to Google" % resurl |
---|
[1593] | 45 | return 0 |
---|
| 46 | |
---|
[3566] | 47 | request = 'http://www.google.com/webmasters/tools/ping?sitemap=' +\ |
---|
| 48 | urlquote(resurl) |
---|
| 49 | try: |
---|
| 50 | # BBB: Change urlopen -> socket.urlopen when |
---|
| 51 | # compatibility with python2.4 is not important |
---|
| 52 | g = urlopen(request) |
---|
| 53 | except urllib2.URLError, e: |
---|
| 54 | if hasattr(e, 'reason'): |
---|
| 55 | logger.error('We failed to reach a server. ' |
---|
| 56 | 'Request: %s. ' |
---|
| 57 | 'Reason: %s' % (request, e.reason)) |
---|
| 58 | elif hasattr(e, 'code'): |
---|
| 59 | logger.error('The server couldn\'t fulfill the request. ' |
---|
| 60 | 'Request: %s ' |
---|
| 61 | 'Error code: %s. ' % (request, e.code)) |
---|
| 62 | else: |
---|
| 63 | # Reading single byte should be enough for server pinged |
---|
| 64 | # to get our request, process it and send some response. |
---|
| 65 | g.read(1) |
---|
| 66 | g.close() |
---|
[1622] | 67 | |
---|
[1593] | 68 | return 0 |
---|
[3510] | 69 | |
---|
| 70 | |
---|
| 71 | def getDefaultPage(obj): |
---|
| 72 | """ Method gets default page for object (folderish) """ |
---|
| 73 | plone_tool = getToolByName(obj, 'plone_utils') |
---|
| 74 | return plone_tool.getDefaultPage(obj) |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | def isDefaultPage(obj): |
---|
| 78 | """ If object is default page then return True""" |
---|
| 79 | plone_tool = getToolByName(obj, 'plone_utils') |
---|
| 80 | return plone_tool.isDefaultPage(obj) |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | def dateTime(obj): |
---|
| 84 | """ Method gets modification date """ |
---|
| 85 | return DateTime(obj.ModificationDate()) |
---|