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 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 _generate_id(self, string): """ This produces a chopped string id from a title, or text, or else uses unknown. For dublicates the method adds 1. """ string = string.strip() if len(string) > 20: string = string[:20] chooser = ISilvaNameChooser(self) id = chooser.chooseName(string, None) # regex the cooked id and strip invalid characters # replace multiple underscores with single underscores # if no string use 'unknown' id = self.reg_start_under.sub( '', self.reg_under.sub('_', self.reg_nonword.sub('_', id))) if not id: id = 'unknown' if id in self.objectIds(): highest = 1 regex = re.compile('^%s_(\d+)$' % re.escape(id)) for other_id in self.objectIds(): match = regex.search(other_id) if match: new_int = int(match.group(1)) if new_int > highest: highest = new_int highest += 1 id = '%s_%s' % (id, highest) return id
def _generate_id(self, string): """ This produces a chopped string id from a title, or text, or else uses unknown. For dublicates the method adds 1. """ string = string.strip() if len(string) > 20: string = string[:20] chooser = ISilvaNameChooser(self) id = chooser.chooseName(string, None) # regex the cooked id and strip invalid characters # replace multiple underscores with single underscores # if no string use 'unknown' id = self.reg_start_under.sub("", self.reg_under.sub("_", self.reg_nonword.sub("_", id))) if not id: id = "unknown" if id in self.objectIds(): highest = 1 regex = re.compile("^%s_(\d+)$" % re.escape(id)) for other_id in self.objectIds(): match = regex.search(other_id) if match: new_int = int(match.group(1)) if new_int > highest: highest = new_int highest += 1 id = "%s_%s" % (id, highest) return id
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 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