1 | import os |
---|
2 | from Acquisition import aq_base |
---|
3 | from zLOG import LOG |
---|
4 | from zope.interface import implements |
---|
5 | from AccessControl import ClassSecurityInfo |
---|
6 | from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
---|
7 | from Products.Archetypes.public import * |
---|
8 | from Products.CMFCore.ActionProviderBase import ActionProviderBase |
---|
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 Products.qPingTool import qPingToolMessageFactory as _ |
---|
17 | from interfaces import IPingTool |
---|
18 | from adapter import ICanonicalURL |
---|
19 | from config import PROJECTNAME |
---|
20 | |
---|
21 | _marker = [] |
---|
22 | |
---|
23 | def 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 | |
---|
32 | class PingTool(ATFolder, PloneFolder): |
---|
33 | """ |
---|
34 | |
---|
35 | >>> IPingTool.implementedBy(PingTool) |
---|
36 | True |
---|
37 | """ |
---|
38 | security = ClassSecurityInfo() |
---|
39 | |
---|
40 | implements(IPingTool) |
---|
41 | __implements__ = (IOrderedContainer,) |
---|
42 | |
---|
43 | archetype_name = portal_type = 'PingTool' |
---|
44 | manage_options = ( |
---|
45 | {'label' : 'Overview', 'action' : 'manage_overview'}, |
---|
46 | ) + ATFolder.manage_options |
---|
47 | |
---|
48 | manage_overview = PageTemplateFile(os.path.join('www', 'overview'), globals(), __name__='manage_overview') |
---|
49 | |
---|
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') |
---|
64 | def pingFeedReader(self,context): |
---|
65 | """ ping """ |
---|
66 | status = 'failed' |
---|
67 | pingProp = self.getPingProperties(context) |
---|
68 | if not pingProp['enable_ping']: |
---|
69 | message = _(u'Ping is dissabled.') |
---|
70 | return status, message |
---|
71 | canonical_url = ICanonicalURL(self).getCanonicalURL() |
---|
72 | if not canonical_url: |
---|
73 | return status, _(u'Ping is impossible.Setup canonical_url.') |
---|
74 | ps = getToolByName(context,'portal_syndication') |
---|
75 | if ps.isSyndicationAllowed(context): |
---|
76 | sites = pingProp['ping_sites'] |
---|
77 | message = _(u'Select servers.') |
---|
78 | for site in sites: |
---|
79 | status = 'success' |
---|
80 | message = _(u'The servers are pinged.') |
---|
81 | site_obj = getattr(self, site) |
---|
82 | site_method = site_obj.getMethod_name() |
---|
83 | site_url = site_obj.getUrl() |
---|
84 | PingMethod = XMLRPCMethod('myid', "", site_url, site_method, 25) |
---|
85 | title = context.Title() |
---|
86 | rss_version = site_obj.getRss_version() |
---|
87 | ping_url = pingProp['ping_'+rss_version] |
---|
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)) |
---|
95 | else: |
---|
96 | message = 'The %s is not syndication allowed' % url |
---|
97 | return status, message |
---|
98 | |
---|
99 | security.declareProtected(ManagePortal, 'setupPing') |
---|
100 | def setupPing(self,context, |
---|
101 | enable_ping=0, |
---|
102 | ping_sites=(), |
---|
103 | ping_Weblog='', |
---|
104 | ping_RSS1='', |
---|
105 | ping_RSS2='', |
---|
106 | REQUEST=None): |
---|
107 | """ """ |
---|
108 | obj=aq_base(context) |
---|
109 | status = 'success' |
---|
110 | message = _(u'Changes saved.') |
---|
111 | syInfo = getattr(obj, 'syndication_information', None) |
---|
112 | |
---|
113 | if syInfo is None: |
---|
114 | message = _(u'Syndication is Disabled') |
---|
115 | status = 'failed' |
---|
116 | else: |
---|
117 | syInfo.ping_sites = list(ping_sites) |
---|
118 | syInfo.enable_ping = enable_ping |
---|
119 | syInfo.ping_Weblog = ping_Weblog |
---|
120 | syInfo.ping_RSS1 = ping_RSS1 |
---|
121 | syInfo.ping_RSS2 = ping_RSS2 |
---|
122 | |
---|
123 | return status, message |
---|
124 | |
---|
125 | security.declareProtected(ManagePortal, 'getPingProperties') |
---|
126 | def getPingProperties(self, context): |
---|
127 | """ """ |
---|
128 | obj=aq_base(context) |
---|
129 | syInfo = getattr(obj, 'syndication_information', None) |
---|
130 | pingProperties={} |
---|
131 | pingProperties['ping_sites'] = getattr(syInfo, 'ping_sites', []) |
---|
132 | pingProperties['enable_ping'] = getattr(syInfo, 'enable_ping', 0) |
---|
133 | |
---|
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'): |
---|
150 | rss_templates = {'Weblog':'','RSS1':'/RSS','RSS2':'/RSS2'} |
---|
151 | url = getToolByName(context, 'portal_url').getRelativeContentURL(context) |
---|
152 | canonical_url = ICanonicalURL(self).getCanonicalURL() |
---|
153 | ping_url = '' |
---|
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] |
---|
159 | ping_url = url + site_rss_version |
---|
160 | return ping_url |
---|
161 | |
---|
162 | registerType(PingTool, PROJECTNAME) |
---|