comparison 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
comparison
equal deleted inserted replaced
9:7df920c18c83 10:e054949143e9
1 import urlparse, urllib
2 from dateutil.tz import tzlocal
1 3
2 class NotFound(ValueError): 4 class NotFound(ValueError):
3 pass 5 pass
4 6
5 class Links(object): 7 class Links(object):
6 def __init__(self, db): 8 def __init__(self, db):
7 self.coll = db['links'] 9 self.coll = db['links']
8 10
9 def insertOrUpdate(self, doc): 11 def insertOrUpdate(self, doc):
12 if not doc['href']:
13 raise ValueError("no link")
10 self.extract(doc) 14 self.extract(doc)
11 self.coll.update({'href':doc['href']}, doc, upsert=True, safe=True) 15 self.coll.update({'href':doc['href']}, doc, upsert=True, safe=True)
12 16
13 def extract(self, doc): 17 def extract(self, doc):
14 forUsers = [] 18 forUsers = []
27 elif len(docs) > 1: 31 elif len(docs) > 1:
28 raise ValueError("%s docs found for href %s" % (len(docs), uri)) 32 raise ValueError("%s docs found for href %s" % (len(docs), uri))
29 else: 33 else:
30 return docs[0] 34 return docs[0]
31 35
36 def forDisplay(self, doc):
37 """return a mustache-ready dict for this db doc"""
38 out = doc.copy()
39 del out['_id']
40 out['t'] = out['t'].astimezone(tzlocal()).isoformat()
41 if not out['description'].strip():
42 out['displayDescription'] = out['href']
43 else:
44 out['displayDescription'] = out['description']
45
46 out['tagWords'] = [{'word' : w} for w in out['tag'].split(None)]
47 out['domain'] = urlparse.urlparse(out['href']).netloc
48 out['editLink'] = 'addLink?' + urllib.urlencode([('url', out['href'])])
49 out['shareWith'] = [{'label' : uri} for uri in doc.get('shareWith', [])]
50 return out
51
52 def fromPostdata(self, data, user, t):
53 if not user or not data.href:
54 raise ValueError("incomplete")
55 return dict(
56 user=user,
57 description=data.description,
58 extended=data.extended,
59 href=data.href,
60 private=data.private,
61 shareWith=filter(None, data.shareWith.split(',')),
62 tag=data.tag,
63 t=t,
64 )