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