Exemplo n.º 1
0
def getContent(req, ids):

    user = users.getUserFromRequest(req)
    access = AccessData(user=user)
    language = lang(req)

    if "action" in req.params:
        state = 'ok'

        if req.params.get('action') == "removefiles":
            basenode = tree.getNode(req.params.get('id'))
            for f in basenode.getFiles():
                try:
                    os.remove(f.retrieveFile())
                    pass
                except:
                    state = "error"
            for f in basenode.getFiles():
                basenode.removeFile(f)
            req.write(json.dumps({'state': state}))
            return None

        if req.params.get('action') == "buildnode":  # create nodes
            basenode = tree.getNode(req.params.get('id'))
            newnodes = []
            errornodes = []
            basenodefiles_processed = []
            if req.params.get('uploader', '') == 'plupload':
                filename2scheme = {}
                for k in req.params:
                    if k.startswith("scheme_"):
                        filename2scheme[
                            k.replace('scheme_', '', 1)] = req.params.get(k)

                for f in basenode.getFiles():
                    filename = f.getName()
                    if filename in filename2scheme:
                        _m = getMimeType(filename)

                        if _m[1] == "bibtex":  # bibtex import handler
                            try:
                                nn = importBibTeX(f.retrieveFile(), basenode)
                                newnodes.append(nn.id)
                                basenodefiles_processed.append(f)
                            except ValueError, e:
                                errornodes.append((filename, str(e)))

                        logger.debug("filename: %r, mimetype: %r" % (filename, _m))
                        logger.debug("__name__=%r, func=%r; _m=%r, _m[1]=%r" % (__name__, funcname(), _m, _m[1]))

                        node_type = '%s/%s' % (_m[1], filename2scheme[filename])

                        n = tree.Node(filename, type=node_type)

                        basenode.addChild(n)
                        n.set("creator", user.getName())
                        n.set("creationtime",  str(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time()))))

                        n.addFile(f)

                        n.event_files_changed()
                        clearFromCache(n)
                        newnodes.append(n.id)
                        basenodefiles_processed.append(f)
                        basenode.removeFile(f)
                        msg_t = (user.getName(), n.id, n.name, n.type,filename, basenode.id, basenode.name, basenode.type)
                        msg = "%s created new node id=%r (name=%r, type=%r) by uploading file %r, node is child of base node id=%r (name=%r, type=%r)" % msg_t
                        logger.info(msg)
                        logging.getLogger('usertracing').info(msg)

            else:
                for filename in req.params.get('files').split('|'):
                    _m = getMimeType(filename)
                    logger.debug("... in %s.%s: getMimeType(filename=%r)=%r" % (__name__, funcname(), filename, _m))
                    fs = basenode.getFiles()
                    logger.debug("... in %s.%s: basenode.id=%r, basenode_files: %r" % (__name__, funcname(), basenode.id, [(x.getName(), x.retrieveFile()) for x in fs]))
                    if _m[1] == req.params.get('type') or req.params.get('type') == 'file':
                        for f in basenode.getFiles():
                            # ambiguity here ?
                            if f.retrieveFile().endswith(filename):
                                # bibtex import handler
                                if _m[1] == "bibtex" and not req.params.get('type') == 'file':
                                    try:
                                        nn = importBibTeX(f.retrieveFile(), basenode)
                                        newnodes.append(nn.id)
                                        basenodefiles_processed.append(f)
                                    except ValueError, e:
                                        errornodes.append((filename, str(e)))
                                else:

                                    logger.debug("creating new node: filename: %r" % filename)
                                    logger.debug("files at basenode: %r" % [(x.getName(), x.retrieveFile()) for x in basenode.getFiles()])

                                    n = tree.Node(filename, type='%s/%s' % (req.params.get('type'), req.params.get('value')))
                                    basenode.addChild(n)
                                    n.set("creator", user.getName())
                                    n.set("creationtime",  str(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time()))))
                                    # clones to a file with random name
                                    cloned_file = f.clone(None)
                                    n.addFile(cloned_file)
                                    if hasattr(n, 'event_files_changed'):
                                        n.event_files_changed()
                                    clearFromCache(n)
                                    newnodes.append(n.id)
                                    basenodefiles_processed.append(f)
                                    msg_t = (user.getName(), n.id, n.name, n.type, filename, basenode.id, basenode.name, basenode.type)
                                    msg = "%s created new node id=%r (name=%r, type=%r) by uploading file %r, node is child of base node id=%r (name=%r, type=%r)" % msg_t
                                    logger.info(msg)
                                    logging.getLogger('usertracing').info(msg)
                                    break  # filename may not be unique

            new_tree_labels = [{'id': basenode.id, 'label': getTreeLabel(basenode, lang=language)}]
            for f in basenodefiles_processed:
                basenode.removeFile(f)
                f_path = f.retrieveFile()
                if os.path.exists(f_path):
                    logger.debug("%s going to remove file %r from disk" % (user.getName(), f_path))
                    os.remove(f_path)

            mime = getMimeType(filename)
            scheme_type = {mime[1]: []}
            for scheme in getSchemes(req):
                if mime[1] in scheme.getDatatypes():
                    scheme_type[mime[1]].append(scheme)
                    # break

            # standard file
            content = req.getTAL('web/edit/modules/upload.html', {'files': [filename], 'schemes': scheme_type}, macro="uploadfileok")

            res = json.dumps({'state': state, 'newnodes': newnodes, 'errornodes':
                              errornodes, 'new_tree_labels': new_tree_labels, 'ret': content})
            req.write(res)
            return None
Exemplo n.º 2
0
            logger.debug("upload done -> deliver view of object")

            if proceed_to_uploadcomplete:
                req.params['file'] = realname

            mime = getMimeType(req.params.get('file'))
            data_extra = req.params.get('data_extra', '')
            if data_extra == 'tofile':

                ctx = {
                    'filename': f.getName(),
                }
                ctx.update(upload_to_filetype_filehandler(req))
                content = req.getTAL('web/edit/modules/upload.html', ctx, macro="uploadfileok_plupload")
                basenode = tree.getNode(req.params.get('id'))
                new_tree_labels = [{'id': basenode.id, 'label': getTreeLabel(basenode, lang=language)}]
                req.write(json.dumps({'type': 'file', 'ret': content, 'state': state, 'filename': req.params.get('file'), 'new_tree_labels': new_tree_labels}))
                return None

            if mime[1] == "other":  # file type not supported
                req.write(json.dumps({'type': mime[1], 'ret': req.getTAL('web/edit/modules/upload.html', {}, macro="uploadfileerror"), 'state': 'error', 'filename': req.params.get('file')}))
                logger.debug("%r|%s.%s: added file to node %r (%r, %r) -> file type not supported" % (get_user_id(req), __name__, funcname(), n.id, n.name, n.type))
                return None

            elif mime[1] == "zip":  # zip file
                if req.params.get('uploader', '') == 'plupload':
                    macro = "uploadzipfileok_plupload"
                else:
                    macro = "uploadzipfileok"
                content = req.getTAL('web/edit/modules/upload.html', upload_ziphandler(req), macro=macro)
            elif mime[1] == "bibtex":  # bibtex file
Exemplo n.º 3
0
            data_extra = req.params.get('data_extra', '')
            if data_extra == 'tofile':

                ctx = {
                    'filename': f.getName(),
                }
                ctx.update(upload_to_filetype_filehandler(req))
                content = req.getTAL('web/edit/modules/upload.html',
                                     ctx,
                                     macro="uploadfileok_plupload")
                basenode = q(Node).get(req.params.get('id'))
                new_tree_labels = [{
                    'id':
                    basenode.id,
                    'label':
                    getTreeLabel(basenode, lang=language)
                }]
                req.write(
                    json.dumps(
                        {
                            'type': 'file',
                            'ret': content,
                            'state': state,
                            'filename': req.params.get('file'),
                            'new_tree_labels': new_tree_labels
                        },
                        ensure_ascii=False))
                return None

            if mime[1] == "other":  # file type not supported
                req.write(
Exemplo n.º 4
0
def getContent(req, ids):

    user = users.getUserFromRequest(req)
    language = lang(req)

    def get_ids_from_query():
        ids = get_ids_from_req(req)
        return ",".join(ids)

    if "action" in req.params:
        state = 'ok'

        if req.params.get('action') == "removefiles":
            basenode = q(Node).get(req.params.get('id'))
            for f in basenode.files:
                try:
                    os.remove(f.abspath)
                    pass
                except:
                    state = "error"
            basenode.files = []
            db.session.commit()
            req.write(json.dumps({'state': state}, ensure_ascii=False))
            return None

        if req.params.get('action') == "buildnode":  # create nodes
            basenode = q(Node).get(req.params.get('id'))
            newnodes = []
            errornodes = []
            basenodefiles_processed = []
            if req.params.get('uploader', '') == 'plupload':
                filename2scheme = {}
                for k in req.params:
                    if k.startswith("scheme_"):
                        filename2scheme[k.replace('scheme_', '',
                                                  1)] = req.params.get(k)

                for f in basenode.files:
                    filename = f.name
                    if filename in filename2scheme:
                        mimetype = getMimeType(filename)

                        if mimetype[1] == "bibtex":  # bibtex import handler
                            try:
                                new_node = importBibTeX(f.abspath, basenode)
                                newnodes.append(new_node.id)
                                basenodefiles_processed.append(f)
                            except ValueError, e:
                                errornodes.append((filename, unicode(e)))

                        logg.debug("filename: %s, mimetype: %s", filename,
                                   mimetype)
                        logg.debug("__name__=%s, func=%s; _m=%s, _m[1]=%s",
                                   __name__, funcname(), mimetype, mimetype[1])

                        content_class = Node.get_class_for_typestring(
                            mimetype[1])
                        node = content_class(name=filename,
                                             schema=filename2scheme[filename])

                        basenode.children.append(node)
                        node.set("creator", user.login_name)
                        node.set(
                            "creationtime",
                            unicode(
                                time.strftime('%Y-%m-%dT%H:%M:%S',
                                              time.localtime(time.time()))))
                        # set filetype for uploaded file as requested by the content class
                        f.filetype = content_class.get_upload_filetype()
                        node.files.append(f)
                        node.event_files_changed()
                        newnodes.append(node.id)
                        basenodefiles_processed.append(f)
                        basenode.files.remove(f)
                        db.session.commit()
                        logg.info(
                            "%s created new node id=%s (name=%s, type=%s) by uploading file %s, "
                            "node is child of base node id=%s (name=%s, type=%s)",
                            user.login_name, node.id, node.name, node.type,
                            filename, basenode.id, basenode.name,
                            basenode.type)

            else:
                for filename in req.params.get('files').split('|'):
                    mimetype = getMimeType(filename)
                    logg.debug("... in %s.%s: getMimeType(filename=%s)=%s",
                               __name__, funcname(), filename, mimetype)
                    if mimetype[1] == req.params.get('type') or req.params.get(
                            'type') == 'file':
                        for f in basenode.files:
                            # ambiguity here ?
                            if f.abspath.endswith(filename):
                                # bibtex import handler
                                if mimetype[
                                        1] == "bibtex" and not req.params.get(
                                            'type') == 'file':
                                    try:
                                        new_node = importBibTeX(
                                            f.abspath, basenode)
                                        newnodes.append(new_node.id)
                                        basenodefiles_processed.append(f)
                                    except ValueError, e:
                                        errornodes.append(
                                            (filename, unicode(e)))
                                    db.session.commit()
                                else:

                                    logg.debug(
                                        "creating new node: filename: %s",
                                        filename)
                                    logg.debug("files at basenode: %s",
                                               [(x.getName(), x.abspath)
                                                for x in basenode.files])

                                    content_class = Node.get_class_for_typestring(
                                        req.params.get('type'))
                                    node = content_class(
                                        name=filename,
                                        schema=req.params.get('value'))

                                    basenode.children.append(node)
                                    node.set("creator", user.login_name)
                                    node.set(
                                        "creationtime",
                                        unicode(
                                            time.strftime(
                                                '%Y-%m-%dT%H:%M:%S',
                                                time.localtime(time.time()))))

                                    # clones to a file with random name
                                    cloned_file = importFileRandom(f.abspath)
                                    # set filetype for uploaded file as requested by the content class
                                    cloned_file.filetype = content_class.get_upload_filetype(
                                    )
                                    node.files.append(cloned_file)
                                    node.event_files_changed()
                                    newnodes.append(node.id)
                                    basenodefiles_processed.append(f)

                                    logg.info(
                                        "%s created new node id=%s (name=%s, type=%s) by uploading file %s, "
                                        "node is child of base node id=%s (name=%s, type=%s)",
                                        user.login_name, node.id, node.name,
                                        node.type, filename, basenode.id,
                                        basenode.name, basenode.type)

                                    break  # filename may not be unique

            new_tree_labels = [{
                'id': basenode.id,
                'label': getTreeLabel(basenode, lang=language)
            }]
            for f in basenodefiles_processed:
                basenode.files.remove(f)
                f_path = f.abspath
                if os.path.exists(f_path):
                    logg.debug("%s going to remove file %s from disk",
                               user.login_name, f_path)
                    os.remove(f_path)

            mime = getMimeType(filename)
            scheme_type = {mime[1]: []}
            for scheme in get_permitted_schemas():
                if mime[1] in scheme.getDatatypes():
                    scheme_type[mime[1]].append(scheme)
                    # break

            db.session.commit()
            # standard file
            content = req.getTAL('web/edit/modules/upload.html', {
                'files': [filename],
                'schemes': scheme_type
            },
                                 macro="uploadfileok")

            res = {
                'state': state,
                'newnodes': newnodes,
                'errornodes': errornodes,
                'new_tree_labels': new_tree_labels,
                'ret': content
            }
            res = json.dumps(res, ensure_ascii=False)
            req.write(res)
            return None
Exemplo n.º 5
0
def getContent(req, ids):

    user = users.getUserFromRequest(req)
    language = lang(req)

    def get_ids_from_query():
        ids = get_ids_from_req(req)
        return ",".join(ids)

    if "action" in req.params:
        state = 'ok'

        if req.params.get('action') == "removefiles":
            basenode = q(Node).get(req.params.get('id'))
            for f in basenode.files:
                try:
                    os.remove(f.abspath)
                    pass
                except:
                    state = "error"
            basenode.files = []
            db.session.commit()
            req.write(json.dumps({'state': state}, ensure_ascii=False))
            return None

        if req.params.get('action') == "buildnode":  # create nodes
            basenode = q(Node).get(req.params.get('id'))
            newnodes = []
            errornodes = []
            basenodefiles_processed = []
            if req.params.get('uploader', '') == 'plupload':
                filename2scheme = {}
                for k in req.params:
                    if k.startswith("scheme_"):
                        filename2scheme[
                            k.replace('scheme_', '', 1)] = req.params.get(k)

                for f in basenode.files:
                    filename = f.name
                    if filename in filename2scheme:
                        mimetype = getMimeType(filename)

                        if mimetype[1] == "bibtex":  # bibtex import handler
                            try:
                                new_node = importBibTeX(f.abspath, basenode, req=req)
                                newnodes.append(new_node.id)
                                basenodefiles_processed.append(f)
                            except ValueError, e:
                                errornodes.append((filename, unicode(e)))

                        logg.debug("filename: %s, mimetype: %s", filename, mimetype)
                        logg.debug("__name__=%s, func=%s; _m=%s, _m[1]=%s", __name__, funcname(), mimetype, mimetype[1])

                        content_class = Node.get_class_for_typestring(mimetype[1])
                        node = content_class(name=filename, schema=filename2scheme[filename])

                        basenode.children.append(node)
                        node.set("creator", user.login_name)
                        node.set("creationtime",  unicode(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time()))))
                        # set filetype for uploaded file as requested by the content class
                        f.filetype = content_class.get_upload_filetype()
                        node.files.append(f)
                        node.event_files_changed()
                        newnodes.append(node.id)
                        basenodefiles_processed.append(f)
                        basenode.files.remove(f)
                        db.session.commit()
                        logg.info("%s created new node id=%s (name=%s, type=%s) by uploading file %s, "
                            "node is child of base node id=%s (name=%s, type=%s)", user.login_name, node.id, node.name, node.type,
                             filename, basenode.id, basenode.name, basenode.type)

            else:
                for filename in req.params.get('files').split('|'):
                    mimetype = getMimeType(filename)
                    logg.debug("... in %s.%s: getMimeType(filename=%s)=%s", __name__, funcname(), filename, mimetype)
                    if mimetype[1] == req.params.get('type') or req.params.get('type') == 'file':
                        for f in basenode.files:
                            # ambiguity here ?
                            if f.abspath.endswith(filename):
                                # bibtex import handler
                                if mimetype[1] == "bibtex" and not req.params.get('type') == 'file':
                                    try:
                                        new_node = importBibTeX(f.abspath, basenode, req=req)
                                        newnodes.append(new_node.id)
                                        basenodefiles_processed.append(f)
                                    except ValueError, e:
                                        errornodes.append((filename, unicode(e)))
                                    db.session.commit()
                                else:

                                    logg.debug("creating new node: filename: %s", filename)
                                    logg.debug("files at basenode: %s", [(x.getName(), x.abspath) for x in basenode.files])

                                    content_class = Node.get_class_for_typestring(req.params.get('type'))
                                    node = content_class(name=filename, schema=req.params.get('value'))

                                    basenode.children.append(node)
                                    node.set("creator", user.login_name)
                                    node.set("creationtime",  unicode(time.strftime('%Y-%m-%dT%H:%M:%S',
                                                                                    time.localtime(time.time()))))

                                    # clones to a file with random name
                                    cloned_file = importFileRandom(f.abspath)
                                    # set filetype for uploaded file as requested by the content class
                                    cloned_file.filetype = content_class.get_upload_filetype()
                                    node.files.append(cloned_file)
                                    node.event_files_changed()
                                    newnodes.append(node.id)
                                    basenodefiles_processed.append(f)

                                    logg.info("%s created new node id=%s (name=%s, type=%s) by uploading file %s, "
                                    "node is child of base node id=%s (name=%s, type=%s)", user.login_name, node.id, node.name, node.type, filename,
                                    basenode.id, basenode.name, basenode.type)

                                    break  # filename may not be unique

            new_tree_labels = [{'id': basenode.id, 'label': getTreeLabel(basenode, lang=language)}]
            for f in basenodefiles_processed:
                basenode.files.remove(f)
                f_path = f.abspath
                if os.path.exists(f_path):
                    logg.debug("%s going to remove file %s from disk", user.login_name, f_path)
                    os.remove(f_path)

            mime = getMimeType(filename)
            scheme_type = {mime[1]: []}
            for scheme in get_permitted_schemas():
                if mime[1] in scheme.getDatatypes():
                    scheme_type[mime[1]].append(scheme)
                    # break

            db.session.commit()
            # standard file
            content = req.getTAL('web/edit/modules/upload.html', {'files': [filename], 'schemes': scheme_type}, macro="uploadfileok")

            res = {'state': state, 'newnodes': newnodes, 'errornodes':
                              errornodes, 'new_tree_labels': new_tree_labels, 'ret': content}
            res = json.dumps(res, ensure_ascii=False)
            req.write(res)
            return None