annotate link.py @ 16:d44d3e4d415b

don't show other user's tags as previously-saved tags to the current user Ignore-this: 89491c15006254dbd5a67970b3834ac7
author Drew Perttula <drewp@bigasterisk.com>
date Sun, 17 Mar 2013 00:48:23 -0700
parents e054949143e9
children 8008ec2fd763
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
1 import urlparse, urllib
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
2 from dateutil.tz import tzlocal
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
3
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4 class NotFound(ValueError):
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
5 pass
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
7 class Links(object):
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
8 def __init__(self, db):
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
9 self.coll = db['links']
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
10
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
11 def insertOrUpdate(self, doc):
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
12 if not doc['href']:
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
13 raise ValueError("no link")
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14 self.extract(doc)
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15 self.coll.update({'href':doc['href']}, doc, upsert=True, safe=True)
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
16
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
17 def extract(self, doc):
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
18 forUsers = []
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
19 tags = []
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
20 for t in doc.get('tag', '').split(' '):
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
21 if t.startswith('for:'):
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
22 forUsers.append(t[4:])
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23 else:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
24 tags.append(t)
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
25 doc['extracted'] = dict(tags=tags, forUsers=forUsers)
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
26
16
d44d3e4d415b don't show other user's tags as previously-saved tags to the current user
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
27 def find(self, uri, user):
d44d3e4d415b don't show other user's tags as previously-saved tags to the current user
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
28 docs = list(self.coll.find({'href': uri, 'user' : user}))
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
29 if len(docs) == 0:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
30 raise NotFound("not found")
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
31 elif len(docs) > 1:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
32 raise ValueError("%s docs found for href %s" % (len(docs), uri))
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
33 else:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
34 return docs[0]
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
35
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
36 def forDisplay(self, doc):
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
37 """return a mustache-ready dict for this db doc"""
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
38 out = doc.copy()
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
39 del out['_id']
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
40 out['t'] = out['t'].astimezone(tzlocal()).isoformat()
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
41 if not out['description'].strip():
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
42 out['displayDescription'] = out['href']
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
43 else:
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
44 out['displayDescription'] = out['description']
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
45
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
46 out['tagWords'] = [{'word' : w} for w in out['tag'].split(None)]
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
47 out['domain'] = urlparse.urlparse(out['href']).netloc
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
48 out['editLink'] = 'addLink?' + urllib.urlencode([('url', out['href'])])
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
49 out['shareWith'] = [{'label' : uri} for uri in doc.get('shareWith', [])]
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
50 return out
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
51
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
52 def fromPostdata(self, data, user, t):
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
53 if not user or not data.href:
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
54 raise ValueError("incomplete")
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
55 return dict(
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
56 user=user,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
57 description=data.description,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
58 extended=data.extended,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
59 href=data.href,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
60 private=data.private,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
61 shareWith=filter(None, data.shareWith.split(',')),
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
62 tag=data.tag,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
63 t=t,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
64 )