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