[1593] | 1 | from string import find |
---|
| 2 | from zope.interface import implements, Interface, Attribute |
---|
[3002] | 3 | from zope.component import queryMultiAdapter |
---|
[1593] | 4 | |
---|
[2742] | 5 | from Acquisition import aq_inner, aq_parent |
---|
[1593] | 6 | from Products.Five import BrowserView |
---|
| 7 | from Products.CMFCore.utils import getToolByName |
---|
| 8 | |
---|
[3152] | 9 | from quintagroup.plonegooglesitemaps \ |
---|
| 10 | import qPloneGoogleSitemapsMessageFactory as _ |
---|
[3002] | 11 | from quintagroup.plonegooglesitemaps.interfaces import IBlackoutFilter |
---|
[3152] | 12 | from quintagroup.plonegooglesitemaps.browser.utils import additionalURLs, \ |
---|
| 13 | applyOperations |
---|
[1593] | 14 | |
---|
| 15 | |
---|
| 16 | class 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 | |
---|
[3152] | 37 | |
---|
[1593] | 38 | class 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 | |
---|
[2742] | 60 | @property |
---|
| 61 | def search_path(self): |
---|
| 62 | return '/'.join(aq_parent(aq_inner(self.context)).getPhysicalPath()) |
---|
| 63 | |
---|
[1593] | 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 | |
---|
[3002] | 76 | brain_url_map = applyOperations(self.getBOFiltered(objects), reg_exps) |
---|
[1593] | 77 | # Prepare dictionary for view |
---|
| 78 | for url, b in brain_url_map.items(): |
---|
[3152] | 79 | res_map = {'url': url, } |
---|
| 80 | [res_map.update({k: f(b)}) for k, f in self.additional_maps] |
---|
[1593] | 81 | result.append(res_map) |
---|
| 82 | self.num_entries = len(result) |
---|
| 83 | return result |
---|
| 84 | |
---|
[3002] | 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 |
---|
[3152] | 95 | |
---|
[3002] | 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; |
---|
[3152] | 100 | 4->same to 3), but path get from the folder, where sitemap is |
---|
| 101 | located; |
---|
[3002] | 102 | 5->filter name is "foo_filter" (must be registered IBlackoutFilter, |
---|
| 103 | named "foo_filter"), which get filter arguments: arg-1, arg-2 |
---|
[3152] | 104 | |
---|
[3002] | 105 | Detailed explanation look in filters.txt doctest. |
---|
[3152] | 106 | """ |
---|
[3002] | 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 | |
---|
[1593] | 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()) |
---|