1 | from string import find |
---|
2 | from zope.interface import implements, Interface, Attribute |
---|
3 | |
---|
4 | from Products.Five import BrowserView |
---|
5 | from Products.CMFCore.utils import getToolByName |
---|
6 | |
---|
7 | from Products.qPloneGoogleSitemaps import qPloneGoogleSitemapsMessageFactory as _ |
---|
8 | from utils import additionalURLs, applyOperations |
---|
9 | |
---|
10 | |
---|
11 | class ISitemapView(Interface): |
---|
12 | """ |
---|
13 | Sitemap view interface |
---|
14 | """ |
---|
15 | |
---|
16 | def results(): |
---|
17 | """ Return list of dictionary objects |
---|
18 | which confirm Sitemap conditions |
---|
19 | """ |
---|
20 | |
---|
21 | def getAdditionalURLs(): |
---|
22 | """ Return additional URL list |
---|
23 | """ |
---|
24 | |
---|
25 | def updateRequest(): |
---|
26 | """ Add compression header to RESPONSE |
---|
27 | if allowed |
---|
28 | """ |
---|
29 | |
---|
30 | numEntries = Attribute("Return number of entries") |
---|
31 | |
---|
32 | class CommonSitemapView(BrowserView): |
---|
33 | """ |
---|
34 | Sitemap browser view |
---|
35 | """ |
---|
36 | implements(ISitemapView) |
---|
37 | |
---|
38 | # key, function map for extend return results |
---|
39 | # with mapping data |
---|
40 | additional_maps = () |
---|
41 | |
---|
42 | |
---|
43 | def __init__(self, context, request): |
---|
44 | self.context = context |
---|
45 | self.request = request |
---|
46 | |
---|
47 | @property |
---|
48 | def portal_catalog(self): |
---|
49 | return getToolByName(self.context, 'portal_catalog') |
---|
50 | |
---|
51 | @property |
---|
52 | def portal(self): |
---|
53 | return getToolByName(self.context, 'portal_url').getPortalObject() |
---|
54 | |
---|
55 | def getFilteredObjects(self): |
---|
56 | """ Return brains |
---|
57 | """ |
---|
58 | return [] |
---|
59 | |
---|
60 | def results(self): |
---|
61 | """ Prepare mapping for template |
---|
62 | """ |
---|
63 | result = [] |
---|
64 | objects = self.getFilteredObjects() |
---|
65 | blackout_list = self.context.getBlackout_list() |
---|
66 | reg_exps = self.context.getReg_exp() |
---|
67 | |
---|
68 | brain_url_map = applyOperations([ob for ob in objects |
---|
69 | if (ob.getId not in blackout_list)], |
---|
70 | reg_exps) |
---|
71 | |
---|
72 | purl = self.portal.absolute_url() |
---|
73 | # Prepare dictionary for view |
---|
74 | for url, b in brain_url_map.items(): |
---|
75 | res_map = {'url' : purl+url,} |
---|
76 | [res_map.update({k : f(b)}) for k, f in self.additional_maps] |
---|
77 | result.append(res_map) |
---|
78 | self.num_entries = len(result) |
---|
79 | return result |
---|
80 | |
---|
81 | def updateRequest(self): |
---|
82 | self.request.RESPONSE.setHeader('Content-Type', 'text/xml') |
---|
83 | try: |
---|
84 | compression = self.context.enableHTTPCompression() |
---|
85 | if compression: |
---|
86 | compression(request=self.request) |
---|
87 | except: |
---|
88 | pass |
---|
89 | |
---|
90 | def getAdditionalURLs(self): |
---|
91 | return additionalURLs(self.context) |
---|
92 | |
---|
93 | @property |
---|
94 | def numEntries(self): |
---|
95 | return len(self.results()) + len(self.getAdditionalURLs()) |
---|