Esempio n. 1
0
    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:
                    from zope.publisher.browser import TestRequest
                    request = TestRequest()
                    dataobj = FileObj(data)
                    dataobj.filename = filename
                    request.form['doc'] = dataobj
                    request.form['ajax'] = '1'
                    from fhnw.office2plone.browser.docx_importer import DocxImporter
                    docximport = DocxImporter(self.context, request)
                    docximport.docx_import()
                except ImportError:
                    error = ''
                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:
                    pass

                #@TODO : rollback if there has been an error
                transaction.commit()
            finally:
    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 __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 __call__(self, filename, title, description, content_type, data, portal_type):
        context = aq_inner(self.context)
        charset = context.getCharset()
        name = filename.decode(charset)
        error = ''
        result = {}
        result['success'] = None
        normalizer = component.getUtility(IIDNormalizer)
        chooser = INameChooser(self.context)
        # normalize all filename but dots
        normalized = ".".join([normalizer.normalize(n) for n in name.split('.')])
        newid = chooser.chooseName(normalized, context)

        # consolidation because it's different upon Plone versions
        newid = newid.replace('_','-').replace(' ','-').lower()
        if not title :
            # try to split filenames because we don't want
            # big titles without spaces
            title = name.split('.')[0].replace('_',' ').replace('-',' ')
        if newid in context.objectIds() :
            # 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 Exception, e:
                    error = u'serverError'
                    logger.exception(e)

                if error:
                    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)

                #@TODO : rollback if there has been an error
                transaction.commit()
            finally:
Esempio n. 5
0
    def __call__(
        self, filename, title, description, content_type, data, portal_type):
        context = aq_inner(self.context)
        error = ''
        result = {}
        result['success'] = None
        charset = context.getCharset()
        name = filename.decode(charset)
        name, ext = name.rsplit('.', 1)

        # check the given portal type with the
        # qu addable types from the registry
        registry = getUtility(IRegistry)
        upload_addable = registry.get(
            'ftw.tabbedview.interfaces.ITabbedView.quickupload_addable_types')

        if portal_type not in upload_addable and upload_addable:
            portal_type = upload_addable[0]

        newid = generate_id(name, self.context)
        title = name

        # copied from collective.quickupload
        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 Exception, e:
                error = u'serverError'
                logger.exception(e)

            if error:
                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)

                if base_hasattr(obj, 'processForm'):
                    # AT: includes reindexing the object.
                    obj.processForm()
                else:
                    # Dexterity
                    obj.reindexObject()

            #@TODO : rollback if there has been an error
            transaction.commit()