Browse Source

Update tag creation

oz123 10 years ago
parent
commit
2dbd86c283
2 changed files with 22 additions and 15 deletions
  1. 18 14
      pwman/data/drivers/sqlite.py
  2. 4 1
      pwman/tests/test_sqlite.py

+ 18 - 14
pwman/data/drivers/sqlite.py

@@ -418,22 +418,26 @@ class SQLite(SQLiteDatabaseNewForm):
     def add_node(self, node):
         sql = ("INSERT INTO NODE(USER, PASSWORD, URL, NOTES)"
                "VALUES(?, ?, ?, ?)")
-        self._cur.execute(sql, (node._username, node._password, node._url,
-                                node._notes))
         node_tags = list(node)
-        node, tags = node_tags[:3], node_tags[-1]
+        node, tags = node_tags[:4], node_tags[-1]
+        self._cur.execute(sql, (node))
         self._setnodetags(self._cur.lastrowid, tags)
         self._con.commit()
 
-    def _setnodetags(self, nodeid, tags):
-        """
-        for each tag of the node:
-            if tag exists:
-                get_tag_id
-            else:
-                create tag
-                get_tag_id
-        for each tag id:
-            update the look up table
-        """
+    def _get_or_create_tag(self, tagcipher):
+        sql_search = "SELECT ID FROM TAG WHERE DATA LIKE (?)"
+        self._cur.execute(sql_search, ([tagcipher]))
+        if self._cur.fetchone():
+            return self._cur.lastrowid
+        else:
+            sql_insert = "INSERT INTO TAG(DATA) VALUES(?)"
+            self._cur.execute(sql_insert, ([tagcipher]))
+            return self._cur.lastrowid
+
+    def _update_tag_lookup(self, nodeid, tid):
         pass
+
+    def _setnodetags(self, nodeid, tags):
+        for tag in tags:
+            tid = self._get_or_create_tag(tag)
+            self._update_tag_lookup(nodeid, tid)

+ 4 - 1
pwman/tests/test_sqlite.py

@@ -55,7 +55,10 @@ class TestSQLite(unittest.TestCase):
         # clearly this fails, while alice is not found in clear text in the
         # database!
         ce = CryptoEngine.get()
-        self.assertIn(ce.encrypt(u'alice'), rv.fetchone()[1])
+        res = rv.fetchone()
+        self.assertIn(ce.encrypt(u'alice'), res[1])
+        self.db._get_or_create_tag(node._tags[0])
+        self.db._get_or_create_tag(node._tags[0])
 
     def tearDown(self):
         self.db.close()