Ejemplo n.º 1
0
def shared_get():
    """
    Get all resources shared with the user.

    :return: the list of shared files, with complete hierarchy
    """
    return ok('OK', [r.resource.deep for r in current_user.roles])
Ejemplo n.º 2
0
def file_version_details(res_id, version_id):
    """
    Get the details of a specific version of a file.

    :param res_id: the file's id
    :param version_id: the version's id
    :return: the version's details
    """
    version = Version.query.filter_by(id=version_id, res_id=res_id).first()
    return ok('OK', version.serialized)
Ejemplo n.º 3
0
def root_content():
    """
    Get resources located in user's root.

    Get the list of all user's resources with no parent.

    :return: user's resources in root folder
    """
    resources = Resource.query.filter_by(owner_id=current_user.id,
                                         parent_id=None).all()
    return ok('OK', [r.deep for r in resources])
Ejemplo n.º 4
0
def resource_details(res_id=None):
    """
    Get a resource's details.

    Return the specified resource's details. If the resource is a folder, the recursive
    hierarchy is also returned.

    :param res_id: the requested resource's id
    :return: the requested resource's details
    """
    file = Resource.query.get(res_id)
    return ok('OK', file.deep)
Ejemplo n.º 5
0
def file_version_delete(res_id, version_id):
    """
    Delete a specific version of a file.

    :param res_id: the file's id
    :param version_id: the version's id
    :return: the version's details
    """
    version = Version.query.filter_by(id=version_id, res_id=res_id).first()
    Version.delete(version)
    db.session.commit()
    return ok('File version successfully deleted.', version.serialized)
Ejemplo n.º 6
0
def resource_delete(res_id):
    """
    Delete a resource.

    If the resource is a file, all versions and files on the disk will be removed.
    If the resource is a folder, all children are also deleted.

    :param res_id: the requested resource's id
    :return: the deleted resource
    """
    resource = Resource.query.get(res_id)
    Resource.delete(resource)
    db.session.commit()
    return ok('File successfully deleted.', resource.deep)
Ejemplo n.º 7
0
def user_login():
    """
    Log the user in.

    :return: the user info if login is successful
    """
    content = request.get_json()
    if content is None:
        content = {}
    username = content.get('username', None)
    if username is None:
        return bad_request('Username must be submitted.')
    user = User.query.filter_by(username=username).first()
    if user is None:
        return unauthorized('Wrong credentials.')
    password = content.get('password', None)
    if not bcrypt.check_password_hash(user.password, password):
        return unauthorized('Wrong credentials.')
    login_user(user)
    return ok('Login successful.', user.serialized)
Ejemplo n.º 8
0
def resource_update(res_id):
    """
    Update a resource's details.

    :param res_id: the requested resource's id
    :return: the updated resource
    """
    resource = Resource.query.get(res_id)
    content = request.get_json()
    params = {}
    if 'name' in content:
        params['name'] = content['name']
    if 'extension' in content and resource.type == resource_type.file:
        params['extension'] = content['extension']
    if 'parent_id' in content:
        parent_id = content['parent_id']
        parent = Resource.query.get(
            parent_id) if parent_id is not None else None
        params['parent'] = parent
        if parent.owner.id != resource.owner.id:
            return bad_request('Resource cannot be moved here')
    Resource.update(resource, **params)
    db.session.commit()
    return ok('Resource updated', resource.deep)
Ejemplo n.º 9
0
def revoke_resource(res_id, user_id):
    res = Resource.query.get(res_id)
    user = User.query.get(user_id)
    Role.unlink(res, user)
    db.session.commit()
    return ok('Rights revoked on resource')
Ejemplo n.º 10
0
def user_list():
    return ok('OK', [u.serialized for u in User.query.all()])
Ejemplo n.º 11
0
def user_get_details():
    return ok('', current_user.serialized)
Ejemplo n.º 12
0
def user_logout():
    """
    Log the user out.
    """
    logout_user()
    return ok('Logout successful.')