Exemplo n.º 1
0
def user(user_id=None):
    if request.method == 'POST':
        fullname = request.form['fullname']
        user = User(fullname=fullname)
        save(user)
        session.refresh(user)
    elif request.method =='GET':
        must_be_logged_in(lambda a:a) # check if user is logged in or not
        user = session.query(User).filter(User.id == user_id).one()
    return paginate_response(user.as_dict())
Exemplo n.º 2
0
def user(user_id=None):
    if request.method == 'POST':
        fullname = request.form['fullname']
        user = User(fullname=fullname)
        save(user)
        session.refresh(user)
    elif request.method == 'GET':
        must_be_logged_in(lambda a: a)  # check if user is logged in or not
        user = session.query(User).filter(User.id == user_id).one()
    return paginate_response(user.as_dict())
Exemplo n.º 3
0
def files(file_id):
    file_folder = session.query(Node).filter(File.user==get_user() and File.id==file_id).one()
    if request.method=='GET':
        assert file_folder.is_folder
        children = [child.as_dict() for child in file_folder.files]
        return paginate_response(children)
    #todo: this code for put and patch files deals with file checkout. it does NOT match with what api does. understand and fix.
    elif request.method == 'PUT':
        file_folder.checked_out = True
        save(file_folder)
    elif request.method == 'PATCH':
        file_folder.checked_out = True
        save(file_folder)
    return paginate_response(file_folder.as_dict())
Exemplo n.º 4
0
def files(file_id):
    file_folder = session.query(Node).filter(File.user == get_user()
                                             and File.id == file_id).one()
    if request.method == 'GET':
        assert file_folder.is_folder
        children = [child.as_dict() for child in file_folder.files]
        return paginate_response(children)
    #todo: this code for put and patch files deals with file checkout. it does NOT match with what api does. understand and fix.
    elif request.method == 'PUT':
        file_folder.checked_out = True
        save(file_folder)
    elif request.method == 'PATCH':
        file_folder.checked_out = True
        save(file_folder)
    return paginate_response(file_folder.as_dict())
Exemplo n.º 5
0
def resources(node_id, provider, file_id=None):
    if file_id is None:
        file_folder = session.query(Node).filter(Node.user==get_user() and Node.id == node_id).one().files[0] # provider folder
    else:
        file_folder = session.query(File).filter(File.id==file_id and File.user==get_user() and File.node_id == node_id).one()
    if request.method=='GET': # download
        assert file_folder.is_file
        #make it so that the returned content is actually downloadable.
        content = file_folder.contents
        response = make_response(content)
        # Set the right header for the response
        response.headers["Content-Disposition"] = "attachment; filename={}".format(file_folder.name)
        return response
    elif request.method == 'PUT': # upload new file, upload new folder, update existing file
        if file_folder.is_folder: # upload new file, upload new folder
            parent = file_folder
            kind = request.args.get('kind','file')
            new_file_folder = File(
                type=File.FOLDER if kind == 'folder' else File.FILE,
                node=parent.node,
                user=parent.user,
                parent=parent,
                name=request.args.get('name'),
                provider=provider,
                contents=request.get_data() if kind == 'file' else None
            )
            save(new_file_folder)
            session.refresh(new_file_folder)
            return paginate_response(new_file_folder.as_dict())
        else: # update existing file
            assert request.args.get('kind') == 'file'
            assert request.args.get('name') is not None
            assert file_folder.has_parent # if no parent, then provider thus error.
            # this creates new version of the file
            file_folder.content = request.get_data()
            file_folder.name = request.args.get('name')
            session.refresh(file_folder)
            return paginate_response(file_folder.as_dict())
    elif request.method=='POST': # rename, move
        assert file_folder.has_parent
        if request.json['action'] == 'rename':
            file_folder.name = request.json['rename']
            save(file_folder)
            session.refresh(file_folder)
            return paginate_response(file_folder.as_dict())
        elif request.json['action'] == 'move':
            new_parent_id = request.json['path'].split('/')[1]
            new_parent = session.query(File).filter(File.id == new_parent_id).one()
            file_folder.parent = new_parent

            if request.json['rename']:
                file_folder.name = request.json['rename']

            save(file_folder)
            session.refresh(file_folder)
            return paginate_response(file_folder.as_dict())
    elif request.method=='DELETE':
        session.query(File).filter(File.id==file_id and File.user==get_user()).delete()
        #todo: unclear what to return in this case right now.
        return jsonify({'success':'true'})
Exemplo n.º 6
0
def node(node_id=None):
    if request.method == 'POST':

        user = get_user()
        title = request.get_json()['data']['attributes']['title']
        node = Node(title=title, user=user)
        session.refresh(user)
        save(node)
        provider = File(name=File.DEFAULT_PROVIDER, type=File.FOLDER, user=user, node=node)
        node.files.append(provider)
        save(node)
        save(provider)
        session.refresh(node)
    elif request.method =='GET':
        node = session.query(Node).filter(Node.user==get_user() and Node.id==node_id).one()
    return paginate_response(node.as_dict())
Exemplo n.º 7
0
def node(node_id=None):
    if request.method == 'POST':

        user = get_user()
        title = request.get_json()['data']['attributes']['title']
        node = Node(title=title, user=user)
        session.refresh(user)
        save(node)
        provider = File(name=File.DEFAULT_PROVIDER,
                        type=File.FOLDER,
                        user=user,
                        node=node)
        node.files.append(provider)
        save(node)
        save(provider)
        session.refresh(node)
    elif request.method == 'GET':
        node = session.query(Node).filter(Node.user == get_user()
                                          and Node.id == node_id).one()
    return paginate_response(node.as_dict())
Exemplo n.º 8
0
def resources(node_id, provider, file_id=None):
    if file_id is None:
        file_folder = session.query(Node).filter(
            Node.user == get_user()
            and Node.id == node_id).one().files[0]  # provider folder
    else:
        file_folder = session.query(File).filter(
            File.id == file_id and File.user == get_user()
            and File.node_id == node_id).one()
    if request.method == 'GET':  # download
        assert file_folder.is_file
        #make it so that the returned content is actually downloadable.
        content = file_folder.contents
        response = make_response(content)
        # Set the right header for the response
        response.headers[
            "Content-Disposition"] = "attachment; filename={}".format(
                file_folder.name)
        return response
    elif request.method == 'PUT':  # upload new file, upload new folder, update existing file
        if file_folder.is_folder:  # upload new file, upload new folder
            parent = file_folder
            kind = request.args.get('kind', 'file')
            new_file_folder = File(
                type=File.FOLDER if kind == 'folder' else File.FILE,
                node=parent.node,
                user=parent.user,
                parent=parent,
                name=request.args.get('name'),
                provider=provider,
                contents=request.get_data() if kind == 'file' else None)
            save(new_file_folder)
            session.refresh(new_file_folder)
            return paginate_response(new_file_folder.as_dict())
        else:  # update existing file
            assert request.args.get('kind') == 'file'
            assert request.args.get('name') is not None
            assert file_folder.has_parent  # if no parent, then provider thus error.
            # this creates new version of the file
            file_folder.content = request.get_data()
            file_folder.name = request.args.get('name')
            session.refresh(file_folder)
            return paginate_response(file_folder.as_dict())
    elif request.method == 'POST':  # rename, move
        assert file_folder.has_parent
        if request.json['action'] == 'rename':
            file_folder.name = request.json['rename']
            save(file_folder)
            session.refresh(file_folder)
            return paginate_response(file_folder.as_dict())
        elif request.json['action'] == 'move':
            new_parent_id = request.json['path'].split('/')[1]
            new_parent = session.query(File).filter(
                File.id == new_parent_id).one()
            file_folder.parent = new_parent

            if request.json['rename']:
                file_folder.name = request.json['rename']

            save(file_folder)
            session.refresh(file_folder)
            return paginate_response(file_folder.as_dict())
    elif request.method == 'DELETE':
        session.query(File).filter(File.id == file_id
                                   and File.user == get_user()).delete()
        #todo: unclear what to return in this case right now.
        return jsonify({'success': 'true'})