annotate lookup.py @ 39:c538dc39b851

user login fixes
author drewp@bigasterisk.com
date Sat, 19 Nov 2022 17:06:36 -0800
parents f3a15a724483
children 94181d521d6d
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 """
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
2 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
3
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4 /user
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
5 /user/tag+tag+tag
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
7 and the add-bookmark stuff
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
8 """
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
9 import datetime
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
10 import json
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
11 import logging
33
b82432594778 skaffold config and other updates
drewp@bigasterisk.com
parents: 32
diff changeset
12 import os
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
13 import time
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
14 import urllib.error
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
15 import urllib.parse
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
16 import urllib.request
34
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
17 from collections import defaultdict
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
18
34
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
19 import bottle
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
20 import pymongo
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
21 from bottle import static_file
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
22 from dateutil.tz import tzlocal
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
23
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
24 from jadestache import Renderer
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
25 from link import Links, NotFound
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
26 from pagetitle import PageTitle
38
f3a15a724483 mongo api and up-checking
drewp@bigasterisk.com
parents: 35
diff changeset
27 from mongo_required import open_mongo_or_die, die_on_mongo_connection_errors
f3a15a724483 mongo api and up-checking
drewp@bigasterisk.com
parents: 35
diff changeset
28 from get_agent import bottleGetAgent
f3a15a724483 mongo api and up-checking
drewp@bigasterisk.com
parents: 35
diff changeset
29 db = open_mongo_or_die()['href']
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
30 pageTitle = PageTitle(db)
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
31 links = Links(db)
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
32 renderer = Renderer(search_dirs=['template'], debug=bottle.DEBUG)
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
33 log = logging.getLogger()
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
34
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
35
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
36 def getUser():
39
c538dc39b851 user login fixes
drewp@bigasterisk.com
parents: 38
diff changeset
37 try:
c538dc39b851 user login fixes
drewp@bigasterisk.com
parents: 38
diff changeset
38 agent = bottleGetAgent()
c538dc39b851 user login fixes
drewp@bigasterisk.com
parents: 38
diff changeset
39 username = db['user'].find_one({'_id': str(agent)})['username'] if agent else None
c538dc39b851 user login fixes
drewp@bigasterisk.com
parents: 38
diff changeset
40 except KeyError:
c538dc39b851 user login fixes
drewp@bigasterisk.com
parents: 38
diff changeset
41 username = agent = None
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
42 return username, agent
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
43
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
44
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
45 def siteRoot():
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
46 try:
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
47 return bottle.request.headers['x-site-root'].rstrip('/')
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
48 except KeyError:
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
49 log.warn(repr(bottle.request.__dict__))
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
50 raise
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
51
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
52
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
53 @bottle.route('/static/<path:path>')
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
54 def server_static(path):
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
55 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
56
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
57
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
58 def recentLinks(user, tags, allowEdit):
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
59 out = {'links': []}
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
60 t1 = time.time()
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
61 spec = {'user': user}
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
62 if tags:
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
63 spec['extracted.tags'] = {'$all': tags}
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
64 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
65 link = links.forDisplay(doc)
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
66 link['allowEdit'] = allowEdit
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
67 out['links'].append(link)
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
68 out['stats'] = {'queryTimeMs': round((time.time() - t1) * 1000, 2)}
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
69 return out
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
70
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
71
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
72 def allTags(user, withTags=[]):
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
73 """withTags limits results to other tags that have been used with
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
74 those tags"""
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
75 withTags = set(withTags)
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
76 count = defaultdict(lambda: 0) # tag : count
33
b82432594778 skaffold config and other updates
drewp@bigasterisk.com
parents: 32
diff changeset
77 for doc in db['links'].find({'user': user}, projection=['extracted.tags']):
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
78 docTags = set(doc.get('extracted', {}).get('tags', []))
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
79 if withTags and not withTags.issubset(docTags):
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
80 continue
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
81 for t in docTags.difference(withTags):
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
82 count[t] = count[t] + 1
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
83 byFreq = [(n, t) for t, n in count.items()]
28
7c82ffbca5d0 py3 and k8s upgrade
drewp@bigasterisk.com
parents: 25
diff changeset
84 byFreq.sort(key=lambda n_t: (-n_t[0], n_t[1]))
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
85 return [{'label': t, 'count': n} for n, t in byFreq]
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
86
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
87
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
88 def renderWithTime(name, data):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
89 t1 = time.time()
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
90 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
91 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
92 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
93 return rendered
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
94
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
95
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
96 @bottle.route('/addLink')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
97 def addLink():
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
98 out = {
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
99 'toRoot': siteRoot(),
25
e02fc021ab89 rm old siteRoot variable
drewp@bigasterisk.com
parents: 24
diff changeset
100 'absRoot': siteRoot(),
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
101 'user': getUser()[0],
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
102 'withKnockout': True,
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
103 'fillHrefJson': json.dumps(bottle.request.params.get('url', '')),
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
104 }
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
105 return renderWithTime('add.jade', out)
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
106
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
107
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
108 @bottle.route('/addOverlay')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
109 def addOverlay():
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
110 p = bottle.request.params
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
111
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
112 return ""
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
113
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
114
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
115 @bottle.route('/addLink/proposedUri')
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
116 def proposedUri():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
117 uri = bottle.request.params.uri
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
118 user, _ = getUser()
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
119
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
120 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
121 prevDoc = links.find(uri, user)
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
122 except NotFound:
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
123 prevDoc = None
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
124
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
125 return {
34
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
126 'description': prevDoc['description'] if prevDoc else pageTitle.pageTitle(uri),
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
127 'tag': prevDoc['tag'] if prevDoc else '',
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
128 'extended': prevDoc['extended'] if prevDoc else '',
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
129 'shareWith': prevDoc.get('shareWith', []) if prevDoc else [],
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
130 'suggestedTags': ['tag1', 'tag2'],
34
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
131 'existed': prevDoc is not None,
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
132 }
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
133
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
134
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
135 # 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
136
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
137
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
138 @bottle.route('/tags')
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
139 def tagFilterComplete():
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
140 params = bottle.request.params
28
7c82ffbca5d0 py3 and k8s upgrade
drewp@bigasterisk.com
parents: 25
diff changeset
141 haveTags = [_f for _f in params['have'].split(',') if _f]
22
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
142 if haveTags and len(haveTags[-1]) > 0:
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
143 haveTags, partialTerm = haveTags[:-1], haveTags[-1]
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
144 else:
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
145 partialTerm = ""
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
146
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
147 out = []
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
148 for t in allTags(params.user, withTags=haveTags):
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
149 if partialTerm and partialTerm not in t['label']:
fa55f4439977 fix autocomplete. less console.log
Drew Perttula <drewp@bigasterisk.com>
parents: 21
diff changeset
150 continue
34
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
151 out.append({'id': t['label'], 'text': "%s (%s%s)" % (t['label'], t['count'], " left" if haveTags else "")})
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
152
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
153 return {'tags': out}
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
154
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
155
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
156 @bottle.route('/<user>/')
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
157 def userSlash(user):
28
7c82ffbca5d0 py3 and k8s upgrade
drewp@bigasterisk.com
parents: 25
diff changeset
158 bottle.redirect(siteRoot() + "/%s" % urllib.parse.quote(user))
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
159
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
160
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
161 @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
162 def userAllJson(user):
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
163 data = recentLinks(user, [], allowEdit=getUser()[0] == user)
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
164 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
165 return json.dumps(data)
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
166
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
167
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
168 @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
169 def userAll(user):
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
170 return userLinks(user, "")
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
171
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
172
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
173 @bottle.route('/<user>', method='POST')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
174 def userAddLink(user):
39
c538dc39b851 user login fixes
drewp@bigasterisk.com
parents: 38
diff changeset
175 u=getUser()[0]
c538dc39b851 user login fixes
drewp@bigasterisk.com
parents: 38
diff changeset
176 if u is None:
c538dc39b851 user login fixes
drewp@bigasterisk.com
parents: 38
diff changeset
177 raise ValueError('not logged in')
c538dc39b851 user login fixes
drewp@bigasterisk.com
parents: 38
diff changeset
178 if u != user:
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
179 raise ValueError("not logged in as %s" % user)
28
7c82ffbca5d0 py3 and k8s upgrade
drewp@bigasterisk.com
parents: 25
diff changeset
180 print(repr(bottle.request.params.__dict__))
34
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
181 doc = links.fromPostdata(bottle.request.params, user, datetime.datetime.now(tzlocal()))
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
182 links.insertOrUpdate(doc)
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
183
28
7c82ffbca5d0 py3 and k8s upgrade
drewp@bigasterisk.com
parents: 25
diff changeset
184 print("notify about sharing to", repr(doc['shareWith']))
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
185
25
e02fc021ab89 rm old siteRoot variable
drewp@bigasterisk.com
parents: 24
diff changeset
186 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
187
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
188
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
189 def parseTags(tagComponent):
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
190 # the %20 is coming from davis.js, not me :(
28
7c82ffbca5d0 py3 and k8s upgrade
drewp@bigasterisk.com
parents: 25
diff changeset
191 return [_f for _f in tagComponent.replace("%20", "+").split('+') if _f]
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
192
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
193
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
194 @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
195 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
196 tags = parseTags(tags)
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
197 data = recentLinks(user, tags, allowEdit=getUser()[0] == user)
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
198 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
199 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
200
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
201
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
202 @bottle.route('/<user>/<tags>')
38
f3a15a724483 mongo api and up-checking
drewp@bigasterisk.com
parents: 35
diff changeset
203 @die_on_mongo_connection_errors()
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
204 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
205 tags = parseTags(tags)
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
206 log.info('userLinks user=%r tags=%r', user, tags)
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
207 data = recentLinks(user, tags, allowEdit=getUser()[0] == user)
34
c7b59377ab35 reformat
drewp@bigasterisk.com
parents: 33
diff changeset
208 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
209 data['toRoot'] = siteRoot()
7
93d94f327e82 autocomplete in link page box
Drew Perttula <drewp@bigasterisk.com>
parents: 5
diff changeset
210 data['allTags'] = allTags(user)
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 7
diff changeset
211 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
212 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
213
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
214 data['pageTags'] = [{"word": t} for t in tags]
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
215 data['stats']['template'] = 'TEMPLATETIME'
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
216 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
217
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
218
21
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
219 @bottle.route('/templates')
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
220 def templates():
8008ec2fd763 fix up link page reloading. tried davisjs; may not need it
Drew Perttula <drewp@bigasterisk.com>
parents: 20
diff changeset
221 return json.dumps({'linklist': renderer.load_template("linklist.jade")})
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
222
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
223
35
76f379c99317 /metrics
drewp@bigasterisk.com
parents: 34
diff changeset
224 @bottle.route('/metrics')
76f379c99317 /metrics
drewp@bigasterisk.com
parents: 34
diff changeset
225 def metrics():
76f379c99317 /metrics
drewp@bigasterisk.com
parents: 34
diff changeset
226 return ''
76f379c99317 /metrics
drewp@bigasterisk.com
parents: 34
diff changeset
227
76f379c99317 /metrics
drewp@bigasterisk.com
parents: 34
diff changeset
228
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
229 @bottle.route('/')
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
230 def root():
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
231 data = {
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
232 'toRoot': siteRoot(),
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
233 'stats': {
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
234 'template': 'TEMPLATETIME'
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
235 },
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
236 'users': [{
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
237 'user': doc['username']
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
238 } for doc in db['user'].find()],
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
239 }
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
240 return renderWithTime('index.jade', data)
30
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
241
e86642cf7393 style and requirements.txt cleanup
drewp@bigasterisk.com
parents: 28
diff changeset
242
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
243 if __name__ == '__main__':
24
ab9a6132529a redo siteRoot handling. fix some pathing bugs.
drewp@bigasterisk.com
parents: 22
diff changeset
244 logging.basicConfig(level=logging.INFO)
33
b82432594778 skaffold config and other updates
drewp@bigasterisk.com
parents: 32
diff changeset
245 bottle.run(server='gunicorn', host='0.0.0.0', port=10002, workers=1)