| 1 | from itertools import ifilter |
|---|
| 2 | from zope.interface import implements |
|---|
| 3 | from zope.component import queryMultiAdapter |
|---|
| 4 | from quintagroup.plonegooglesitemaps.interfaces import IBlackoutFilter |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | class IdBlackoutFilter(object): |
|---|
| 8 | """Filter-out by ID.""" |
|---|
| 9 | |
|---|
| 10 | implements(IBlackoutFilter) |
|---|
| 11 | |
|---|
| 12 | def __init__(self, context, request): |
|---|
| 13 | self.context = context |
|---|
| 14 | self.request = request |
|---|
| 15 | |
|---|
| 16 | def filterOut(self, fdata, fargs): |
|---|
| 17 | """Filter-out fdata list by id in fargs.""" |
|---|
| 18 | return ifilter(lambda b, fa=fargs: (b.getId or b.id) != fargs, |
|---|
| 19 | fdata) |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | class PathBlackoutFilter(object): |
|---|
| 23 | """Filter-out by PATH.""" |
|---|
| 24 | |
|---|
| 25 | implements(IBlackoutFilter) |
|---|
| 26 | |
|---|
| 27 | def __init__(self, context, request): |
|---|
| 28 | self.context = context |
|---|
| 29 | self.request = request |
|---|
| 30 | |
|---|
| 31 | def filterOut(self, fdata, fargs): |
|---|
| 32 | """Filter-out fdata list by path in fargs.""" |
|---|
| 33 | if fargs.startswith("/"): |
|---|
| 34 | # absolute path filter |
|---|
| 35 | portal_id = queryMultiAdapter((self.context, self.request), |
|---|
| 36 | name=u"plone_portal_state" |
|---|
| 37 | ).portal().getId() |
|---|
| 38 | test_path = '/' + portal_id + fargs |
|---|
| 39 | elif fargs.startswith("./"): |
|---|
| 40 | # relative path filter |
|---|
| 41 | container_path = '/'.join(self.context.getPhysicalPath()[:-1]) |
|---|
| 42 | test_path = container_path + fargs[1:] |
|---|
| 43 | else: |
|---|
| 44 | # unrecognized starting point |
|---|
| 45 | return fdata |
|---|
| 46 | |
|---|
| 47 | return ifilter(lambda b, tp=test_path: b.getPath() != tp, |
|---|
| 48 | fdata) |
|---|