source: products/qPloneGoogleSitemaps/branches/contenttype/content/sitemap.py @ 421

Last change on this file since 421 was 421, checked in by crchemist, 18 years ago

Added plone-2.5 compatibility

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1"""Definition of the Sitemap content type
2"""
3
4from zope.interface import implements, directlyProvides
5
6from Products.Archetypes import atapi
7from Products.ATContentTypes.content import base
8from Products.ATContentTypes.content import schemata
9from Products.CMFCore.utils import getToolByName
10
11from Products.qPloneGoogleSitemaps import qPloneGoogleSitemapsMessageFactory as _
12from Products.qPloneGoogleSitemaps.interfaces import ISitemap
13from Products.qPloneGoogleSitemaps.config import * 
14
15SitemapSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((
16
17    # -*- Your Archetypes field definitions here ... -*-
18    atapi.StringField(
19        name='sitemapType',
20        storage = atapi.AnnotationStorage(),
21        required=True,
22        default='content',
23        vocabulary=SITEMAPS_VIEW_MAP.keys(),
24        widget=atapi.SelectionWidget(
25            label=_(u"Sitemap type"),
26            description=_(u"Select Type of the sitemap."),
27        ),
28    ),
29    atapi.LinesField(
30        name='portalTypes',
31        storage = atapi.AnnotationStorage(),
32        required=True,
33        default=['Document',],
34        vocabulary="availablePortalTypes",
35        #schemata ='default',
36        widget=atapi.MultiSelectionWidget(
37            label=_(u"Define the types"),
38            description=_(u"Define the types to be included in sitemap."),
39        ),
40    ),
41    atapi.LinesField(
42        name='states',
43        storage = atapi.AnnotationStorage(),
44        required=True,
45        default=['published',],
46        vocabulary="getWorkflowStates",
47        #schemata ='default',
48        widget=atapi.MultiSelectionWidget(
49            label=_(u"Review status"),
50            description=_(u"You may include items in sitemap depend of their " \
51                          u"review state."),
52        ),
53    ),
54    atapi.LinesField(
55        name='blackout_list',
56        storage = atapi.AnnotationStorage(),
57        required=False,
58        #default='',
59        #schemata ='default',
60        widget=atapi.LinesWidget(
61            label=_(u"Blackout entries"),
62            description=_(u"The objects with the given ids will not be " \
63                          u"included in sitemap."),
64        ),
65    ),
66    atapi.LinesField(
67        name='reg_exp',
68        storage = atapi.AnnotationStorage(),
69        required=False,
70        #default='',
71        #schemata ='default',
72        widget=atapi.LinesWidget(
73            label=_(u"URL processing Regular Expressions"),
74            description=_(u"Provide regular expressions (in Perl syntax), " \
75                          u"one per line to be applied to URLs before " \
76                          u"including them into Sitemap. For instance, " \
77                          u"\"s/\/index_html//\" will remove /index_html " \
78                          u"from URLs representing default documents."),
79        ),
80    ),
81    atapi.LinesField(
82        name='urls',
83        storage = atapi.AnnotationStorage(),
84        required=False,
85        #default='',
86        #schemata ='default',
87        widget=atapi.LinesWidget(
88            label=_(u"Additional URLs"),
89            description=_(u"Define additional URLs that are not objects and " \
90                          u"that should be included in sitemap."),
91        ),
92    ),
93    atapi.StringField(
94        name='verificationFilename',
95        storage = atapi.AnnotationStorage(),
96        required=False,
97        #default='',
98        #schemata ='default',
99        widget=atapi.StringWidget(
100            label=_(u"Provide verification file name"),
101            description=_(u"Default verification file name for this sitemaps"),
102        ),
103    ),
104    atapi.LinesField(
105        name='pingTransitions',
106        storage = atapi.AnnotationStorage(),
107        required=False,
108        default='content',
109        vocabulary='getWorkflowTransitions',
110        schemata="pinging",
111        widget=atapi.MultiSelectionWidget(
112            label=_(u"Pinging workflow transitions"),
113            description=_(u"Select workflow transitions for pinging google on."),
114        ),
115    ),
116
117))
118
119# Set storage on fields copied from ATContentTypeSchema, making sure
120# they work well with the python bridge properties.
121
122SitemapSchema['id'].widget.ignore_visible_ids = True
123SitemapSchema['title'].storage = atapi.AnnotationStorage()
124SitemapSchema['title'].required=False
125SitemapSchema['title'].widget.visible = {'edit':'invisible', 'view':'invisible'}
126SitemapSchema['description'].storage = atapi.AnnotationStorage()
127SitemapSchema['description'].widget.visible = {'edit':'invisible', 'view':'invisible'}
128
129schemata.finalizeATCTSchema(SitemapSchema, moveDiscussion=False)
130SitemapSchema['relatedItems'].schemata='metadata'
131SitemapSchema['relatedItems'].widget.visible = {'edit':'invisible', 'view':'invisible'}
132
133class Sitemap(base.ATCTContent):
134    """Search engine Sitemap content type"""
135    implements(ISitemap)
136
137    portal_type = "Sitemap"
138    schema = SitemapSchema
139
140    #title = atapi.ATFieldProperty('title')
141    #description = atapi.ATFieldProperty('description')
142
143    def at_post_create_script(self):
144        # Set default layout on creation
145        default_layout = SITEMAPS_VIEW_MAP[self.getSitemapType()]
146        self._setProperty('layout', default_layout)
147
148    def availablePortalTypes(self):
149        pt = getToolByName(self, 'portal_types')
150        types = pt.listContentTypes()
151        return atapi.DisplayList(zip(types,types))
152
153    def getWorkflowStates(self):
154        pw = getToolByName(self,'portal_workflow')
155        states = list(set([v for k,v in pw.listWFStatesByTitle()]))
156        states.sort()
157        return atapi.DisplayList(zip(states, states))
158
159    def getWorkflowTransitions(self):
160        wf_trans = []
161        pw = getToolByName(self,'portal_workflow')
162        for wf_id in pw.getWorkflowIds():
163            wf = pw.getWorkflowById(wf_id)
164            if not wf:
165                continue
166            for wf_tr in wf.transitions.values():
167                if wf_tr.after_script_name in AVAILABLE_WF_SCRIPTS:
168                    wf_trans.append(("%s#%s" % (wf_id,wf_tr.id),
169                        "%s : %s (%s)" % (wf_id,wf_tr.id,wf_tr.title_or_id())))
170        return atapi.DisplayList(wf_trans)
171
172    def setPingTransitions(self, value, **kw):
173        """Add 'Ping sitemap' afterscript for selected workflow transitions.
174        """
175        self.getField('pingTransitions').set(self, value)
176        if not IS_PLONE_3:
177            # Update Workflow if needed
178            pw = getToolByName(self, 'portal_workflow')
179            #ping_googlesitemap = PING_EMETHODS_MAP[self.getSitemapType()]
180            transmap = {}
181            for key in value:
182                if key.find('#')>0:
183                    ids = key.split('#')
184                    wfid = ids[0]
185                    if not wfid in transmap.keys():
186                        transmap[wfid]=[]
187                    transmap[wfid].append(ids[1])
188            for wfid in transmap.keys():
189                workflow = pw.getWorkflowById(wfid)
190                if ping_googlesitemap not in workflow.scripts.objectIds():
191                    workflow.scripts.manage_addProduct['ExternalMethod'].manage_addExternalMethod(
192                        ping_googlesitemap,
193                        'Ping sitemap',
194                        'qPloneGoogleSitemaps.ping_googlesitemap',
195                        ping_googlesitemap)
196                transitions_set = transmap[wfid]
197                for transition in workflow.transitions.values():
198                    trid = transition.id
199                    tras = transition.after_script_name
200                    if (tras == '') and (trid in transitions_set):
201                        #set
202                        after_script = ping_googlesitemap
203                    elif (tras == ping_googlesitemap) and not (trid in transitions_set):
204                        #reset
205                        after_script = ''
206                    else:
207                        #avoid properties set
208                        continue
209                    transition.setProperties(title=transition.title,
210                        new_state_id=transition.new_state_id,
211                        after_script_name=after_script,
212                        actbox_name=transition.actbox_name)
213
214atapi.registerType(Sitemap, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.