Explorar o código

Node is now a container object

One can do list(node), this makes code a bit nicer.
This is also useful for the database drivers
oz123 %!s(int64=10) %!d(string=hai) anos
pai
achega
4f75cf8380
Modificáronse 3 ficheiros con 18 adicións e 8 borrados
  1. 6 5
      pwman/data/drivers/sqlite.py
  2. 6 0
      pwman/data/nodes.py
  3. 6 3
      pwman/tests/test_sqlite.py

+ 6 - 5
pwman/data/drivers/sqlite.py

@@ -418,13 +418,14 @@ class SQLite(SQLiteDatabaseNewForm):
     def add_node(self, node):
         sql = ("INSERT INTO NODE(USER, PASSWORD, URL, NOTES)"
                "VALUES(?, ?, ?, ?)")
-        self._cur.execute(sql, node[:4])
-        node = list(node)
-        node.append(self._cur.lastrowid)
-        self._setnodetags(node)
+        self._cur.execute(sql, (node._username, node._password, node._url,
+                                node._notes))
+        node_tags = list(node)
+        node, tags = node_tags[:3], node_tags[-1]
+        self._setnodetags(self._cur.lastrowid, tags)
         self._con.commit()
 
-    def _setnodetags(self, node):
+    def _setnodetags(self, nodeid, tags):
         """
         for each tag of the node:
             if tag exists:

+ 6 - 0
pwman/data/nodes.py

@@ -161,6 +161,12 @@ class Node(object):
             res += self.__dir__[item]
         return res
 
+    def __iter__(self):
+        for item in ['_username', '_password',
+                     '_url', '_notes']:
+            yield getattr(self, item)
+        yield self._tags
+
     @property
     def password(self):
         """Get the current password."""

+ 6 - 3
pwman/tests/test_sqlite.py

@@ -19,7 +19,7 @@
 import os
 import unittest
 from pwman.data.drivers.sqlite import SQLite
-
+from pwman.data.nodes import Node
 
 class TestSQLite(unittest.TestCase):
     def setUp(self):
@@ -43,8 +43,11 @@ class TestSQLite(unittest.TestCase):
         self.assertListEqual([u'foo', u'bar'], list(f))
 
     def test_3_add_node(self):
-        node = ("alice", "secret", "wonderland.com", "a really great place",
-                ['foo', 'bar'])
+        node = Node(clear_text=True,
+                    **{'username': u"alice", 'password': u"secret",
+                       'url': u"wonderland.com",
+                       'notes': u"a really great place",
+                       'tags': [u'foo', u'bar']})
         self.db.add_node(node)
         rv = self.db._cur.execute("select * from node")
         self.assertIn('alice', rv.fetchone())