commit 7218b9c1d8b2ce63fdfc9413081db12af4bffe26
Author: dsorber <david.sorber@gmail.com>
Date:   Wed Jul 16 22:04:08 2014 -0400

    Added a tiny bit of code tonight. Need to keep track of dirty value of tags lists using hash (convert from set to frozenset before hashing).

diff --git a/software/bookmark_library/lib/bookmark.py b/software/bookmark_library/lib/bookmark.py
index 135e53e..b260ab1 100644
--- a/software/bookmark_library/lib/bookmark.py
+++ b/software/bookmark_library/lib/bookmark.py
@@ -3,12 +3,16 @@ from urllib.request import urlopen
 
 import sqlite3
 
+from tag import Tag
+
 
 DB_PATH = 'bookmarks.db'
 
 SQL_SELECT_BOOKMARK = "SELECT * FROM bookmarks WHERE id=?"
 SQL_INSERT_BOOKMARK = "INSERT INTO bookmarks VALUES (?,?,?,?,?,?,?,?)"
 SQL_UPDATE_BOOKMARK = "UPDATE bookmarks SET {:s} WHERE id=?"
+SQL_SELECT_CAT_TAGS = "SELECT tag_id FROM category_tags where bookmark_id=?"
+SQL_SELECT_DESC_TAGS = "SELECT tag_id FROM description_tags where bookmark_id=?"
 
 HTTP_OKAY = 200
 
@@ -75,6 +79,14 @@ class Bookmark(object):
                 self.deleted = False
             else:
                 self.deleted = True
+                
+            # Grab category tags
+            for row in db.execute(SQL_SELECT_CAT_TAGS, (self.id,)):
+				self.category_tags.add(Tag(int(row[0])))
+				
+			# Grab description tags
+            for row in db.execute(SQL_SELECT_DESC_TAGS, (self.id,)):
+				self.description_tags.add(Tag(int(row[0])))
         
         # This dict MUST appear *after* the data attributes, it is used to 
         # record which data attributes are dirty
@@ -84,7 +96,7 @@ class Bookmark(object):
                        'times_visited': False,
                        'last_visited': False,
                        'last_reachable': False,
-                       'deleted': False}        
+                       'deleted': False}
     
     def __del__(self):
         """ Automatically save any changes to a record before the object is 
diff --git a/software/bookmark_library/lib/tag.py b/software/bookmark_library/lib/tag.py
index 655214c..3751821 100644
--- a/software/bookmark_library/lib/tag.py
+++ b/software/bookmark_library/lib/tag.py
@@ -2,6 +2,7 @@ import sqlite3
 
 SQL_SELECT_TAG_BY_ID = "SELECT * FROM tags WHERE id=?"
 SQL_SELECT_TAG_BY_VALUE = "SELECT * FROM tags WHERE value=?"
+SQL_INSERT_TAG = "INSERT INTO tags VALUES (?,?)"
 
 
 class Tag(object):
@@ -31,11 +32,28 @@ class Tag(object):
         elif self.value:
             # If a value was passed in, check to see if it already exists in 
             # the DB; if so set the ID accordingly
-            db = self._db_conn.cursor()
-            db.execute(SQL_SELECT_TAG_BY_VALUE, (self.value,))
-            row = db.fetchone()
-            if row:
-               self.id = int(row[0]) 
+            self.id = self._get_id_from_value() 
 
     def __str__(self, value):
         return repr(self.value)
+        
+    def _get_id_from_value(self):
+        db = self._db_conn.cursor()
+        db.execute(SQL_SELECT_TAG_BY_VALUE, (self.value,))
+        row = db.fetchone()
+        if row:
+            return int(row[0])
+        else:
+            raise Exception # id not found
+        
+    def save(self, value):
+        # Only insert a new tag if it does not already exist in the database
+        if not self.id:
+            db = self._db_conn.cursor()
+            values = (0, self.value)
+            db.execute(SQL_INSERT_TAG, values)
+            self._db_conn.commit()
+            
+            # Now set Tag object's ID
+            self.id = self._get_id_from_value()
+            
