source: products/qPingTool/branches/plone-3.1/PingTool.py

Last change on this file was 225, checked in by mylan, 18 years ago

Cleaned and equalized with Plone 2.0.5 version patch.py & utils.py

  • Property svn:eol-style set to native
File size: 6.2 KB
RevLine 
[1]1import os
[198]2from Acquisition import aq_base
3from zLOG import LOG
4from zope.interface import implements
5from AccessControl import ClassSecurityInfo
6from Products.PageTemplates.PageTemplateFile import PageTemplateFile
7from Products.Archetypes.public import *
[1]8from Products.CMFCore.ActionProviderBase import ActionProviderBase
[198]9from Products.CMFCore.permissions import ManagePortal
10from Products.CMFCore.utils import getToolByName
[1]11from Products.ATContentTypes.content.folder import ATFolder
12from Products.CMFPlone.interfaces.OrderedContainer import IOrderedContainer
[198]13from Products.CMFPlone.PloneFolder import PloneFolder
[1]14from Products.XMLRPCMethod.XMLRPCMethod import RPCThread, XMLRPCMethod
15
[225]16from Products.qPingTool import qPingToolMessageFactory as _
[198]17from interfaces import IPingTool
18from adapter import ICanonicalURL
19from config import PROJECTNAME
20
[1]21_marker = []
22
23def modify_fti(fti):
24    fti['title'] = 'Portal Ping Tool'
25    fti['allowed_content_types'] = ('PingInfo',)
26    fti['filter_content_types'] = 1
27    #fti['icon'] = 'tool.gif'
28    #fti['immediate_view'] = 'view'
29    #fti['default_view'] = 'view'
30
31
[198]32class PingTool(ATFolder, PloneFolder):
[1]33    """
34
[198]35    >>> IPingTool.implementedBy(PingTool)
36    True
37    """
38    security = ClassSecurityInfo()
[1]39
[198]40    implements(IPingTool)
[1]41    __implements__ = (IOrderedContainer,)
42
[198]43    archetype_name = portal_type = 'PingTool'
[1]44    manage_options =  (
45            {'label' : 'Overview', 'action' : 'manage_overview'},
46        ) + ATFolder.manage_options
47
[208]48    manage_overview = PageTemplateFile(os.path.join('www', 'overview'), globals(), __name__='manage_overview')
[1]49
[198]50    def om_icons(self):
51        """ Checking on ZMI for canonical_url setting."""
52        icons = ({'path':'misc_/qPingTool/tool.gif' \
53                    ,'alt':self.meta_type \
54                    ,'title':self.meta_type \
55                },)
56        if not ICanonicalURL(self).getCanonicalURL():
57            icons = icons + ({'path':'misc_/PageTemplates/exclamation.gif' \
58                                ,'alt':'Error' \
59                                ,'title':'PingTool needs setting canonical_url' \
60                                },)
61        return icons
62
63    security.declareProtected(ManagePortal, 'pingFeedReader')
[1]64    def pingFeedReader(self,context):
65        """ ping """
[198]66        status = 'failed'
[210]67        pingProp = self.getPingProperties(context)
[198]68        if not pingProp['enable_ping']:
[225]69            message = _(u'Ping is dissabled.')
[198]70            return status, message
[215]71        canonical_url = ICanonicalURL(self).getCanonicalURL()
72        if not canonical_url:
[225]73            return status, _(u'Ping is impossible.Setup canonical_url.')
[1]74        ps = getToolByName(context,'portal_syndication')
[210]75        if ps.isSyndicationAllowed(context):
[1]76            sites = pingProp['ping_sites']
[225]77            message = _(u'Select servers.')
[210]78            for site in sites:
79                status = 'success'
[225]80                message = _(u'The servers are pinged.')
[215]81                site_obj = getattr(self, site)
82                site_method = site_obj.getMethod_name()
83                site_url = site_obj.getUrl()
[210]84                PingMethod = XMLRPCMethod('myid', "", site_url, site_method, 25)
85                title = context.Title()
[215]86                rss_version = site_obj.getRss_version()
87                ping_url = pingProp['ping_'+rss_version]
[210]88                try: 
89                    result_ping = PingMethod(title, ping_url)
90                    result = result_ping['message']
91                except:
92                    result = 'The site %s generated error for %s.' % (site_url, ping_url)
93                LOG('qPingTool', 100, result)
94                message += '\nReturned message from %s: %s' % (site_url, str(result))
[198]95        else:
96            message = 'The %s is not syndication allowed' % url
[1]97        return status, message
98
[198]99    security.declareProtected(ManagePortal, 'setupPing')
[1]100    def setupPing(self,context,
101                  enable_ping=0,
102                  ping_sites=(),
[215]103                  ping_Weblog='',
104                  ping_RSS1='',
105                  ping_RSS2='',
[1]106                  REQUEST=None):
107        """   """       
108        obj=aq_base(context)
109        status = 'success'
[225]110        message = _(u'Changes saved.')
[1]111        syInfo = getattr(obj, 'syndication_information', None)
112
113        if syInfo is None:
[225]114            message = _(u'Syndication is Disabled')
[1]115            status = 'failed'
116        else:
117            syInfo.ping_sites = list(ping_sites)
118            syInfo.enable_ping = enable_ping
[215]119            syInfo.ping_Weblog = ping_Weblog
120            syInfo.ping_RSS1 = ping_RSS1
121            syInfo.ping_RSS2 = ping_RSS2
122           
[1]123        return status, message
124
[198]125    security.declareProtected(ManagePortal, 'getPingProperties')
[1]126    def getPingProperties(self, context):
127        """ """
128        obj=aq_base(context)
129        syInfo = getattr(obj, 'syndication_information', None)
[215]130        pingProperties={}
131        pingProperties['ping_sites'] = getattr(syInfo, 'ping_sites', [])
132        pingProperties['enable_ping'] = getattr(syInfo, 'enable_ping', 0)
[1]133
[215]134        pingProperties['ping_Weblog'] = getattr(syInfo, 'ping_Weblog', '')
135        if not pingProperties['ping_Weblog']:
136            pingProperties['ping_Weblog'] = self.getPingDefaultUrl(context, 'Weblog')
137
138        pingProperties['ping_RSS1'] = getattr(syInfo, 'ping_RSS1', '')
139        if not pingProperties['ping_RSS1']:
140            pingProperties['ping_RSS1'] = self.getPingDefaultUrl(context, 'RSS1')
141
142        pingProperties['ping_RSS2'] = getattr(syInfo, 'ping_RSS2', '')
143        if not pingProperties['ping_RSS2']:
144            pingProperties['ping_RSS2'] = self.getPingDefaultUrl(context, 'RSS2')
145
146        return  pingProperties
147       
148    security.declareProtected(ManagePortal, 'getPingDefaultUrl')
149    def getPingDefaultUrl(self, context, rss_version='Weblog'):
[210]150        rss_templates = {'Weblog':'','RSS1':'/RSS','RSS2':'/RSS2'}
151        url = getToolByName(context, 'portal_url').getRelativeContentURL(context)
[215]152        canonical_url = ICanonicalURL(self).getCanonicalURL()
[210]153        ping_url = ''
[215]154        if canonical_url:
155            if not canonical_url[-1] == '/':
156                canonical_url += '/'
157            url = canonical_url + url
158            site_rss_version = rss_templates[rss_version]
[210]159            ping_url = url + site_rss_version
160        return ping_url
161
[198]162registerType(PingTool, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.