From ab5d06c96dda489d5d1e28f4a68d6c2aed26476b Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 24 May 2022 11:46:31 +0300 Subject: Add new endpoint for "approving" case-attributes modifications * wqflask/wqflask/metadata_edits.py (approve_case_attribute_data): New end-point. --- wqflask/wqflask/metadata_edits.py | 67 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/wqflask/wqflask/metadata_edits.py b/wqflask/wqflask/metadata_edits.py index 101e6949..1a4d6f8d 100644 --- a/wqflask/wqflask/metadata_edits.py +++ b/wqflask/wqflask/metadata_edits.py @@ -806,3 +806,70 @@ def reject_case_attribute_data(): conn.commit() return redirect(url_for("metadata_edit.show_case_attribute_columns")) + + +@metadata_edit.route("/case-attributes/approve", methods=["POST", ]) +@case_attributes_edit_access +def approve_case_attribute_data(): + case_attr_id = request.form.to_dict().get("id") + with database_connection() as conn: + with conn.cursor() as cursor: + cursor.execute( + "SELECT json_diff_data FROM caseattributes_audit " + "WHERE id = %s", (case_attr_id,) + ) + diff_data = cursor.fetchone() + if diff_data: + diff_data = json.loads(diff_data[0]) + try: + # Insert (Most Important) + if diff_data.get("Insert"): + data = diff_data.get("Insert") + cursor.execute( + "INSERT INTO CaseAttribute " + "(Name, Description) VALUES " + "(%s, %s)", + (data.get("name"), data.get("description"),)) + cursor.execute( + "UPDATE caseattributes_audit SET " + "status = 'approved' WHERE id = %s", + (case_attr_id,)) + # Delete + elif diff_data.get("Deletion"): + data = diff_data.get("Deletion") + cursor.execute( + "DELETE FROM CaseAttribute " + "WHERE Id = %s", + (data.get("id"),)) + cursor.execute( + "UPDATE caseattributes_audit SET " + "status = 'approved' WHERE id = %s", + (case_attr_id,)) + # Modification + elif diff_data.get("Modification"): + data = diff_data.get("Modification") + if (desc_ := data.get("description")): + cursor.execute( + "UPDATE CaseAttribute SET " + "Description = %s WHERE Id = %s", + (desc_.get("Current"), diff_data.get("id"),) + ) + if (name_ := data.get("name")): + cursor.execute( + "UPDATE CaseAttribute SET " + "Name = %s WHERE Id = %s", + (name_.get("Current"), diff_data.get("id"),) + ) + if cursor.rowcount: + cursor.execute( + "UPDATE caseattributes_audit SET " + "status = 'approved' WHERE id = %s", + (case_attr_id,)) + except Exception as _e: + import MySQLdb + conn.rollback() + raise MySQLdb.Error(_e) from _e + conn.commit() + return redirect(url_for("metadata_edit.show_case_attribute_columns")) + + -- cgit v1.2.3