source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/browser/commonview.py @ 3152

Last change on this file since 3152 was 3152, checked in by zidane, 9 years ago

fixes pep8

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