annotate lookup.py @ 25:e02fc021ab89

rm old siteRoot variable Ignore-this: 9113eeea8df3b32648b781035de98878
author drewp@bigasterisk.com
date Tue, 24 May 2016 23:06:52 -0700
parents ab9a6132529a
children 7c82ffbca5d0
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 """
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
11 import pymongo, bottle, time, urllib, datetime, json, restkit, logging
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)
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
23 log = logging.getLogger()
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
24
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
25 def getLoginBar():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
26 openidProxy = restkit.Resource("http://bang:9023/")
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
27 return openidProxy.get("_loginBar",
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
28 headers={
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
29 "Cookie" : bottle.request.headers.get('cookie'),
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
30 'x-site': 'http://bigasterisk.com/openidProxySite/href',
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
31 }).body_string()
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
32
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
33 def getUser():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
34 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
35 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
36 return username, agent
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
37
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
38 def siteRoot():
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
39 try:
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
40 return bottle.request.headers['x-site-root'].rstrip('/')
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
41 except KeyError:
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
42 log.warn(repr(bottle.request.__dict__))
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
43 raise
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
44
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
45 @bottle.route('/static/<path:path>')
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
46 def server_static(path):
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
47 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
48
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
49 def recentLinks(user, tags, allowEdit):
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
50 out = {'links':[]}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
51 t1 = time.time()
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
52 spec = {'user':user}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
53 if tags:
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
54 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
55 for doc in db['links'].find(spec, sort=[('t', -1)], limit=50):
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
56 link = links.forDisplay(doc)
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
57 link['allowEdit'] = allowEdit
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
58 out['links'].append(link)
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
59 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
60 return out
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
61
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
62 def allTags(user, withTags=[]):
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
63 """withTags limits results to other tags that have been used with
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
64 those tags"""
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
65 withTags = set(withTags)
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
66 count = defaultdict(lambda: 0) # tag : count
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
67 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
68 docTags = set(doc.get('extracted', {}).get('tags', []))
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
69 if withTags and not withTags.issubset(docTags):
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
70 continue
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
71 for t in docTags.difference(withTags):
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
72 count[t] = count[t] + 1
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
73 byFreq = [(n, t) for t,n in count.iteritems()]
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
74 byFreq.sort(key=lambda (n,t): (-n, t))
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
75 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
76
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
77 def renderWithTime(name, data):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
78 t1 = time.time()
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
79 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
80 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
81 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
82 return rendered
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
83
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
84 @bottle.route('/addLink')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
85 def addLink():
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
86 out = {
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
87 'toRoot': siteRoot(),
25
e02fc021ab89 rm old siteRoot variable
drewp@bigasterisk.com
parents: 24
diff changeset
88 'absRoot': siteRoot(),
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
89 'user': getUser()[0],
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
90 'withKnockout': True,
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
91 '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
92 'loginBar': getLoginBar(),
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
93 }
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
94 return renderWithTime('add.jade', out)
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
95
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
96 @bottle.route('/addOverlay')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
97 def addOverlay():
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
98 p = bottle.request.params
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
99
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
100 return ""
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
101
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
102
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
103 @bottle.route('/addLink/proposedUri')
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
104 def proposedUri():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
105 uri = bottle.request.params.uri
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
106 user, _ = getUser()
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
107
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
108 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
109 prevDoc = links.find(uri, user)
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
110 except NotFound:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
111 prevDoc = None
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
112
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
113 return {
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
114 '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
115 'tag' : prevDoc['tag'] if prevDoc else '',
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
116 'extended' : prevDoc['extended'] if prevDoc else '',
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
117 'shareWith' : prevDoc.get('shareWith', []) if prevDoc else [],
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
118 'suggestedTags': ['tag1', 'tag2'],
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
119 'existed': prevDoc is not None,
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
120 }
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
121
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
122 if 0:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
123 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
124
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
125
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
126 @bottle.route('/tags')
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
127 def tagFilterComplete():
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
128 params = bottle.request.params
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
129 haveTags = filter(None, params['have'].split(','))
22
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
130 if haveTags and len(haveTags[-1]) > 0:
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
131 haveTags, partialTerm = haveTags[:-1], haveTags[-1]
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
132 else:
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
133 partialTerm = ""
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
134
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
135 out = []
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
136 for t in allTags(params.user, withTags=haveTags):
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
137 if partialTerm and partialTerm not in t['label']:
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
138 continue
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
139 out.append({'id': t['label'],
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
140 'text': "%s (%s%s)" % (t['label'],
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
141 t['count'],
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
142 " left" if haveTags else "")})
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
143
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
144 return {'tags' : out}
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
145
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
146 @bottle.route('/<user>/')
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
147 def userSlash(user):
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
148 bottle.redirect(siteRoot() + "/%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
149
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
150 @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
151 def userAllJson(user):
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
152 data = recentLinks(user, [], allowEdit=getUser()[0] == user)
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
153 data['toRoot'] = siteRoot()
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
154 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
155
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
156 @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
157 def userAll(user):
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
158 return userLinks(user, "")
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
159
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
160
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
161 @bottle.route('/<user>', method='POST')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
162 def userAddLink(user):
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
163 if getUser()[0] != user:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
164 raise ValueError("not logged in as %s" % user)
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
165 print repr(bottle.request.params.__dict__)
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
166 doc = links.fromPostdata(bottle.request.params,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
167 user,
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
168 datetime.datetime.now(tzlocal()))
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
169 links.insertOrUpdate(doc)
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
170
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
171 print "notify about sharing to", repr(doc['shareWith'])
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
172
25
e02fc021ab89 rm old siteRoot variable
drewp@bigasterisk.com
parents: 24
diff changeset
173 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
174
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
175 def parseTags(tagComponent):
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
176 # 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
177 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
178
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
179 @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
180 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
181 tags = parseTags(tags)
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
182 data = recentLinks(user, tags, allowEdit=getUser()[0] == user)
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
183 data['toRoot'] = siteRoot()
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
184 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
185
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 @bottle.route('/<user>/<tags>')
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
188 def userLinks(user, tags):
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
189 tags = parseTags(tags)
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
190 log.info('userLinks user=%r tags=%r', user, tags)
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
191 data = recentLinks(user, tags, allowEdit=getUser()[0] == user)
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
192 data['loginBar'] = getLoginBar()
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
193 data['desc'] = ("%s's recent links" % user) + (" tagged %s" % (tags,) if tags else "")
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
194 data['toRoot'] = siteRoot()
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
195 data['allTags'] = allTags(user)
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
196 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
197 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
198
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
199 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
200 data['stats']['template'] = 'TEMPLATETIME'
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
201 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
202
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
203 @bottle.route('/templates')
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
204 def templates():
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
205 return json.dumps({'linklist': renderer.load_template("linklist.jade")})
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
206
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
207 @bottle.route('/')
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
208 def root():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
209 data = {
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
210 'loginBar': getLoginBar(),
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
211 'toRoot': siteRoot(),
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
212 'stats': {'template': 'TEMPLATETIME'},
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
213 '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
214 }
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
215 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
216
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
217 if __name__ == '__main__':
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
218 logging.basicConfig(level=logging.INFO)
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
219 bottle.run(server='gunicorn', host='0.0.0.0', port=10002, workers=4)