def test_item_can_be_added_to_blacklist(self): config = IConfig(self.portal) config.setPathBlacklist(PersistentList()) # The blacklist is empty by default. self.assertEqual(0, len(config.getPathBlacklist())) # Items can be added to the blacklist. config.appendPathToBlacklist('hans') self.assertIn('hans', config.getPathBlacklist()) self.assertEqual(1, len(config.getPathBlacklist()))
def test_item_can_be_added_to_blacklist(self): config = IConfig(self.portal) config.setPathBlacklist(PersistentList()) # The blacklist is empty by default. self.assertEqual(0, len(config.getPathBlacklist())) # Items can be added to the blacklist. config.appendPathToBlacklist('hans') self.assertIn( 'hans', config.getPathBlacklist() ) self.assertEqual(1, len(config.getPathBlacklist()))
def is_blacklisted(self, context=None, path=None): """ Checks if the adapter the context, the given `context` or the given `path` is blacklisted. """ if context and path: raise ValueError( 'Only one of `context` and `path` can be checked at once.') elif not context and not path: context = self.context elif not path and type(context) in (str, unicode): path = context context = None if not path and isinstance(context, CatalogBrains.AbstractCatalogBrain): # context is a brain path = context.getPath() if not path: path = '/'.join(context.getPhysicalPath()) path = path.strip() if path.endswith('/'): path = path[:-1] # check the path config = IConfig(self.portal) for blocked_path in config.getPathBlacklist(): blocked_path = blocked_path.strip() if path == blocked_path: return True if blocked_path.endswith('*') and \ path.startswith(blocked_path[:-1]): if path == blocked_path[:-1]: return True elif blocked_path[-2] != '/' and \ path[len(blocked_path) - 1] == '/': return False else: return True return False
def is_blacklisted(self, context=None, path=None): """ Checks if the adapter the context, the given `context` or the given `path` is blacklisted. """ if context and path: raise ValueError('Only one of `context` and `path` can be checked at once.') elif not context and not path: context = self.context elif not path and type(context) in (str, unicode): path = context context = None if not path and isinstance(context, CatalogBrains.AbstractCatalogBrain): # context is a brain path = context.getPath() if not path: path = '/'.join(context.getPhysicalPath()) path = path.strip() if path.endswith('/'): path = path[:-1] # check the path config = IConfig(self.portal) for blocked_path in config.getPathBlacklist(): blocked_path = blocked_path.strip() if path == blocked_path: return True if blocked_path.endswith('*') and \ path.startswith(blocked_path[:-1]): if path == blocked_path[:-1]: return True elif blocked_path[-2] != '/' and \ path[len(blocked_path) - 1] == '/': return False else: return True return False
class PathBlacklistView(BrowserView): def __init__(self, *args, **kwargs): super(PathBlacklistView, self).__init__(*args, **kwargs) self.portal = self.context.portal_url.getPortalObject() self.config = IConfig(self.portal) def __call__(self, *args, **kwargs): delete = self.request.get('delete', None) if delete: if self.config.removePathFromBlacklist(delete): msg = _(u'info_path_removed', default=u'Removed path ${path} from blacklist', mapping={'path': delete}) IStatusMessage(self.request).addStatusMessage(msg, type='info') return self.request.RESPONSE.redirect( './@@publisher-config-blacklist') return super(PathBlacklistView, self).__call__(*args, **kwargs) def render_table(self): generator = getUtility(ITableGenerator, 'ftw.tablegenerator') return generator.generate(self._table_rows(), self._table_columns()) def render_add_form(self): z2.switch_on(self) form = AddPathForm(self.context, self.request) return form() def _table_rows(self): for path in self.config.getPathBlacklist(): yield { 'Path': path, '': '<a href="./@@publisher-config-blacklist?delete=%s">Delete</a>' % \ path, } def _table_columns(self): return ('Path', '')
class PathBlacklistView(BrowserView): def __init__(self, *args, **kwargs): super(PathBlacklistView, self).__init__(*args, **kwargs) self.portal = self.context.portal_url.getPortalObject() self.config = IConfig(self.portal) def __call__(self, *args, **kwargs): delete = self.request.get('delete', None) if delete: if self.config.removePathFromBlacklist(delete): msg = _(u'info_path_removed', default=u'Removed path ${path} from blacklist', mapping={'path': delete}) IStatusMessage(self.request).addStatusMessage(msg, type='info') return self.request.RESPONSE.redirect('./@@publisher-config-blacklist') return super(PathBlacklistView, self).__call__(*args, **kwargs) def render_table(self): generator = getUtility(ITableGenerator, 'ftw.tablegenerator') return generator.generate(self._table_rows(), self._table_columns()) def render_add_form(self): z2.switch_on(self) form = AddPathForm(self.context, self.request) return form() def _table_rows(self): for path in self.config.getPathBlacklist(): yield { 'Path': path, '': '<a href="./@@publisher-config-blacklist?delete=%s">Delete</a>' % \ path, } def _table_columns(self): return ('Path', '')