1 | from zope.interface import implements, Interface, Attribute |
---|
2 | from zope.component import queryMultiAdapter |
---|
3 | |
---|
4 | from Acquisition import aq_inner, aq_parent |
---|
5 | from Products.Five import BrowserView |
---|
6 | from Products.CMFCore.utils import getToolByName |
---|
7 | |
---|
8 | #from quintagroup.plonegooglesitemaps \ |
---|
9 | # import qPloneGoogleSitemapsMessageFactory as _ |
---|
10 | from quintagroup.plonegooglesitemaps.interfaces import IBlackoutFilter |
---|
11 | from quintagroup.plonegooglesitemaps.browser.utils import additionalURLs, \ |
---|
12 | applyOperations |
---|
13 | |
---|
14 | |
---|
15 | class ISitemapView(Interface): |
---|
16 | """ |
---|
17 | Sitemap view interface |
---|
18 | """ |
---|
19 | |
---|
20 | def results(): |
---|
21 | """ Return list of dictionary objects |
---|
22 | which confirm Sitemap conditions |
---|
23 | """ |
---|
24 | |
---|
25 | def getAdditionalURLs(): |
---|
26 | """ Return additional URL list |
---|
27 | """ |
---|
28 | |
---|
29 | def updateRequest(): |
---|
30 | """ Add compression header to RESPONSE |
---|
31 | if allowed |
---|
32 | """ |
---|
33 | |
---|
34 | numEntries = Attribute("Return number of entries") |
---|
35 | |
---|
36 | |
---|
37 | class CommonSitemapView(BrowserView): |
---|
38 | """ |
---|
39 | Sitemap browser view |
---|
40 | """ |
---|
41 | implements(ISitemapView) |
---|
42 | |
---|
43 | # key, function map for extend return results |
---|
44 | # with mapping data |
---|
45 | additional_maps = () |
---|
46 | |
---|
47 | def __init__(self, context, request): |
---|
48 | self.context = context |
---|
49 | self.request = request |
---|
50 | |
---|
51 | @property |
---|
52 | def portal_catalog(self): |
---|
53 | return getToolByName(self.context, 'portal_catalog') |
---|
54 | |
---|
55 | @property |
---|
56 | def portal(self): |
---|
57 | return getToolByName(self.context, 'portal_url').getPortalObject() |
---|
58 | |
---|
59 | @property |
---|
60 | def search_path(self): |
---|
61 | return '/'.join(aq_parent(aq_inner(self.context)).getPhysicalPath()) |
---|
62 | |
---|
63 | def getFilteredObjects(self): |
---|
64 | """ Return brains |
---|
65 | """ |
---|
66 | return [] |
---|
67 | |
---|
68 | def results(self): |
---|
69 | """ Prepare mapping for template |
---|
70 | """ |
---|
71 | result = [] |
---|
72 | objects = self.getFilteredObjects() |
---|
73 | reg_exps = self.context.getReg_exp() |
---|
74 | |
---|
75 | brain_url_map = applyOperations(self.getBOFiltered(objects), reg_exps) |
---|
76 | # Prepare dictionary for view |
---|
77 | for url, b in brain_url_map.items(): |
---|
78 | res_map = {'url': url, } |
---|
79 | [res_map.update({k: f(b)}) for k, f in self.additional_maps] |
---|
80 | result.append(res_map) |
---|
81 | self.num_entries = len(result) |
---|
82 | return result |
---|
83 | |
---|
84 | def getBOFiltered(self, objects): |
---|
85 | """Return black-out filtered objects |
---|
86 | Every record in blackout_list filter should follow the spec: |
---|
87 | [<filter name>:]<filter arguments> |
---|
88 | For example: |
---|
89 | 1| index.html |
---|
90 | 2| id:index.html |
---|
91 | 3| path:/folder_1_level/obj_in_folder |
---|
92 | 4| path:./folder_near_sitemap/obj_in_folder |
---|
93 | 5| foo_filter:arg-1, arg-2 |
---|
94 | |
---|
95 | 1->used default "id" filter - remove "index.html" objects; |
---|
96 | 2->explicit "id" filter - remove "index.html" objects; |
---|
97 | 3->"path" filter - remove /folder_1_level/obj_in_folder object, |
---|
98 | path from the root of the plone site; |
---|
99 | 4->same to 3), but path get from the folder, where sitemap is |
---|
100 | located; |
---|
101 | 5->filter name is "foo_filter" (must be registered IBlackoutFilter, |
---|
102 | named "foo_filter"), which get filter arguments: arg-1, arg-2 |
---|
103 | |
---|
104 | Detailed explanation look in filters.txt doctest. |
---|
105 | """ |
---|
106 | blackout_list = self.context.getBlackout_list() |
---|
107 | for frec in blackout_list: |
---|
108 | fspec = frec.split(":", 1) |
---|
109 | fargs = fspec.pop() |
---|
110 | fname = fspec and fspec.pop() or "id" |
---|
111 | fengine = queryMultiAdapter((self.context, self.request), |
---|
112 | interface=IBlackoutFilter, name=fname) |
---|
113 | if fengine: |
---|
114 | objects = list(fengine.filterOut(objects, fargs)) |
---|
115 | return objects |
---|
116 | |
---|
117 | def updateRequest(self): |
---|
118 | self.request.RESPONSE.setHeader('Content-Type', 'text/xml') |
---|
119 | try: |
---|
120 | compression = self.context.enableHTTPCompression() |
---|
121 | if compression: |
---|
122 | compression(request=self.request) |
---|
123 | except: |
---|
124 | pass |
---|
125 | |
---|
126 | def getAdditionalURLs(self): |
---|
127 | return additionalURLs(self.context) |
---|
128 | |
---|
129 | @property |
---|
130 | def numEntries(self): |
---|
131 | return len(self.results()) + len(self.getAdditionalURLs()) |
---|