annotate lookup.py @ 23:d6a09e8efa56

old diffs Ignore-this: ff070c2de9eca9e3d9307bf9345debbb
author drewp@bigasterisk.com
date Sun, 22 May 2016 16:38:54 -0700
parents fa55f4439977
children ab9a6132529a
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(','))
22
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
119 if haveTags and len(haveTags[-1]) > 0:
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
120 haveTags, partialTerm = haveTags[:-1], haveTags[-1]
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
121 else:
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
122 partialTerm = ""
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
123
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
124 out = []
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
125 for t in allTags(params.user, withTags=haveTags):
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
126 if partialTerm and partialTerm not in t['label']:
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
127 continue
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
128 out.append({'id': t['label'],
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
129 'text': "%s (%s%s)" % (t['label'],
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
130 t['count'],
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
131 " left" if haveTags else "")})
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
132
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
133 return {'tags' : out}
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
134
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
135 @bottle.route('/<user>/')
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
136 def userSlash(user):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
137 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
138
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
139 @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
140 def userAllJson(user):
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
141 data = recentLinks(user, [])
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
142 data['toRoot'] = "."
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
143 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
144
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
145 @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
146 def userAll(user):
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
147 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
148
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
149
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
150 @bottle.route('/<user>', method='POST')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
151 def userAddLink(user):
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
152 if getUser()[0] != user:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
153 raise ValueError("not logged in as %s" % user)
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
154 print repr(bottle.request.params.__dict__)
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
155 doc = links.fromPostdata(bottle.request.params,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
156 user,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
157 datetime.datetime.now(tzlocal()))
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
158 links.insertOrUpdate(doc)
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
159
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
160 print "notify about sharing to", repr(doc['shareWith'])
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
161
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
162 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
163
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
164 def parseTags(tagComponent):
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
165 # 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
166 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
167
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
168 @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
169 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
170 tags = parseTags(tags)
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
171 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
172 data['toRoot'] = ".."
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
173 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
174
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
175
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
176 @bottle.route('/<user>/<tags>')
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
177 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
178 tags = parseTags(tags)
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
179 data = recentLinks(user, tags)
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
180 data['loginBar'] = getLoginBar()
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
181 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
182 data['toRoot'] = toRoot
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
183 data['allTags'] = allTags(user)
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
184 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
185 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
186
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
187 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
188 data['stats']['template'] = 'TEMPLATETIME'
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
189 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
190
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
191 @bottle.route('/templates')
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
192 def templates():
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
193 return json.dumps({'linklist': renderer.load_template("linklist.jade")})
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
194
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
195 @bottle.route('/')
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
196 def root():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
197 data = {
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
198 'loginBar': getLoginBar(),
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
199 'toRoot': ".",
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
200 'stats': {'template': 'TEMPLATETIME'},
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
201 '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
202 }
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
203 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
204
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
205 if __name__ == '__main__':
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
206 bottle.run(server='gunicorn', host='0.0.0.0', port=10002, workers=4)