Coverage for bookie.models : 88%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""Sqlalchemy Models for objects stored with Bookie"""
"""Called by the app on startup to setup bindings to the DB"""
settings.get('fulltext.index'))
# setup the User relation, we've got import race conditions, ugh backref="bmark")
"""Method to turn an SA instance into a dict so we can output to json"""
"""We need to treat datetime's special to get them to json""" else:
else:
"""Returns an iterable that supports .next() so we can do dict(sa_instance)
"""
"""Merge in items in the values dict into our object
if it's one of our columns
""" for col in self.__table__.columns: if col.name in values: setattr(self, col.name, values[col.name])
# Setup the SQLAlchemy database engine
'bmark_tags', Base.metadata, Column('bmark_id', Integer, ForeignKey('bmarks.bid'), primary_key=True), Column('tag_id', Integer, ForeignKey('tags.tid'), primary_key=True) )
"""Exception class for erroring when a bookmark is not a valid one."""
"""Handle all non-instance related tags functions"""
def from_string(tag_str): """Split a list of tags in string form to instances
Currently it only supports space delimited
""" return {}
# any tags left in the list are new
"""Find all of the tags in the system"""
# limit to only the tag names in this list
# then we'll need to bind to bmarks to be able to limit on the # username field bmark = aliased(Bmark) qry = qry.join((bmark, Tag.bmark)).\ filter(bmark.username == username)
qry = qry.order_by(order_by) else:
"""Find all of the tags that begin with prefix
:param current: a list of current tags to compare with
If we provide a current then we should only complete tags that have bookmarks with the current tag AND starts with the new prefix. In this way when filtering tags we only complete things that make sense to complete
"""
# if we have a username limit to only bookmarks of that user
else: # things get a bit more complicated """ SELECT DISTINCT(tag_id), tags.name FROM bmark_tags JOIN tags ON bmark_tags.tag_id = tags.tid WHERE bmark_id IN ( SELECT bmark_id FROM bmark_tags WHERE tag_id IN ( SELECT DISTINCT(t.tid) FROM tags t WHERE t.name in ('vagrant', 'tips') ) ) AND tags.name LIKE ('ub%'); """ filter(Tag.name.in_(current)).group_by(Tag.tid)
filter(Bmark.tags.any(Tag.tid.in_(current_tags))).\ group_by(Bmark.bid)
filter(Tag.name.startswith(prefix)).\ filter(Tag.bmark.any(Bmark.bid.in_(good_bmarks)))
"""Find suggestions for tags for a bookmark
The plan: Suggest recent tags if there's a recent bookmark to pull tags from Suggest related tags if there are other tags in bookmarks related somehow (tbd) Suggested other tags based on other people bookmarking this url
"""
# find the tags from the most recent bookmark if available
tag_suggest.extend(recent.tag_str.split(" "))
def count(): """Count how many tags we have in the system"""
"""Bookmarks can have many many tags"""
"""Handle non-instance model issues for readable"""
"""Handle the storing of the readable version of the page content"""
ForeignKey('bmarks.bid'), primary_key=True) ForeignKey('bmarks.hash_id'), index=True)
else: return u""
# Background the process of fulltext indexing this bookmark's content. target.bmark.bid, target.clean_content)
"""Manage non-instance methods of Hashed objects"""
"""Count how many unique hashed urls we've got.""" return Hashed.query.count()
def get_by_url(url): """Return a hashed object for the url specified""" else:
"""The hashed url string and some metadata"""
"""We'll auto hash the id for them and set this up"""
"""Class to handle non-instance Bmark functions""" """Get a bmark from the system via the url""" # normalize the url
options(contains_eager(Bmark.hashed)).\ filter(Hashed.url == clean_url)
"""Get a bmark from the system via the hash_id""" # normalize the url options(contains_eager(Bmark.hashed)).\ filter(Hashed.hash_id == hash_id)
"""Get the last bookmark a user submitted
Only check for a recent one, last 3 hours
"""
with_content=False, with_tags=True): """Search for specific sets of bookmarks"""
qry = qry.outerjoin(Bmark.readable).\ options(contains_eager(Bmark.readable))
order_by = Bmark.stored.desc()
limit(limit).\ offset(offset).\ from_self()
qry = qry.join(Bmark.tags).\ options(contains_eager(Bmark.tags))
if isinstance(tags, str): qry = qry.filter(Tag.name == tags) qry = qry.order_by(order_by).\ limit(limit).\ offset(offset).\ from_self() else: bids_we_want = select( [bmarks_tags.c.bmark_id.label('good_bmark_id')], from_obj=[ bmarks_tags.join( 'tags', and_(Tag.name.in_(tags), bmarks_tags.c.tag_id == Tag.tid) ). join('bmarks', Bmark.bid == bmarks_tags.c.bmark_id) ]).\ group_by(bmarks_tags.c.bmark_id, Bmark.stored).\ having( func.count(bmarks_tags.c.tag_id) >= len(tags) ).order_by(Bmark.stored.desc())
qry = qry.join( ( bids_we_want.limit(limit).offset(offset).alias('bids'), Bmark.bid == bids_we_want.c.good_bmark_id ) )
# now outer join with the tags again so that we have the # full list of tags for each bmark we filterd down to options(contains_eager(Bmark.tags))
# join to hashed so we always have the url # if we have with_content, this is already done
def user_dump(username): """Get a list of all of the user's bookmarks for an export dump usually
""" options( contains_eager(Bmark.tags) ).\ join(Bmark.hashed).\ options( contains_eager(Bmark.hashed) ).\ filter(Bmark.username == username).all()
"""Get a recent set of bookmarks""" qry = Bmark.query
offset = limit * page qry = qry.order_by(Bmark.stored.desc()).\ limit(limit).\ offset(offset).\ from_self()
if with_tags: qry = qry.outerjoin(Bmark.tags).\ options(contains_eager(Bmark.tags))
return qry.all()
"""Get the bookmarks by most popular first""" qry = Hashed.query
offset = limit * page qry = qry.order_by(Hashed.clicks.desc()).\ limit(limit).\ offset(offset).\ from_self()
bmark = aliased(Bmark) qry = qry.join((bmark, Hashed.bmark)).\ options(contains_eager(Hashed.bmark, alias=bmark))
tags = aliased(Tag) if with_tags: qry = qry.outerjoin((tags, bmark.tags)).\ options(contains_eager(Hashed.bmark, bmark.tags, alias=tags)) res = qry.all() return res
"""Store a bookmark
:param url: bookmarked url :param desc: the one line description :param ext: the extended description/notes :param dt: The original stored time of this bmark :param fulltext: an instance of a fulltext handler
"""
url, username, desc=desc, ext=ext, tags=tags, )
# if we have a dt then manually set the stored value
"""Get a list of the hash_ids we have stored"""
"""How many bookmarks are there
:param username: should we limit to a username?
"""
"""Some stupid tools to help work with bookmarks"""
def normalize_url(url): """We need to clean the url so that we can easily find/check for dupes
Things to do: - strip any trailing spaces - Leave any query params, but think about removing common ones like google analytics stuff utm_*
""" # url = url.strip().strip('/')
"""Basic bookmark table object"""
# this could be chrome_extension, firefox_extension, website, browser XX, # import, etc nullable=False,)
# DON"T USE
Tag, backref="bmark", collection_class=attribute_mapped_collection('name'), secondary=bmarks_tags, lazy='joined', innerjoin=False, )
backref="bmark", uselist=False )
backref="bmark", cascade="all, delete, delete-orphan", primaryjoin="Readable.bid == Bmark.bid", uselist=False)
"""Create a new bmark instance
:param url: string of the url to be added as a bookmark
:param desc: Description field, optional :param ext: Extended desc field, optional :param tags: Space sep list of Bookmark tags, optional
""" # if we already have this url hashed, get that hash
else:
# tags are space separated else:
return "<Bmark: {0}:{1}>".format(self.bid, self.hashed.url)
"""Generate a single spaced string of our tags"""
"""Given a tag string, split and update our tags to be these"""
"""Update things before insert/update for fulltext needs"""
"""Update things before insert/update for the fulltext needs
""" content = target.readable.clean_content
# Background the process of fulltext indexing this bookmark's content.
|