diff link.py @ 10:e054949143e9

reworking addlink and shareWith support Ignore-this: b1665b776f3964f7fde219acadc51f32
author drewp@bigasterisk.com
date Fri, 15 Mar 2013 00:29:53 -0700
parents f8c4c7ce5f4a
children d44d3e4d415b
line wrap: on
line diff
--- a/link.py	Wed Mar 06 23:23:18 2013 -0800
+++ b/link.py	Fri Mar 15 00:29:53 2013 -0700
@@ -1,3 +1,5 @@
+import urlparse, urllib
+from dateutil.tz import tzlocal
 
 class NotFound(ValueError):
     pass
@@ -7,6 +9,8 @@
         self.coll = db['links']
         
     def insertOrUpdate(self, doc):
+        if not doc['href']:
+            raise ValueError("no link")
         self.extract(doc)
         self.coll.update({'href':doc['href']}, doc, upsert=True, safe=True)
 
@@ -29,3 +33,32 @@
         else:
             return docs[0]
             
+    def forDisplay(self, doc):
+        """return a mustache-ready dict for this db doc"""
+        out = doc.copy()
+        del out['_id']
+        out['t'] = out['t'].astimezone(tzlocal()).isoformat()
+        if not out['description'].strip():
+            out['displayDescription'] = out['href']
+        else:
+            out['displayDescription'] = out['description']
+
+        out['tagWords'] = [{'word' : w} for w in out['tag'].split(None)]
+        out['domain'] = urlparse.urlparse(out['href']).netloc
+        out['editLink'] = 'addLink?' + urllib.urlencode([('url', out['href'])])
+        out['shareWith'] = [{'label' : uri} for uri in doc.get('shareWith', [])]
+        return out
+
+    def fromPostdata(self, data, user, t):
+        if not user or not data.href:
+            raise ValueError("incomplete")
+        return dict(
+            user=user,
+            description=data.description,
+            extended=data.extended,
+            href=data.href,
+            private=data.private,
+            shareWith=filter(None, data.shareWith.split(',')),
+            tag=data.tag,
+            t=t,
+        )