def add_dataset(parent: uuid.UUID) -> uuid.UUID: """ Add a dataset that can be used for tests. Will be conneted to the provided order. The "edit" user is the editor. Args: parent (uuid.UUID): The order to use as parent. Returns: uuid.UUID: The _id of the dataset. """ mongo_db = db_connection() indata = structure.dataset() indata.update( { "description": "Added by fixture.", "title": "Test title from fixture", "tags": ["fromFixture", "testing"], } ) indata.update(TEST_LABEL) mongo_db["datasets"].insert_one(indata) mongo_db["orders"].update_one({"_id": parent}, {"$push": {"datasets": indata["_id"]}}) return indata["_id"]
def get_dataset_data_structure(): """ Get an empty dataset entry. Returns: flask.Response: JSON structure of a dataset. """ empty_dataset = structure.dataset() empty_dataset["_id"] = "" return utils.response_json({"dataset": empty_dataset})
def add_dataset(identifier: str): # pylint: disable=too-many-branches """ Add a dataset to the given order. Args: identifier (str): The order to add the dataset to. """ order = utils.req_get_entry("orders", identifier) if not order: flask.abort(status=404) if (not utils.req_has_permission("DATA_MANAGEMENT") and flask.g.current_user["_id"] not in order["editors"]): flask.abort(status=403) new_dataset = structure.dataset() jsondata = flask.request.json if not jsondata or "dataset" not in jsondata or not isinstance( jsondata["dataset"], dict): flask.abort(status=400) indata = jsondata["dataset"] validation = utils.basic_check_indata(indata, new_dataset, ["_id"]) if not validation.result: flask.abort(status=validation.status) indata = utils.prepare_for_db(indata) new_dataset.update(indata) ds_result = utils.req_commit_to_db("datasets", "add", new_dataset) if not ds_result.log or not ds_result.data: flask.abort(status=500) order_result = flask.g.db["orders"].update_one( {"_id": order["_id"]}, {"$push": { "datasets": new_dataset["_id"] }}) if not order_result.acknowledged: flask.current_app.logger.error("Failed to add dataset %s to order %s", new_dataset["_id"], order["_id"]) flask.abort(status=500) order["datasets"].append(new_dataset["_id"]) utils.req_make_log_new( data_type="order", action="edit", comment="Dataset added", data=order, ) return utils.response_json({"_id": ds_result.ins_id})
def add_dataset_full(): """ Add an order with a dataset. Returns: tuple: (order_uuid, dataset_uuid) """ mongo_db = db_connection() # prepare order_indata = structure.order() order_indata.update({"description": "Added by fixture.", "title": "Test title from fixture"}) order_indata.update(TEST_LABEL) edit_user = mongo_db["users"].find_one({"auth_ids": USERS["edit"]}) order_indata["authors"] = [edit_user["_id"]] order_indata["editors"] = [edit_user["_id"]] order_indata["generators"] = [edit_user["_id"]] order_indata["organisation"] = edit_user["_id"] dataset_indata = structure.dataset() dataset_indata.update({"description": "Added by fixture.", "title": "Test title from fixture"}) dataset_indata.update(TEST_LABEL) collection_indata = structure.collection() collection_indata.update( { "description": "Added by fixture.", "title": "Test title from fixture", "editors": [edit_user["_id"]], } ) collection_indata.update(TEST_LABEL) mongo_db["datasets"].insert_one(dataset_indata) order_indata["datasets"].append(dataset_indata["_id"]) collection_indata["datasets"].append(dataset_indata["_id"]) mongo_db["orders"].insert_one(order_indata) mongo_db["collections"].insert_one(collection_indata) return (order_indata["_id"], dataset_indata["_id"], collection_indata["_id"])
def gen_datasets(db, nr_datasets: int = 500): uuids = [] orders = [entry["_id"] for entry in db["orders"].find()] for i in range(1, nr_datasets + 1): dataset = structure.dataset() changes = {"title": f"Dataset {i} Title", "description": make_description()} # add extra field if random.random() > 0.4: key = random.choice(EXTRA_KEYS) changes["properties"] = {key: random.choice(EXTRA_FIELDS[key])} changes["tags"] = list( set(random.choice(EXTRA_VALUES) for _ in range(random.randint(0, 6))) ) dataset.update(changes) uuids.append(db["datasets"].insert_one(dataset).inserted_id) make_log( db, action="add", data=dataset, data_type="dataset", comment="Generated", user="******", ) order_uuid = random.choice(orders) db["orders"].update_one({"_id": order_uuid}, {"$push": {"datasets": uuids[-1]}}) order = db["orders"].find_one({"_id": order_uuid}) make_log( db, action="edit", data=order, data_type="order", comment="Generated - add ds", user="******", ) return uuids
def gen_frontend_test_entries(db): apikey = {"salt": "abc", "key": "frontend"} apihash = utils.gen_api_key_hash(apikey["key"], apikey["salt"]) changes = [ { "_id": "u-4f2418f7-2609-43f1-8c7f-82c08e6daf26", "affiliation": "Frontend Test University", "api_key": apihash, "api_salt": apikey["salt"], "auth_ids": [f"author::frontend"], "email": f"*****@*****.**", "contact": f"*****@*****.**", "name": f"Frontend Author", "permissions": [], "url": "https://www.example.com/frontend_author", }, { "_id": "u-d54dc97d-ff9e-4e73-86bb-9c7a029e1b43", "affiliation": "Frontend Test University", "api_key": apihash, "api_salt": apikey["salt"], "auth_ids": [f"generator::frontend"], "email": f"*****@*****.**", "contact": f"*****@*****.**", "name": f"Frontend Generator", "permissions": ["DATA_EDIT"], "url": "https://www.example.com/frontend_generator", }, { "_id": "u-a5a7534b-1b49-41a5-b909-738e49cd137d", "affiliation": "Frontend Test University", "api_key": apihash, "api_salt": apikey["salt"], "auth_ids": [f"organisation::frontend"], "email": f"*****@*****.**", "contact": f"*****@*****.**", "name": f"Frontend Organisation", "permissions": ["DATA_MANAGEMENT", "USER_MANAGEMENT"], "url": "https://www.example.com/frontend_organisation", }, { "_id": "u-3a9a19a7-cd30-4c7b-b280-e35220e1a611", "affiliation": "Frontend Test University", "api_key": apihash, "api_salt": apikey["salt"], "auth_ids": [f"editor::frontend"], "email": f"*****@*****.**", "contact": f"*****@*****.**", "name": f"Frontend Editor", "permissions": ["DATA_EDIT"], "url": "https://www.example.com/frontend_editor", }, ] for entry in changes: user = structure.user() user.update(entry) db["users"].insert_one(user) make_log( db, action="add", data=user, data_type="user", comment="Generated", user="******", ) order = structure.order() changes = { "_id": "o-d4467732-8ddd-43a6-a904-5b7376f60e5c", "authors": ["u-4f2418f7-2609-43f1-8c7f-82c08e6daf26"], "generators": ["u-d54dc97d-ff9e-4e73-86bb-9c7a029e1b43"], "organisation": "u-a5a7534b-1b49-41a5-b909-738e49cd137d", "editors": ["u-3a9a19a7-cd30-4c7b-b280-e35220e1a611"], "description": "An order added for frontend tests", "title": f"Frontend Test Order", "properties": {"Type": "Frontend Test Entry"}, "tags": ["Frontend", "Test"], "datasets": ["d-79a755f1-69b0-4734-9977-ac945c4c51c1", "d-27cc1144-67bf-45b2-af21-425f9bfc7333"] } order.update(changes) db["orders"].insert_one(order) make_log( db, action="add", data=order, data_type="order", comment="Generated", user="******", ) dataset = structure.dataset() changes = { "_id": "d-79a755f1-69b0-4734-9977-ac945c4c51c1", "description": "A dataset added for frontend tests", "title": f"Frontend Test Dataset", "properties": {"Type": "Frontend Test Entry"}, "tags": ["Frontend", "Test"], } dataset.update(changes) db["datasets"].insert_one(dataset) make_log( db, action="add", data=dataset, data_type="dataset", comment="Generated", user="******", ) dataset = structure.dataset() changes = { "_id": "d-27cc1144-67bf-45b2-af21-425f9bfc7333", "description": "A dataset added for frontend tests 2", "title": f"Frontend Test Dataset 2", "properties": {"Type": "Frontend Test Entry"}, "tags": ["Frontend", "Test"], } dataset.update(changes) db["datasets"].insert_one(dataset) make_log( db, action="add", data=dataset, data_type="dataset", comment="Generated", user="******", ) collection = structure.collection() changes = { "_id": "c-21c8ecd1-9908-462f-ba84-3ca399074b36", "editors": ["u-3a9a19a7-cd30-4c7b-b280-e35220e1a611"], "description": "A collection added for frontend tests", "title": f"Frontend Test Collection", "properties": {"Type": "Frontend Test Entry"}, "tags": ["Frontend", "Test"], "datasets": ["d-79a755f1-69b0-4734-9977-ac945c4c51c1", "d-27cc1144-67bf-45b2-af21-425f9bfc7333"] } collection.update(changes) db["collections"].insert_one(collection) make_log( db, action="add", data=dataset, data_type="collection", comment="Generated", user="******", )