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

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

Add content from qPloneGoogleSitemaps. Fix all imports

  • Property svn:eol-style set to native
File size: 7.8 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 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',],
35        vocabulary="availablePortalTypes",
36        #schemata ='default',
37        widget=atapi.MultiSelectionWidget(
38            label=_(u"Define the types"),
39            description=_(u"Define the types to be included in sitemap."),
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 availablePortalTypes(self):
138        pt = getToolByName(self, 'portal_types')
139        types = pt.listContentTypes()
140        return atapi.DisplayList(zip(types,types))
141
142    def getWorkflowStates(self):
143        pw = getToolByName(self,'portal_workflow')
144        states = list(set([v for k,v in pw.listWFStatesByTitle()]))
145        states.sort()
146        return atapi.DisplayList(zip(states, states))
147
148    def getWorkflowTransitions(self):
149        wf_trans = []
150        pw = getToolByName(self,'portal_workflow')
151        for wf_id in pw.getWorkflowIds():
152            wf = pw.getWorkflowById(wf_id)
153            if not wf:
154                continue
155            for wf_tr in wf.transitions.values():
156                if wf_tr.after_script_name in AVAILABLE_WF_SCRIPTS:
157                    wf_trans.append(("%s#%s" % (wf_id,wf_tr.id),
158                        "%s : %s (%s)" % (wf_id,wf_tr.id,wf_tr.title_or_id())))
159        return atapi.DisplayList(wf_trans)
160
161    def setPingTransitions(self, value, **kw):
162        """Add 'Ping sitemap' afterscript for selected workflow transitions.
163        """
164        self.getField('pingTransitions').set(self, value)
165        if not IS_PLONE_3:
166            # Update Workflow if needed
167            pw = getToolByName(self, 'portal_workflow')
168            #ping_googlesitemap = PING_EMETHODS_MAP[self.getSitemapType()]
169            transmap = {}
170            for key in value:
171                if key.find('#')>0:
172                    ids = key.split('#')
173                    wfid = ids[0]
174                    if not wfid in transmap.keys():
175                        transmap[wfid]=[]
176                    transmap[wfid].append(ids[1])
177            for wfid in transmap.keys():
178                workflow = pw.getWorkflowById(wfid)
179                if ping_googlesitemap not in workflow.scripts.objectIds():
180                    workflow.scripts.manage_addProduct['ExternalMethod'].manage_addExternalMethod(
181                        ping_googlesitemap,
182                        'Ping sitemap',
183                        'quintagroup.plonegooglesitemaps.ping_googlesitemap',
184                        ping_googlesitemap)
185                transitions_set = transmap[wfid]
186                for transition in workflow.transitions.values():
187                    trid = transition.id
188                    tras = transition.after_script_name
189                    if (tras == '') and (trid in transitions_set):
190                        #set
191                        after_script = ping_googlesitemap
192                    elif (tras == ping_googlesitemap) and not (trid in transitions_set):
193                        #reset
194                        after_script = ''
195                    else:
196                        #avoid properties set
197                        continue
198                    transition.setProperties(title=transition.title,
199                        new_state_id=transition.new_state_id,
200                        after_script_name=after_script,
201                        actbox_name=transition.actbox_name)
202
203atapi.registerType(Sitemap, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.