def __call__(self, obj, filename, title, description, content_type, data): result = {} result['success'] = None # consolidation because it's different upon Plone versions if title: obj.setTitle(title) if description: obj.setDescription(description) error = IQuickUploadFileSetter(obj).set(data, filename, content_type) # notify edited instead of modified whereas version history is not # saved notify(ObjectEditedEvent(obj)) obj.reindexObject() result['error'] = error if not error: result['success'] = obj IStatusMessage(obj.REQUEST).addStatusMessage( _('msg_file_replaced', default=u"${filename} file has been replaced", mapping={'filename': filename}), type) return result
def __call__(self, filename, title, description, content_type, data, portal_type): context = aq_inner(self.context) error = '' result = {} result['success'] = None newid = get_id_from_filename(filename, context) chooser = INameChooser(context) newid = chooser.chooseName(newid, context) # consolidation because it's different upon Plone versions if not title : # try to split filenames because we don't want # big titles without spaces title = filename.rsplit('.', 1)[0].replace('_',' ').replace('-',' ') if newid in context: # only here for flashupload method since a check_id is done # in standard uploader - see also XXX in quick_upload.py raise NameError, 'Object id %s already exists' %newid else : upload_lock.acquire() try: transaction.begin() try: context.invokeFactory(type_name=portal_type, id=newid, title=title, description=description) except Unauthorized : error = u'serverErrorNoPermission' except ConflictError : # rare with xhr upload / happens sometimes with flashupload error = u'serverErrorZODBConflict' except ValueError: error = u'serverErrorDisallowedType' except Exception, e: error = u'serverError' logger.exception(e) if error: if error == u'serverError': logger.info("An error happens with setId from filename, " "the file has been created with a bad id, " "can't find %s", newid) else: obj = getattr(context, newid) if obj: error = IQuickUploadFileSetter(obj).set(data, filename, content_type) obj._at_rename_after_creation = False try: obj.processForm() except AttributeError: pass del obj._at_rename_after_creation #@TODO : rollback if there has been an error transaction.commit() finally:
def getFileSetter(self, schema): portal_type = 'testtype' setRoles(self.portal, TEST_USER_ID, ('Manager',)) # prepare a fti fti = DexterityFTI(portal_type) fti.klass = 'plone.dexterity.content.Item' fti.schema = schema.__identifier__ self.portal.portal_types._setObject(portal_type, fti) register(fti) # prepare an item item = Item('testitem') item.portal_type = portal_type self.portal._setObject(item.id, item) item = self.portal[item.id] return IQuickUploadFileSetter(item)
def test_adapting(self): item = Item() fileSetter = IQuickUploadFileSetter(item) self.failUnless(fileSetter.__class__ is DexterityFileSetter)