Revision 7218b9c1
Added by dsorber almost 12 years ago
| software/bookmark_library/lib/bookmark.py | ||
|---|---|---|
|
|
||
|
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
|
||
|
|
||
| ... | ... | |
|
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
|
||
| ... | ... | |
|
'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
|
||
| software/bookmark_library/lib/tag.py | ||
|---|---|---|
|
|
||
|
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):
|
||
| ... | ... | |
|
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()
|
||
|
|
||
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).