[1593] | 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 quintagroup.plonegooglesitemaps 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 | # Prepare dictionary for view |
---|
| 73 | for url, b in brain_url_map.items(): |
---|
[2169] | 74 | res_map = {'url' : url,} |
---|
[1593] | 75 | [res_map.update({k : f(b)}) for k, f in self.additional_maps] |
---|
| 76 | result.append(res_map) |
---|
| 77 | self.num_entries = len(result) |
---|
| 78 | return result |
---|
| 79 | |
---|
| 80 | def updateRequest(self): |
---|
| 81 | self.request.RESPONSE.setHeader('Content-Type', 'text/xml') |
---|
| 82 | try: |
---|
| 83 | compression = self.context.enableHTTPCompression() |
---|
| 84 | if compression: |
---|
| 85 | compression(request=self.request) |
---|
| 86 | except: |
---|
| 87 | pass |
---|
| 88 | |
---|
| 89 | def getAdditionalURLs(self): |
---|
| 90 | return additionalURLs(self.context) |
---|
| 91 | |
---|
| 92 | @property |
---|
| 93 | def numEntries(self): |
---|
| 94 | return len(self.results()) + len(self.getAdditionalURLs()) |
---|