1 | from zope.component import getAdapter |
---|
2 | from Products.Archetypes.public import * |
---|
3 | from Products.CMFCore.utils import getToolByName |
---|
4 | |
---|
5 | from archetypes.schemaextender.field import ExtensionField |
---|
6 | |
---|
7 | from quintagroup.pingtool import PingToolMessageFactory as _ |
---|
8 | from interfaces import ICanonicalURL |
---|
9 | |
---|
10 | def getPingDefaultUrl(context, rss_version='Weblog'): |
---|
11 | rss_templates = {'ping_Weblog':'', 'ping_RSS':'/RSS', 'ping_RSS2':'/RSS2'} |
---|
12 | url = getToolByName(context, 'portal_url').getRelativeContentURL(context) |
---|
13 | portal_pingtool = getToolByName(context, 'portal_pingtool', None) |
---|
14 | ping_url = '' |
---|
15 | if portal_pingtool: |
---|
16 | canonical_url = getAdapter(portal_pingtool, ICanonicalURL, 'canonical_url_adapter').getCanonicalURL() |
---|
17 | if canonical_url: |
---|
18 | if not canonical_url.endswith('/'): |
---|
19 | canonical_url += '/' |
---|
20 | if not canonical_url.startswith('http://'): |
---|
21 | canonical_url = 'http://' + canonical_url |
---|
22 | url = canonical_url + url |
---|
23 | site_rss_version = rss_templates[rss_version] |
---|
24 | ping_url = url + site_rss_version |
---|
25 | return ping_url |
---|
26 | |
---|
27 | |
---|
28 | class MyLinesField(ExtensionField, LinesField): |
---|
29 | """A trivial lines field.""" |
---|
30 | |
---|
31 | class MyStringField(ExtensionField, StringField): |
---|
32 | """The string field with custom 'get' method.""" |
---|
33 | |
---|
34 | def get(self, instance, **kwargs): |
---|
35 | value = super(MyStringField, self).get(instance) |
---|
36 | if not value: |
---|
37 | value = getPingDefaultUrl(instance, self.__name__) |
---|
38 | return value |
---|
39 | |
---|
40 | class MyBooleanField(ExtensionField, BooleanField): |
---|
41 | """A trivial boolean field.""" |
---|
42 | |
---|
43 | |
---|
44 | class PingToolExtender(object): |
---|
45 | """ PingToolExtender custom field |
---|
46 | """ |
---|
47 | |
---|
48 | fields = [ |
---|
49 | MyBooleanField('enable_ping', |
---|
50 | default=0, |
---|
51 | schemata='PingSetup', |
---|
52 | widget=BooleanWidget(label=_(u'label_enable_ping', default=u'Enable Ping'), |
---|
53 | description=_(u'help_enable_ping', |
---|
54 | default=u''))), |
---|
55 | MyLinesField('ping_sites', |
---|
56 | schemata='PingSetup', |
---|
57 | multiValued=True, |
---|
58 | vocabulary_factory=u'quintagroup.pingtool.getPingSites', |
---|
59 | enforceVocabulary=True, |
---|
60 | size=10, |
---|
61 | widget=MultiSelectionWidget(format='checkbox', |
---|
62 | size=10, |
---|
63 | label=_(u'label_ping_sites', default=u'Ping Sites'), |
---|
64 | description=_(u'help_ping_sites', |
---|
65 | default=u'List of ping sites.'))), |
---|
66 | MyStringField('ping_Weblog', |
---|
67 | schemata='PingSetup', |
---|
68 | widget=StringWidget(label=_(u'label_weblog_rssversion', default=u'Ping url for Weblog'), |
---|
69 | description=_(u'help_weblog_rssversion', |
---|
70 | default=u'RSS version.'))), |
---|
71 | MyStringField('ping_RSS', |
---|
72 | schemata='PingSetup', |
---|
73 | widget=StringWidget(label=_(u'label_rss1_rssversion', default=u'Ping url for RSS'), |
---|
74 | description=_(u'help_rss1_rssversion', |
---|
75 | default=u'RSS version.'))), |
---|
76 | MyStringField('ping_RSS2', |
---|
77 | schemata='PingSetup', |
---|
78 | widget=StringWidget(label=_(u'label_rss2_rssversion', default=u'Ping url for RSS2'), |
---|
79 | description=_(u'help_rss2_rssversion', |
---|
80 | default=u'RSS version.'))), |
---|
81 | ] |
---|
82 | |
---|
83 | def __init__(self, context): |
---|
84 | self.context = context |
---|
85 | |
---|
86 | def getFields(self): |
---|
87 | return self.fields |
---|