def factory_method( container, identifier, title, no_default_content=False, *args, **kw): if ISilvaFactoryDispatcher.providedBy(container): container = container.Destination() identifier = normalize_identifier(identifier) chooser = ISilvaNameChooser(container) try: chooser.checkName( identifier, None, interface=implementedBy(factory)) except ContentError as e: raise ValueError(e.reason) content = factory(identifier) setattr(content, '__initialization__', True) container._setObject(identifier, content) content = container._getOb(identifier) content.set_title(title) for key, value in kw.items(): if hasattr(aq_base(content), 'set_%s' % key): setter = getattr(content, 'set_%s' % key) setter(value) elif hasattr(aq_base(content), key): setattr(content, key, value) delattr(content, '__initialization__') notify(ContentCreatedEvent( content, no_default_content=no_default_content)) return content
def manage_addFile(context, identifier, title=None, file=None): """Add a File """ filename = None if hasattr(file, "name"): filename = os.path.basename(file.name) container = context if ISilvaFactoryDispatcher.providedBy(container): container = container.Destination() chooser = ISilvaNameChooser(container) identifier = chooser.chooseName(identifier or filename, None, file=file, interface=interfaces.IAsset) try: chooser.checkName(identifier, None) except ContentError as e: raise ValueError(_(u"Please provide a unique id: ${reason}", mapping=dict(reason=e.reason))) service = getUtility(IFilesService) context._setObject(identifier, service.new_file(identifier)) content = context._getOb(identifier) if title is not None: content.set_title(title) if file is not None: content.set_file(file) notify(ObjectCreatedEvent(content)) return content
def manage_addImage(context, identifier, title=None, file=None): """Add an Image. """ if file is not None: validate_image(file) filename = None if hasattr(file, 'name'): filename = os.path.basename(file.name) container = context if ISilvaFactoryDispatcher.providedBy(container): container = container.Destination() name_chooser = ISilvaNameChooser(container) identifier = name_chooser.chooseName(identifier or filename, None, file=file, interface=interfaces.IAsset) try: name_chooser.checkName(identifier, None) except ContentError as e: raise ValueError( _(u"Please provide a unique id: ${reason}", mapping=dict(reason=e.reason))) context._setObject(identifier, Image(identifier)) content = context._getOb(identifier) if title is not None: content.set_title(title) if file is not None: content.set_image(file) notify(ObjectCreatedEvent(content)) return content
def factory_method( container, identifier, title, no_default_version=False, *args, **kw): if ISilvaFactoryDispatcher.providedBy(container): container = container.Destination() identifier = normalize_identifier(identifier) chooser = ISilvaNameChooser(container) try: chooser.checkName( identifier, None, interface=implementedBy(factory)) except ContentError as e: raise ValueError(e.reason) content = factory(identifier) setattr(content, '__initialization__', True) container._setObject(identifier, content) content = container._getOb(identifier) if no_default_version is False: version_factory_name = getFactoryName(version) extension = extensionRegistry.get_extension(extension_name) content.create_version('0', None, None) version_factory = getattr( content.manage_addProduct[extension.product], version_factory_name) version_factory('0', title, *args, **kw) delattr(content, '__initialization__') notify(ContentCreatedEvent( content, no_default_version=no_default_version)) return content
def manage_addImage(context, identifier, title=None, file=None): """Add an Image. """ if file is not None: validate_image(file) filename = None if hasattr(file, 'name'): filename = os.path.basename(file.name) container = context if ISilvaFactoryDispatcher.providedBy(container): container = container.Destination() name_chooser = ISilvaNameChooser(container) identifier = name_chooser.chooseName( identifier or filename, None, file=file, interface=interfaces.IAsset) try: name_chooser.checkName(identifier, None) except ContentError as e: raise ValueError(_(u"Please provide a unique id: ${reason}", mapping=dict(reason=e.reason))) context._setObject(identifier, Image(identifier)) content = context._getOb(identifier) if title is not None: content.set_title(title) if file is not None: content.set_image(file) notify(ObjectCreatedEvent(content)) return content
def image_factory(self, id, content_type, file): """Create an Image. """ filename = None if hasattr(file, 'name'): filename = os.path.basename(file.name) name_chooser = ISilvaNameChooser(self) id = name_chooser.chooseName(id or filename, None, file=file, interface=interfaces.IAsset) try: name_chooser.checkName(id, None) except ContentError: return None img = Image(str(id)).__of__(self) return img
def test_zope_name_chooser(self): """Test name chooser implementation for Zope contents. This is used when adding a Silva root. checkName should ContentError for invalids ids, return True for valid ones. """ factory = self.root.manage_addProduct['OFS'] factory.manage_addFolder('folder', 'Folder') chooser = ISilvaNameChooser(self.root.folder) self.assertTrue(verifyObject(ISilvaNameChooser, chooser)) # checkName. services are valid inside a Zope folder self.assertTrue(chooser.checkName('valid_id', None)) self.assertTrue(chooser.checkName('service_check', None)) self.assertTrue(chooser.checkName('override.html', None)) with self.assertRaises(ContentError): chooser.checkName('aq_parent', None) with self.assertRaises(ContentError): chooser.checkName('document__', None) with self.assertRaises(ContentError): chooser.checkName('__document', None)
def test_silva_name_chooser(self): """Test name chooser implementation for Silva contents. checkName should raise ContentErrror for invalid ids, return True for valid ones. """ chooser = ISilvaNameChooser(self.root) self.assertTrue(verifyObject(ISilvaNameChooser, chooser)) # simple case self.assertTrue(chooser.checkName('valid_id', None)) # index is valid by default self.assertTrue(chooser.checkName('index', None)) with self.assertRaises(ContentError): chooser.checkName('service_check', None) with self.assertRaises(ContentError): chooser.checkName('override.html', None) with self.assertRaises(ContentError): chooser.checkName('aq_parent', None) with self.assertRaises(ContentError): chooser.checkName('document__', None) with self.assertRaises(ContentError): chooser.checkName('__document', None) with self.assertRaises(ContentError): # index is not valid for folders chooser.checkName('index', None, interface=IFolder) with self.assertRaises(ContentError): # index is not valid for assets chooser.checkName('index', None, interface=IAsset)