diff link.py @ 5:f8c4c7ce5f4a

lots of href additions: add/edit, nav fixes Ignore-this: 863335c4680ac9bcc6a7fc5867638d61
author Drew Perttula <drewp@bigasterisk.com>
date Thu, 21 Feb 2013 01:39:01 -0800
parents
children e054949143e9
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/link.py	Thu Feb 21 01:39:01 2013 -0800
@@ -0,0 +1,31 @@
+
+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]
+