Mercurial > code > home > repos > href
annotate link.py @ 22:fa55f4439977
fix autocomplete. less console.log
Ignore-this: 302e2080d044221c430c4eb77ccd4221
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Thu, 11 Jul 2013 01:05:22 -0700 |
parents | 8008ec2fd763 |
children | d6a09e8efa56 |
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] |
21
8008ec2fd763
fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents:
16
diff
changeset
|
35 |
8008ec2fd763
fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents:
16
diff
changeset
|
36 def filter(self, user, startTime): |
8008ec2fd763
fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents:
16
diff
changeset
|
37 return self.coll.find({'user' : user, 't': {'$gte': startTime}}) |
5
f8c4c7ce5f4a
lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 |
10
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
39 def forDisplay(self, doc): |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
40 """return a mustache-ready dict for this db doc""" |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
41 out = doc.copy() |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
42 del out['_id'] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
43 out['t'] = out['t'].astimezone(tzlocal()).isoformat() |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
44 if not out['description'].strip(): |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
45 out['displayDescription'] = out['href'] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
46 else: |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
47 out['displayDescription'] = out['description'] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
48 |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
49 out['tagWords'] = [{'word' : w} for w in out['tag'].split(None)] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
50 out['domain'] = urlparse.urlparse(out['href']).netloc |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
51 out['editLink'] = 'addLink?' + urllib.urlencode([('url', out['href'])]) |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
52 out['shareWith'] = [{'label' : uri} for uri in doc.get('shareWith', [])] |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
53 return out |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
54 |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
55 def fromPostdata(self, data, user, t): |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
56 if not user or not data.href: |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
57 raise ValueError("incomplete") |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
58 return dict( |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
59 user=user, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
60 description=data.description, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
61 extended=data.extended, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
62 href=data.href, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
63 private=data.private, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
64 shareWith=filter(None, data.shareWith.split(',')), |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
65 tag=data.tag, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
66 t=t, |
e054949143e9
reworking addlink and shareWith support
drewp@bigasterisk.com
parents:
5
diff
changeset
|
67 ) |