Revision 137b22ce
Added by dsorber almost 12 years ago
| software/bookmark_library/lib/bookmark.py | ||
|---|---|---|
|
import datetime
|
||
|
import re
|
||
|
from urllib.request import urlopen
|
||
|
|
||
|
import dateutil.parser
|
||
|
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_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=?"
|
||
| ... | ... | |
|
return repr(self.value)
|
||
|
|
||
|
|
||
|
def _format_date(date):
|
||
|
if date is not None:
|
||
|
return date.isoformat()
|
||
|
else:
|
||
|
return date
|
||
|
|
||
|
|
||
|
class Bookmark(object):
|
||
|
|
||
|
def __init__(self, db_conn, id=None, url=None):
|
||
| ... | ... | |
|
self.title = ''
|
||
|
self.description = ''
|
||
|
self.times_visited = 0
|
||
|
self.last_visited = 0
|
||
|
self.last_reachable = 0
|
||
|
self.last_visited = None
|
||
|
self.last_reachable = None
|
||
|
self.date_added = None
|
||
|
self.deleted = False
|
||
|
self.category_tags = set()
|
||
|
self.description_tags = set()
|
||
| ... | ... | |
|
self.title = row[2]
|
||
|
self.description = row[3]
|
||
|
self.times_visited = int(row[4])
|
||
|
self.last_visited = row[5]
|
||
|
self.last_reachable = row[6]
|
||
|
if int(row[7]) == 0:
|
||
|
if row[5]:
|
||
|
self.last_visited = dateutil.parser.parse(row[5])
|
||
|
if row[6]:
|
||
|
self.last_reachable = dateutil.parser.parse(row[6])
|
||
|
self.date_added = dateutil.parser.parse(row[7])
|
||
|
if int(row[8]) == 0:
|
||
|
self.deleted = False
|
||
|
else:
|
||
|
self.deleted = True
|
||
| ... | ... | |
|
for row in db.execute(SQL_SELECT_DESC_TAGS, (self.id,)):
|
||
|
self.description_tags.add(Tag(int(row[0])))
|
||
|
|
||
|
# Store hash of set for dirty comparison
|
||
|
# Store hash of tag sets for dirty comparison
|
||
|
self._cat_tags_hash = hash(frozenset(self.category_tags))
|
||
|
self._desc_tags_hash = hash(frozenset(self.description_tags))
|
||
|
|
||
| ... | ... | |
|
""" Customize setting data attributes so we can tell which ones
|
||
|
are dirty.
|
||
|
"""
|
||
|
# The date_added attribute is immutable once it has been set
|
||
|
if hasattr(self, 'date_added') and name == 'date_added':
|
||
|
if getattr(self, name) is not None:
|
||
|
return
|
||
|
|
||
|
# Check if the attr is a data attribute then check if the value has
|
||
|
# changed. If so, mark the attribute as dirty
|
||
| ... | ... | |
|
|
||
|
def _db_insert(self):
|
||
|
""" Insert a new bookmark record into the database. """
|
||
|
self.date_added = datetime.datetime.now()
|
||
|
|
||
|
db = self._db_conn.cursor()
|
||
|
values = (None, self.url, self.title, self.description,
|
||
|
self.times_visited, self.last_visited, self.last_reachable,
|
||
|
self.times_visited, _format_date(self.last_visited),
|
||
|
_format_date(self.last_reachable),
|
||
|
_format_date(self.date_added),
|
||
|
self.deleted and '1' or '0')
|
||
|
db.execute(SQL_INSERT_BOOKMARK, values)
|
||
|
|
||
| ... | ... | |
|
for key in self._dirty.keys():
|
||
|
if self._dirty[key] == True:
|
||
|
sets.append('{:s}=?'.format(key))
|
||
|
values.append(getattr(self, key))
|
||
|
if key in ['last_reachable', 'last_visited']:
|
||
|
# Datetime objects need to be formated before update
|
||
|
values.append(_format_date(getattr(self, key)))
|
||
|
else:
|
||
|
values.append(getattr(self, key))
|
||
|
|
||
|
if values and sets:
|
||
|
# Add the record id as the last value
|
||
| ... | ... | |
|
sql = SQL_UPDATE_BOOKMARK.format(','.join(sets))
|
||
|
db = self._db_conn.cursor()
|
||
|
db.execute(sql, values)
|
||
|
self._db_conn.commit()
|
||
|
|
||
|
# Reset dirty flags
|
||
|
for key in self._dirty.keys():
|
||
| ... | ... | |
|
if self.description_tags_dirty:
|
||
|
update_tag_lists(SQL_DELETE_DESC_TAGS, SQL_INSERT_DESC_TAGS,
|
||
|
self.description_tags)
|
||
|
|
||
|
self._db_conn.commit()
|
||
|
|
||
|
@property
|
||
|
def category_tags_dirty(self):
|
||
| ... | ... | |
|
if self.url:
|
||
|
response = urlopen(self.url)
|
||
|
if response.getcode() == HTTP_OKAY:
|
||
|
self.last_reachable = datetime.datetime.now()
|
||
|
return True
|
||
|
return False
|
||
|
|
||
|
|
||
|
def visit(self):
|
||
|
""" Increment the visit count and update the last visited datestamp """
|
||
|
self.times_visited += 1
|
||
|
self.last_visited = datetime.datetime.now()
|
||
Added a few things including a "date_added" column. Also adding a blank database so I don't have to recreate it. I'm proabably going to work on an import script next, then the web GUI. I really should write some unit tests at some point.