view link.py @ 6:ce779bdd2fb3

links to peers Ignore-this: 31f60d68f279b53eee2f38b1f570c6c9
author Drew Perttula <drewp@bigasterisk.com>
date Fri, 01 Mar 2013 01:24:05 -0800
parents f8c4c7ce5f4a
children e054949143e9
line wrap: on
line source


class NotFound(ValueError):
    pass

class Links(object):
    def __init__(self, db):
        self.coll = db['links']
        
    def insertOrUpdate(self, doc):
        self.extract(doc)
        self.coll.update({'href':doc['href']}, doc, upsert=True, safe=True)

    def extract(self, doc):
        forUsers = []
        tags = []
        for t in doc.get('tag', '').split(' '):
            if t.startswith('for:'):
                forUsers.append(t[4:])
            else:
                tags.append(t)
        doc['extracted'] = dict(tags=tags, forUsers=forUsers)

    def find(self, uri):
        docs = list(self.coll.find({'href': uri}))
        if len(docs) == 0:
            raise NotFound("not found")
        elif len(docs) > 1:
            raise ValueError("%s docs found for href %s" % (len(docs), uri))
        else:
            return docs[0]