about summary refs log tree commit diff
path: root/gn3/db/case_attributes.py
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/db/case_attributes.py')
-rw-r--r--gn3/db/case_attributes.py35
1 files changed, 26 insertions, 9 deletions
diff --git a/gn3/db/case_attributes.py b/gn3/db/case_attributes.py
index 9f34ed5..afebb7f 100644
--- a/gn3/db/case_attributes.py
+++ b/gn3/db/case_attributes.py
@@ -81,15 +81,18 @@ def queue_edit(cursor, directory: Path, edit: CaseAttributeEdit) -> Optional[int
         return _id
 
 
-
-def update_case_attribute_table(cursor, edit: CaseAttributeEdit) -> int:
-    # Check for ID in LMDB.  If it exists:
-    # 1. Update the case_attributes in mysql
-    # 2. Update the review list, remove from lmdb, and create a new approved set.
-    # 3.
+def update_case_attribute(cursor, directory: Path,
+                          change_id: int, edit: CaseAttributeEdit) -> int:
+    directory = f"{directory}/case-attributes/{edit.inbredset_id}"
+    if not os.path.exists(directory):
+        os.makedirs(directory)
+    env = lmdb.open(directory, map_size=8_000_000)  # 1 MB
+    modifications = {}
+    if edit.changes.get("Modifications").get("Current"):
+        modifications = edit.get("Modifications").get("Current")
     for strain, changes in modifications.items():
         for case_attribute, value in changes.items():
-            value = value.strip()
+            value = str(value).strip()
             cursor.execute("SELECT Id AS StrainId, Name AS StrainName FROM Strain "
                            "WHERE Name = %s",
                            (strain,))
@@ -98,11 +101,25 @@ def update_case_attribute_table(cursor, edit: CaseAttributeEdit) -> int:
             cursor.execute("SELECT CaseAttributeId, Name AS CaseAttributeName "
                            "FROM CaseAttribute WHERE InbredSetId = %s "
                            "AND Name = %s",
-                           (inbredset_id, case_attribute,))
+                           (inbredset_id, edit.inbredset_id,))
             case_attr_id, _ = cursor.fetchone()
             cursor.execute(
                 "INSERT INTO CaseAttributeXRefNew"
                 "(InbredSetId, StrainId, CaseAttributeId, Value) "
                 "VALUES (%s, %s, %s, %s) "
                 "ON DUPLICATE KEY UPDATE Value=VALUES(value)",
-                (inbredset_id, strain_id, case_attr_id, value,))
+                (edit.inbredset_id, strain_id, case_attr_id, value,))
+            cursor.execute(
+                "UPDATE caseattributes_audit SET ",
+                "status = %s WHERE id = %s",
+                (str(edit.status), change_id,))
+            with env.begin(write=True) as txn:
+                review_ids, approved_ids = set(), set()
+                if reviews := txn.get(b"review"):
+                    review_ids = pickle.loads(reviews)
+                    review_ids.remove(change_id)
+                if approvals := txn.get(b"review"):
+                    approved_ids = pickle.loads(approvals)
+                    approved_ids.add(change_id)
+                txn.put(b"review", pickle.dumps(review_ids))
+                txn.put(b"approved", pickle.dumps(approved_ids))