annotate lookup.py @ 24:ab9a6132529a

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