1 | import os |
---|
2 | from Acquisition import aq_base |
---|
3 | from zLOG import LOG |
---|
4 | from zope.interface import implements |
---|
5 | from zope.component import getAdapter |
---|
6 | from AccessControl import ClassSecurityInfo |
---|
7 | from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
---|
8 | from Products.Archetypes.public import * |
---|
9 | from Products.CMFCore.permissions import ManagePortal |
---|
10 | from Products.CMFCore.utils import getToolByName |
---|
11 | from Products.ATContentTypes.content.folder import ATFolder |
---|
12 | from Products.CMFPlone.interfaces.OrderedContainer import IOrderedContainer |
---|
13 | from Products.CMFPlone.PloneFolder import PloneFolder |
---|
14 | from Products.XMLRPCMethod.XMLRPCMethod import RPCThread, XMLRPCMethod |
---|
15 | |
---|
16 | from quintagroup.pingtool import PingToolMessageFactory as _ |
---|
17 | from interfaces import IPingTool, ICanonicalURL |
---|
18 | from config import PROJECTNAME |
---|
19 | |
---|
20 | |
---|
21 | class PingTool(ATFolder, PloneFolder): |
---|
22 | """ |
---|
23 | |
---|
24 | >>> IPingTool.implementedBy(PingTool) |
---|
25 | True |
---|
26 | """ |
---|
27 | security = ClassSecurityInfo() |
---|
28 | |
---|
29 | implements(IPingTool) |
---|
30 | __implements__ = (IOrderedContainer,) |
---|
31 | |
---|
32 | archetype_name = portal_type = 'PingTool' |
---|
33 | manage_options = ( |
---|
34 | {'label' : 'Overview', 'action' : 'manage_overview'}, |
---|
35 | ) + ATFolder.manage_options |
---|
36 | |
---|
37 | manage_overview = PageTemplateFile(os.path.join('www', 'overview'), globals(), __name__='manage_overview') |
---|
38 | |
---|
39 | def om_icons(self): |
---|
40 | """ Checking on ZMI for canonical_url setting.""" |
---|
41 | icons = ({'path':'misc_/%s/tool.gif' % PROJECTNAME \ |
---|
42 | ,'alt':self.meta_type \ |
---|
43 | ,'title':self.meta_type \ |
---|
44 | },) |
---|
45 | if not getAdapter(self, ICanonicalURL, 'canonical_url_adapter').getCanonicalURL(): |
---|
46 | icons = icons + ({'path':'misc_/PageTemplates/exclamation.gif' \ |
---|
47 | ,'alt':'Error' \ |
---|
48 | ,'title':'PingTool needs setting canonical_url' \ |
---|
49 | },) |
---|
50 | return icons |
---|
51 | |
---|
52 | security.declareProtected(ManagePortal, 'pingFeedReader') |
---|
53 | def pingFeedReader(self,context): |
---|
54 | """ ping """ |
---|
55 | message = {'portal_message': '', 'return_message': ''} |
---|
56 | status = 'failed' |
---|
57 | pingProp = self.getPingProperties(context) |
---|
58 | if not pingProp['enable_ping']: |
---|
59 | message['portal_message'] = _(u'Ping is dissabled.') |
---|
60 | return status, message |
---|
61 | canonical_url = getAdapter(self, ICanonicalURL, 'canonical_url_adapter').getCanonicalURL() |
---|
62 | if not canonical_url: |
---|
63 | message['portal_message'] = _(u'Ping is impossible.Setup canonical_url.') |
---|
64 | return status, message |
---|
65 | sites = pingProp['ping_sites'] |
---|
66 | message['portal_message'] = _(u'Select servers.') |
---|
67 | for site in sites: |
---|
68 | status = 'success' |
---|
69 | message['portal_message'] = _(u'The servers are pinged.') |
---|
70 | site_obj = getattr(self, site) |
---|
71 | site_method = site_obj.getMethod_name() |
---|
72 | site_url = site_obj.getUrl() |
---|
73 | PingMethod = XMLRPCMethod('myid', "", site_url, site_method, 25) |
---|
74 | title = context.Title() |
---|
75 | rss_version = site_obj.getRss_version() |
---|
76 | ping_url = pingProp['ping_' + rss_version] |
---|
77 | try: |
---|
78 | severities = 0 |
---|
79 | result_ping = PingMethod(title, ping_url) |
---|
80 | result = ', '.join([':'.join((k, str(v))) for k, v in result_ping.items()]) |
---|
81 | except: |
---|
82 | severities = 100 |
---|
83 | result = 'The site %s generated error for %s.' % (site_url, ping_url) |
---|
84 | LOG(PROJECTNAME, severities, result) |
---|
85 | message['return_message'] += '\nReturned message from %s: %s' % (site_url, str(result)) |
---|
86 | return status, message |
---|
87 | |
---|
88 | security.declareProtected(ManagePortal, 'getPingProperties') |
---|
89 | def getPingProperties(self, context): |
---|
90 | """ """ |
---|
91 | obj=context |
---|
92 | pingProperties={} |
---|
93 | pingProperties['ping_sites'] = obj.getField('ping_sites').getAccessor(obj)() |
---|
94 | pingProperties['enable_ping'] = obj.getField('enable_ping').getAccessor(obj)() |
---|
95 | pingProperties['ping_Weblog'] = obj.getField('ping_Weblog').getAccessor(obj)() |
---|
96 | pingProperties['ping_RSS'] = obj.getField('ping_RSS').getAccessor(obj)() |
---|
97 | pingProperties['ping_RSS2'] = obj.getField('ping_RSS2').getAccessor(obj)() |
---|
98 | return pingProperties |
---|
99 | |
---|
100 | registerType(PingTool, PROJECTNAME) |
---|