annotate lookup.py @ 21:8008ec2fd763

fix up link page reloading. tried davisjs; may not need it Ignore-this: d0021609f019f0734e779a61e3e73b62
author Drew Perttula <drewp@bigasterisk.com>
date Thu, 11 Jul 2013 00:45:55 -0700
parents a8887fb93676
children fa55f4439977
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
1 #!bin/python
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
2 """
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
3 serve some queries over bookmarks:
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
5 /user
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6 /user/tag+tag+tag
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
7
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
8 and the add-bookmark stuff
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
9
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
10 """
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
11 import pymongo, bottle, time, urllib, datetime, json, restkit
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
12 from collections import defaultdict
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
13 from urllib2 import urlparse
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14 from dateutil.tz import tzlocal
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15 from bottle import static_file
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
16 from jadestache import Renderer
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
17 from pagetitle import PageTitle
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
18 from link import Links, NotFound
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
19 db = pymongo.Connection('bang', tz_aware=True)['href']
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
20 pageTitle = PageTitle(db)
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
21 links = Links(db)
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
22 renderer = Renderer(search_dirs=['template'], debug=bottle.DEBUG)
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
24 siteRoot = 'https://bigasterisk.com/href'
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
25
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
26 def getLoginBar():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
27 openidProxy = restkit.Resource("http://bang:9023/")
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
28 return openidProxy.get("_loginBar",
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
29 headers={"Cookie" : bottle.request.headers.get('cookie')}).body_string()
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
30
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
31 def getUser():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
32 agent = bottle.request.headers.get('x-foaf-agent', None)
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
33 username = db['user'].find_one({'_id':agent})['username'] if agent else None
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
34 return username, agent
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
35
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
36 @bottle.route('/static/<path:path>')
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
37 def server_static(path):
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
38 return static_file(path, root='static')
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
39
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
40 def recentLinks(user, tags=None):
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
41 out = {'links':[]}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
42 t1 = time.time()
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
43 spec = {'user':user}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
44 if tags:
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
45 spec['extracted.tags'] = {'$all' : tags}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
46 for doc in db['links'].find(spec, sort=[('t', -1)], limit=50):
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
47 out['links'].append(links.forDisplay(doc))
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
48 out['stats'] = {'queryTimeMs' : round((time.time() - t1) * 1000, 2)}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
49 return out
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
50
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
51 def allTags(user, withTags=[]):
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
52 """withTags limits results to other tags that have been used with
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
53 those tags"""
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
54 withTags = set(withTags)
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
55 count = defaultdict(lambda: 0) # tag : count
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
56 for doc in db['links'].find({'user':user}, fields=['extracted.tags']):
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
57 docTags = set(doc.get('extracted', {}).get('tags', []))
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
58 if withTags and not withTags.issubset(docTags):
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
59 continue
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
60 for t in docTags.difference(withTags):
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
61 count[t] = count[t] + 1
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
62 byFreq = [(n, t) for t,n in count.iteritems()]
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
63 byFreq.sort(key=lambda (n,t): (-n, t))
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
64 return [{'label': t, 'count': n} for n, t in byFreq]
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
65
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
66 def renderWithTime(name, data):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
67 t1 = time.time()
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
68 rendered = renderer.render_name(name, data)
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
69 dt = (time.time() - t1) * 1000
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
70 rendered = rendered.replace('TEMPLATETIME', "%.02f ms" % dt)
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
71 return rendered
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
72
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
73 @bottle.route('/addLink')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
74 def addLink():
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
75 out = {
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
76 'toRoot': '.',
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
77 'absRoot': siteRoot,
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
78 'user': getUser()[0],
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
79 'withKnockout': True,
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
80 'fillHrefJson': json.dumps(bottle.request.params.get('url', '')),
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
81 'loginBar': getLoginBar(),
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
82 }
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
83 return renderWithTime('add.jade', out)
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
84
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
85 @bottle.route('/addOverlay')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
86 def addOverlay():
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
87 p = bottle.request.params
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
88
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
89 return ""
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
90
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
91
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
92 @bottle.route('/addLink/proposedUri')
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
93 def proposedUri():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
94 uri = bottle.request.params.uri
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
95 user, _ = getUser()
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
96
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
97 try:
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
98 prevDoc = links.find(uri, user)
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
99 except NotFound:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
100 prevDoc = None
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
101
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
102 return {
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
103 'description': prevDoc['description'] if prevDoc else pageTitle.pageTitle(uri),
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
104 'tag' : prevDoc['tag'] if prevDoc else '',
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
105 'extended' : prevDoc['extended'] if prevDoc else '',
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
106 'shareWith' : prevDoc.get('shareWith', []) if prevDoc else [],
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
107 'suggestedTags': ['tag1', 'tag2'],
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
108 'existed': prevDoc is not None,
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
109 }
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
110
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
111 if 0:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
112 pass#proposal check existing links, get page title (stuff that in db), get tags from us and other serviecs. maybe the deferred ones ater
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
113
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
114
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
115 @bottle.route('/tags')
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
116 def tagFilterComplete():
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
117 params = bottle.request.params
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
118 haveTags = filter(None, params['have'].split(','))
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
119 return {'tags' : [
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
120 {'id': t['label'],
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
121 'text': "%s (%s%s)" % (t['label'], t['count'], " left" if haveTags else "")}
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
122 for t in allTags(params.user,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
123 withTags=haveTags)]}
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
124
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
125 @bottle.route('/<user>/')
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
126 def userSlash(user):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
127 bottle.redirect("/%s" % urllib.quote(user))
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
128
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
129 @bottle.route('/<user>.json', method='GET')
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
130 def userAllJson(user):
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
131 data = recentLinks(user, [])
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
132 data['toRoot'] = "."
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
133 return json.dumps(data)
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
134
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
135 @bottle.route('/<user>', method='GET')
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
136 def userAll(user):
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
137 return userLinks(user, "", toRoot=".")
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
138
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
139
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
140 @bottle.route('/<user>', method='POST')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
141 def userAddLink(user):
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
142 if getUser()[0] != user:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
143 raise ValueError("not logged in as %s" % user)
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
144 print repr(bottle.request.params.__dict__)
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
145 doc = links.fromPostdata(bottle.request.params,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
146 user,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
147 datetime.datetime.now(tzlocal()))
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
148 links.insertOrUpdate(doc)
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
149
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
150 print "notify about sharing to", repr(doc['shareWith'])
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
151
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
152 bottle.redirect(siteRoot + '/' + user)
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
153
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
154 def parseTags(tagComponent):
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
155 # the %20 is coming from davis.js, not me :(
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
156 return filter(None, tagComponent.replace("%20", "+").split('+'))
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
157
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
158 @bottle.route('/<user>/<tags:re:.*>.json')
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
159 def userLinksJson(user, tags):
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
160 tags = parseTags(tags)
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
161 data = recentLinks(user, tags)
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
162 data['toRoot'] = ".."
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
163 return json.dumps(data)
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
164
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
165
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
166 @bottle.route('/<user>/<tags>')
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
167 def userLinks(user, tags, toRoot=".."):
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
168 tags = parseTags(tags)
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
169 data = recentLinks(user, tags)
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
170 data['loginBar'] = getLoginBar()
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
171 data['desc'] = ("%s's recent links" % user) + (" tagged %s" % (tags,) if tags else "")
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
172 data['toRoot'] = toRoot
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
173 data['allTags'] = allTags(user)
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
174 data['user'] = user
20
a8887fb93676 hide share data from the public. bug in links filter box
Drew Perttula <drewp@bigasterisk.com>
parents: 16
diff changeset
175 data['showPrivateData'] = (user == getUser()[0])
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
176
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
177 data['pageTags'] = [{"word":t} for t in tags]
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
178 data['stats']['template'] = 'TEMPLATETIME'
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
179 return renderWithTime('links.jade', data)
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
180
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
181 @bottle.route('/templates')
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
182 def templates():
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
183 return json.dumps({'linklist': renderer.load_template("linklist.jade")})
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
184
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
185 @bottle.route('/')
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
186 def root():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
187 data = {
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
188 'loginBar': getLoginBar(),
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
189 'toRoot': ".",
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
190 'stats': {'template': 'TEMPLATETIME'},
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
191 'users': [{'user':doc['username']} for doc in db['user'].find()],
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
192 }
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
193 return renderWithTime('index.jade', data)
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
194
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
195 if __name__ == '__main__':
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
196 bottle.run(server='gunicorn', host='0.0.0.0', port=10002, workers=4)