예제 #1
0
파일: api.py 프로젝트: jinxuchen/virtool
async def remove(req):
    """
    Remove a job.

    """
    db = req.app["db"]

    job_id = req.match_info["job_id"]

    document = await db.jobs.find_one(job_id)

    if not document:
        return not_found()

    if virtool.jobs.is_running_or_waiting(document):
        return conflict("Job is running or waiting and cannot be removed")

    # Removed the documents associated with the job ids from the database.
    await db.jobs.delete_one({"_id": job_id})

    try:
        # Calculate the log path and remove the log file. If it exists, return True.
        path = os.path.join(req.app["settings"]["data_path"], "logs", "jobs",
                            job_id + ".log")
        await req.app["run_in_thread"](virtool.utils.rm, path)
    except OSError:
        pass

    return no_content()
예제 #2
0
파일: api.py 프로젝트: jinxuchen/virtool
async def remove_all_api_keys(req):
    """
    Remove all API keys for the session account.

    """
    await req.app["db"].keys.delete_many({"user.id": req["client"].user_id})
    return no_content()
예제 #3
0
async def purge(req):
    """
    Delete all unreferenced HMMs and hide the rest.

    """
    db = req.app["db"]

    await virtool.hmm.db.purge(db, req.app["settings"])

    hmm_path = os.path.join(req.app["settings"]["data_path"],
                            "hmm/profiles.hmm")

    try:
        await req.app["run_in_thread"](virtool.utils.rm, hmm_path)
    except FileNotFoundError:
        pass

    await db.status.find_one_and_update(
        {"_id": "hmm"},
        {"$set": {
            "installed": None,
            "process": None,
            "updates": list()
        }})

    await virtool.hmm.db.fetch_and_update_release(req.app)

    return no_content()
예제 #4
0
파일: api.py 프로젝트: jinxuchen/virtool
async def remove_sequence(req):
    """
    Remove a sequence from an isolate.

    """
    db = req.app["db"]

    otu_id = req.match_info["otu_id"]
    isolate_id = req.match_info["isolate_id"]
    sequence_id = req.match_info["sequence_id"]

    if not await db.sequences.count({"_id": sequence_id}):
        return not_found()

    old = await virtool.otus.db.join(db, {
        "_id": otu_id,
        "isolates.id": isolate_id
    })

    if old is None:
        return not_found()

    if not await virtool.references.db.check_right(req, old["reference"]["id"],
                                                   "modify_otu"):
        return insufficient_rights()

    isolate = virtool.otus.utils.find_isolate(old["isolates"], isolate_id)

    await db.sequences.delete_one({"_id": sequence_id})

    await db.otus.update_one({"_id": otu_id}, {
        "$set": {
            "verified": False
        },
        "$inc": {
            "version": 1
        }
    })

    new = await virtool.otus.db.join(db, otu_id)

    await virtool.otus.db.update_verification(db, new)

    isolate_name = virtool.otus.utils.format_isolate_name(isolate)

    await virtool.history.db.add(
        db, "remove_sequence", old, new,
        f"Removed sequence {sequence_id} from {isolate_name}",
        req["client"].user_id)

    return no_content()
예제 #5
0
파일: api.py 프로젝트: jinxuchen/virtool
async def remove_api_key(req):
    db = req.app["db"]
    user_id = req["client"].user_id
    key_id = req.match_info["key_id"]

    delete_result = await db.keys.delete_one({
        "id": key_id,
        "user.id": user_id
    })

    if delete_result.deleted_count == 0:
        return not_found()

    return no_content()
예제 #6
0
async def remove(req):
    """
    Remove a group.

    """
    db = req.app["db"]

    group_id = req.match_info["group_id"]

    delete_result = await db.groups.delete_one({"_id": group_id})

    if not delete_result.deleted_count:
        return not_found()

    await virtool.groups.db.update_member_users(db, group_id, remove=True)

    return no_content()
예제 #7
0
async def remove(req):
    db = req.app["db"]
    settings = req.app["settings"]

    subtraction_id = req.match_info["subtraction_id"]

    if await db.samples.count({"subtraction.id": subtraction_id}):
        return conflict("Has linked samples")

    delete_result = await db.subtraction.delete_one({"_id": subtraction_id})

    if delete_result.deleted_count == 0:
        return not_found()

    index_path = virtool.subtractions.utils.calculate_index_path(
        settings, subtraction_id)

    await req.app["run_in_thread"](shutil.rmtree, index_path, True)

    return no_content()
예제 #8
0
파일: api.py 프로젝트: jinxuchen/virtool
async def delete_user(req):
    db = req.app["db"]
    ref_id = req.match_info["ref_id"]
    user_id = req.match_info["user_id"]

    document = await db.references.find_one(
        {
            "_id": ref_id,
            "users.id": user_id
        }, ["groups", "users"])

    if document is None:
        return not_found()

    if not await virtool.references.db.check_right(req, ref_id, "modify"):
        return insufficient_rights()

    await virtool.references.db.delete_group_or_user(db, ref_id, user_id,
                                                     "users")

    return no_content()
예제 #9
0
파일: api.py 프로젝트: jinxuchen/virtool
async def remove(req):
    """
    Remove a user.

    """
    db = req.app["db"]

    user_id = req.match_info["user_id"]

    if user_id == req["client"].user_id:
        return bad_request("Cannot remove own account")

    delete_result = await db.users.delete_one({"_id": user_id})

    # Remove user from all references.
    await db.references.update_many({}, {"$pull": {"users": {"id": user_id}}})

    if delete_result.deleted_count == 0:
        return not_found()

    return no_content()
예제 #10
0
파일: api.py 프로젝트: jinxuchen/virtool
async def remove(req):
    """
    Remove an analysis document by its id.

    """
    db = req.app["db"]

    analysis_id = req.match_info["analysis_id"]

    document = await db.analyses.find_one({"_id": analysis_id}, ["job", "ready", "sample"])

    if not document:
        return not_found()

    sample_id = document["sample"]["id"]

    sample = await db.samples.find_one({"_id": sample_id}, virtool.samples.db.PROJECTION)

    if not sample:
        return bad_request("Parent sample does not exist")

    read, write = virtool.samples.utils.get_sample_rights(sample, req["client"])

    if not read or not write:
        return insufficient_rights()

    if not document["ready"]:
        return conflict("Analysis is still running")

    await db.analyses.delete_one({"_id": analysis_id})

    path = os.path.join(req.app["settings"]["data_path"], "samples", sample_id, "analysis", analysis_id)

    await req.app["run_in_thread"](virtool.utils.rm, path, True)

    await virtool.samples.db.recalculate_algorithm_tags(db, sample_id)

    return no_content()
예제 #11
0
파일: api.py 프로젝트: jinxuchen/virtool
async def remove(req):
    """
    Remove a sample document and all associated analyses.

    """
    db = req.app["db"]

    sample_id = req.match_info["sample_id"]

    try:
        if not await virtool.samples.db.check_rights(db, sample_id,
                                                     req["client"]):
            return insufficient_rights()
    except virtool.errors.DatabaseError as err:
        if "Sample does not exist" in str(err):
            return not_found()

        raise

    await virtool.samples.db.remove_samples(db, req.app["settings"],
                                            [sample_id])

    return no_content()
예제 #12
0
파일: api.py 프로젝트: jinxuchen/virtool
async def revert(req):
    """
    Remove the change document with the given ``change_id`` and any subsequent changes.

    """
    db = req.app["db"]

    change_id = req.match_info["change_id"]

    document = await db.history.find_one(change_id, ["reference"])

    if not document:
        return not_found()

    if not await virtool.references.db.check_right(
            req, document["reference"]["id"], "modify_otu"):
        return insufficient_rights()

    try:
        await virtool.history.db.revert(db, change_id)
    except virtool.errors.DatabaseError:
        return conflict("Change is already built")

    return no_content()
예제 #13
0
파일: api.py 프로젝트: jinxuchen/virtool
async def remove_isolate(req):
    """
    Remove an isolate and its sequences from a otu.

    """
    db = req.app["db"]

    otu_id = req.match_info["otu_id"]
    isolate_id = req.match_info["isolate_id"]

    document = await db.otus.find_one({
        "_id": otu_id,
        "isolates.id": isolate_id
    })

    if not document:
        return not_found()

    if not await virtool.references.db.check_right(
            req, document["reference"]["id"], "modify_otu"):
        return insufficient_rights()

    isolates = deepcopy(document["isolates"])

    # Get any isolates that have the isolate id to be removed (only one should match!).
    isolate_to_remove = virtool.otus.utils.find_isolate(isolates, isolate_id)

    # Remove the isolate from the otu' isolate list.
    isolates.remove(isolate_to_remove)

    new_default = None

    # Set the first isolate as default if the removed isolate was the default.
    if isolate_to_remove["default"] and len(isolates):
        new_default = isolates[0]
        new_default["default"] = True

    old = await virtool.otus.db.join(db, otu_id, document)

    document = await db.otus.find_one_and_update({"_id": otu_id}, {
        "$set": {
            "isolates": isolates,
            "verified": False
        },
        "$inc": {
            "version": 1
        }
    })

    new = await virtool.otus.db.join(db, otu_id, document)

    issues = await virtool.otus.db.verify(db, otu_id, joined=new)

    if issues is None:
        await db.otus.update_one({"_id": otu_id}, {"$set": {"verified": True}})

        new["verified"] = True

    # Remove any sequences associated with the removed isolate.
    await db.sequences.delete_many({
        "otu_id": otu_id,
        "isolate_id": isolate_id
    })

    old_isolate_name = virtool.otus.utils.format_isolate_name(
        isolate_to_remove)

    description = f"Removed {old_isolate_name}"

    if isolate_to_remove["default"] and new_default:
        new_isolate_name = virtool.otus.utils.format_isolate_name(new_default)
        description += f" and set {new_isolate_name} as default"

    await virtool.history.db.add(db, "remove_isolate", old, new, description,
                                 req["client"].user_id)

    return no_content()