source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/content/sitemap.py @ 2406

Last change on this file since 2406 was 2406, checked in by mylan, 14 years ago

#131: Refuse from configurable schema extending for portal types in the trunk version.

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