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