From c83c2e23d9841b0f8c74cfb09b7d1b4639dad93a Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 27 Jan 2025 11:53:32 -0600 Subject: Implement editing of basic phenotype metadata. --- uploader/phenotypes/views.py | 142 ++++++++++++++++++++++++++++++------------- 1 file changed, 99 insertions(+), 43 deletions(-) (limited to 'uploader/phenotypes/views.py') diff --git a/uploader/phenotypes/views.py b/uploader/phenotypes/views.py index fe591f1..79b1605 100644 --- a/uploader/phenotypes/views.py +++ b/uploader/phenotypes/views.py @@ -592,6 +592,35 @@ def review_job_data( activelink="add-phenotypes") +def update_phenotype_metadata(conn, metadata: dict): + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute("SELECT * FROM Phenotype WHERE Id=%(phenotype-id)s", + metadata) + res = { + **{ + _key: _val for _key,_val in { + key.lower().replace("_", "-"): value + for key, value in (cursor.fetchone() or {}).items() + }.items() + if _key in metadata.keys() + }, + "phenotype-id": metadata.get("phenotype-id") + } + if res == metadata: + return False + + cursor.execute( + "UPDATE Phenotype SET " + "Pre_publication_description=%(pre-publication-description)s, " + "Post_publication_description=%(post-publication-description)s, " + "Original_description=%(original-description)s, " + "Units=%(units)s, " + "Pre_publication_abbreviation=%(pre-publication-abbreviation)s, " + "Post_publication_abbreviation=%(post-publication-abbreviation)s " + "WHERE Id=%(phenotype-id)s", + metadata) + return cursor.rowcount + @phenotypesbp.route( "/populations//phenotypes/datasets" @@ -631,46 +660,73 @@ def edit_phenotype_data( activelink="edit-phenotype") with database_connection(app.config["SQL_URI"]) as conn: - def __fetch_phenotype__(privileges): - phenotype = phenotype_by_id(conn, - species["SpeciesId"], - population["Id"], - dataset["Id"], - xref_id) - if phenotype is None: - msg = ("Could not find the phenotype with cross-reference ID" - f" '{xref_id}' from dataset '{dataset['FullName']}' " - f" from the '{population['FullName']}' population of " - f" species '{species['FullName']}'.") - return Left({"privileges": privileges, "phenotype-error": msg}) - return {"privileges": privileges, "phenotype": phenotype} - - def __fetch_publication_data__(**kwargs): - pheno = kwargs["phenotype"] - return { - **kwargs, - "publication_data": phenotype_publication_data( - conn, pheno["Id"]) - } - - def __fail__(failure_object): - # process the object - return __render__(failure_object=failure_object) - - return oauth2_post( - "/auth/resource/phenotypes/individual/linked-resource", - json={ - "species_id": species["SpeciesId"], - "population_id": population["Id"], - "dataset_id": dataset["Id"], - "xref_id": xref_id - } - ).then( - lambda resource: tuple( - privilege["privilege_id"] for role in resource["roles"] - for privilege in role["privileges"]) - ).then( - __fetch_phenotype__ - ).then( - lambda args: __fetch_publication_data__(**args) - ).either(__fail__, lambda args: __render__(**args)) + if request.method == "GET": + def __fetch_phenotype__(privileges): + phenotype = phenotype_by_id(conn, + species["SpeciesId"], + population["Id"], + dataset["Id"], + xref_id) + if phenotype is None: + msg = ("Could not find the phenotype with cross-reference ID" + f" '{xref_id}' from dataset '{dataset['FullName']}' " + f" from the '{population['FullName']}' population of " + f" species '{species['FullName']}'.") + return Left({"privileges": privileges, "phenotype-error": msg}) + return {"privileges": privileges, "phenotype": phenotype} + + def __fetch_publication_data__(**kwargs): + pheno = kwargs["phenotype"] + return { + **kwargs, + "publication_data": phenotype_publication_data( + conn, pheno["Id"]) + } + + def __fail__(failure_object): + # process the object + return __render__(failure_object=failure_object) + + return oauth2_post( + "/auth/resource/phenotypes/individual/linked-resource", + json={ + "species_id": species["SpeciesId"], + "population_id": population["Id"], + "dataset_id": dataset["Id"], + "xref_id": xref_id + } + ).then( + lambda resource: tuple( + privilege["privilege_id"] for role in resource["roles"] + for privilege in role["privileges"]) + ).then( + __fetch_phenotype__ + ).then( + lambda args: __fetch_publication_data__(**args) + ).either(__fail__, lambda args: __render__(**args)) + + ## POST + match request.form.get("submit", "invalid-action"): + case "update basic metadata": + if update_phenotype_metadata(conn, { + key: value.strip() if bool(value.strip()) else None + for key, value in request.form.items() + if key not in ("submit",) + }): + flash("Basic metadata updated.", "alert-success") + else: + flash("There were no changes to the basic metadata", + "alert-info") + case "update data": + flash("NOT IMPLEMENTED: Would update data", "alert-success") + case "update publication": + flash("NOT IMPLEMENTED: Would update publication data.", "alert-success") + case _: + flash("Invalid phenotype editing action.", "alert-danger") + + return redirect(url_for( + "species.populations.phenotypes.view_phenotype", + species_id=species["SpeciesId"], + population_id=population["Id"], + dataset_id=dataset["Id"], + xref_id=xref_id)) -- cgit v1.2.3