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

Last change on this file since 412 was 412, checked in by chervol, 18 years ago

merged plone 2.1.x version to trunk

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