ok here we go, the beginning of user profiles. for now we assume there is a single hard-coded user root
This commit is contained in:
@@ -31,10 +31,11 @@ RET_NUM = 100 # number of papers to return per page
|
|||||||
# globals that manage the (lazy) loading of various state for a request
|
# globals that manage the (lazy) loading of various state for a request
|
||||||
|
|
||||||
def get_tags():
|
def get_tags():
|
||||||
|
if g.user is None:
|
||||||
|
return {}
|
||||||
if not hasattr(g, '_tags'):
|
if not hasattr(g, '_tags'):
|
||||||
user = 'root' # root for now, the only default user
|
|
||||||
with get_tags_db() as tags_db:
|
with get_tags_db() as tags_db:
|
||||||
tags_dict = tags_db[user] if user in tags_db else {}
|
tags_dict = tags_db[g.user] if g.user in tags_db else {}
|
||||||
g._tags = tags_dict
|
g._tags = tags_dict
|
||||||
return g._tags
|
return g._tags
|
||||||
|
|
||||||
@@ -48,6 +49,11 @@ def get_metas():
|
|||||||
g._mdb = get_metas_db()
|
g._mdb = get_metas_db()
|
||||||
return g._mdb
|
return g._mdb
|
||||||
|
|
||||||
|
@app.before_request
|
||||||
|
def before_request():
|
||||||
|
g.user = 'root' # current default user, as we have no accounts db at this time just yet
|
||||||
|
#g.user = None # if noone is logged in, will be the default state shortly
|
||||||
|
|
||||||
@app.teardown_request
|
@app.teardown_request
|
||||||
def close_connection(error=None):
|
def close_connection(error=None):
|
||||||
# close any opened database connections
|
# close any opened database connections
|
||||||
@@ -154,6 +160,12 @@ def search_rank(q: str = ''):
|
|||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# primary application endpoints
|
# primary application endpoints
|
||||||
|
|
||||||
|
def default_context():
|
||||||
|
# any global context across all pages, e.g. related to the current user
|
||||||
|
context = {}
|
||||||
|
context['user'] = g.user if g.user is not None else ''
|
||||||
|
return context
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
@@ -208,11 +220,16 @@ def main():
|
|||||||
for i, p in enumerate(papers):
|
for i, p in enumerate(papers):
|
||||||
p['weight'] = float(scores[i])
|
p['weight'] = float(scores[i])
|
||||||
|
|
||||||
# build the page context information and render
|
# build the current tags for the user, and append the special 'all' tag
|
||||||
tags = get_tags()
|
tags = get_tags()
|
||||||
context = {}
|
rtags = [{'name':t, 'n':len(pids)} for t, pids in tags.items()]
|
||||||
|
if rtags:
|
||||||
|
rtags.append({'name': 'all'})
|
||||||
|
|
||||||
|
# build the page context information and render
|
||||||
|
context = default_context()
|
||||||
context['papers'] = papers
|
context['papers'] = papers
|
||||||
context['tags'] = [{'name':t, 'n':len(pids)} for t, pids in tags.items()] + [{'name': 'all'}]
|
context['tags'] = rtags
|
||||||
context['gvars'] = {}
|
context['gvars'] = {}
|
||||||
context['gvars']['rank'] = opt_rank
|
context['gvars']['rank'] = opt_rank
|
||||||
context['gvars']['tags'] = opt_tags
|
context['gvars']['tags'] = opt_tags
|
||||||
@@ -249,26 +266,34 @@ def inspect():
|
|||||||
|
|
||||||
# package everything up and render
|
# package everything up and render
|
||||||
paper = render_pid(pid)
|
paper = render_pid(pid)
|
||||||
context = dict(
|
context = default_context()
|
||||||
paper = paper,
|
context['paper'] = paper
|
||||||
words = words,
|
context['words'] = words
|
||||||
)
|
|
||||||
return render_template('inspect.html', **context)
|
return render_template('inspect.html', **context)
|
||||||
|
|
||||||
|
@app.route('/profile')
|
||||||
|
def profile():
|
||||||
|
context = default_context()
|
||||||
|
return render_template('profile.html', **context)
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# tag related endpoints: add, delete tags for any paper
|
# tag related endpoints: add, delete tags for any paper
|
||||||
|
|
||||||
@app.route('/add/<pid>/<tag>')
|
@app.route('/add/<pid>/<tag>')
|
||||||
def add(pid=None, tag=None):
|
def add(pid=None, tag=None):
|
||||||
user = 'root'
|
if g.user is None:
|
||||||
|
return "error, not logged in"
|
||||||
|
if tag == 'all':
|
||||||
|
return "error, cannot add the protected tag 'all'"
|
||||||
|
|
||||||
with get_tags_db(flag='c') as tags_db:
|
with get_tags_db(flag='c') as tags_db:
|
||||||
|
|
||||||
# create the user if we don't know about them yet with an empty library
|
# create the user if we don't know about them yet with an empty library
|
||||||
if not user in tags_db:
|
if not g.user in tags_db:
|
||||||
tags_db[user] = {}
|
tags_db[g.user] = {}
|
||||||
|
|
||||||
# fetch the user library object
|
# fetch the user library object
|
||||||
d = tags_db[user]
|
d = tags_db[g.user]
|
||||||
|
|
||||||
# add the paper to the tag
|
# add the paper to the tag
|
||||||
if tag not in d:
|
if tag not in d:
|
||||||
@@ -276,22 +301,24 @@ def add(pid=None, tag=None):
|
|||||||
d[tag].add(pid)
|
d[tag].add(pid)
|
||||||
|
|
||||||
# write back to database
|
# write back to database
|
||||||
tags_db[user] = d
|
tags_db[g.user] = d
|
||||||
|
|
||||||
print("added paper %s to tag %s for user %s" % (pid, tag, user))
|
print("added paper %s to tag %s for user %s" % (pid, tag, g.user))
|
||||||
return "ok: " + str(d) # return back the user library for debugging atm
|
return "ok: " + str(d) # return back the user library for debugging atm
|
||||||
|
|
||||||
@app.route('/sub/<pid>/<tag>')
|
@app.route('/sub/<pid>/<tag>')
|
||||||
def sub(pid=None, tag=None):
|
def sub(pid=None, tag=None):
|
||||||
user = 'root'
|
if g.user is None:
|
||||||
|
return "error, not logged in"
|
||||||
|
|
||||||
with get_tags_db(flag='c') as tags_db:
|
with get_tags_db(flag='c') as tags_db:
|
||||||
|
|
||||||
# if the user doesn't have any tags, there is nothing to do
|
# if the user doesn't have any tags, there is nothing to do
|
||||||
if not user in tags_db:
|
if not g.user in tags_db:
|
||||||
return "user has no library of tags ¯\_(ツ)_/¯"
|
return "user has no library of tags ¯\_(ツ)_/¯"
|
||||||
|
|
||||||
# fetch the user library object
|
# fetch the user library object
|
||||||
d = tags_db[user]
|
d = tags_db[g.user]
|
||||||
|
|
||||||
# add the paper to the tag
|
# add the paper to the tag
|
||||||
if tag not in d:
|
if tag not in d:
|
||||||
@@ -300,20 +327,22 @@ def sub(pid=None, tag=None):
|
|||||||
d[tag].remove(pid)
|
d[tag].remove(pid)
|
||||||
|
|
||||||
# write back to database
|
# write back to database
|
||||||
tags_db[user] = d
|
tags_db[g.user] = d
|
||||||
|
|
||||||
print("removed from paper %s the tag %s for user %s" % (pid, tag, user))
|
print("removed from paper %s the tag %s for user %s" % (pid, tag, g.user))
|
||||||
return "ok: " + str(d) # return back the user library for debugging atm
|
return "ok: " + str(d) # return back the user library for debugging atm
|
||||||
|
|
||||||
@app.route('/del/<tag>')
|
@app.route('/del/<tag>')
|
||||||
def delete_tag(tag=None):
|
def delete_tag(tag=None):
|
||||||
user = 'root'
|
if g.user is None:
|
||||||
|
return "error, not logged in"
|
||||||
|
|
||||||
with get_tags_db(flag='c') as tags_db:
|
with get_tags_db(flag='c') as tags_db:
|
||||||
|
|
||||||
if user not in tags_db:
|
if g.user not in tags_db:
|
||||||
return "user does not have a library"
|
return "user does not have a library"
|
||||||
|
|
||||||
d = tags_db[user]
|
d = tags_db[g.user]
|
||||||
|
|
||||||
if tag not in d:
|
if tag not in d:
|
||||||
return "user does not have this tag"
|
return "user does not have this tag"
|
||||||
@@ -322,7 +351,7 @@ def delete_tag(tag=None):
|
|||||||
del d[tag]
|
del d[tag]
|
||||||
|
|
||||||
# write back to database
|
# write back to database
|
||||||
tags_db[user] = d
|
tags_db[g.user] = d
|
||||||
|
|
||||||
print("deleted tag %s for user %s" % (tag, user))
|
print("deleted tag %s for user %s" % (tag, g.user))
|
||||||
return "ok: " + str(d) # return back the user library for debugging atm
|
return "ok: " + str(d) # return back the user library for debugging atm
|
||||||
|
|||||||
@@ -80,5 +80,11 @@ const TagList = props => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactDOM.render(<PaperList papers={papers} />, document.getElementById('wrap'))
|
// render papers into #wrap
|
||||||
ReactDOM.render(<TagList tags={tags} />, document.getElementById('tagwrap'))
|
ReactDOM.render(<PaperList papers={papers} />, document.getElementById('wrap'));
|
||||||
|
|
||||||
|
// render tags into #tagwrap, if it exists
|
||||||
|
let tagwrap_elt = document.getElementById('tagwrap');
|
||||||
|
if (tagwrap_elt) {
|
||||||
|
ReactDOM.render(<TagList tags={tags} />, tagwrap_elt);
|
||||||
|
}
|
||||||
|
|||||||
@@ -175,3 +175,9 @@ body {
|
|||||||
display: inline-block;
|
display: inline-block;
|
||||||
margin-left: 5px;
|
margin-left: 5px;
|
||||||
}
|
}
|
||||||
|
#login-link {
|
||||||
|
/* position on the right of the header */
|
||||||
|
position: absolute;
|
||||||
|
right: 10px;
|
||||||
|
top: 10px;
|
||||||
|
}
|
||||||
@@ -18,6 +18,9 @@
|
|||||||
<body>
|
<body>
|
||||||
<div id="header">
|
<div id="header">
|
||||||
<a href="/">arxiv-sanity</a>
|
<a href="/">arxiv-sanity</a>
|
||||||
|
<a href="/profile" id="login-link">
|
||||||
|
{{ user if user else 'login' }}
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|||||||
@@ -65,8 +65,10 @@ var gvars = {{ gvars | tojson }};
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% if user %}
|
||||||
<div id="tagwrap">
|
<div id="tagwrap">
|
||||||
</div>
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div id="wrap">
|
<div id="wrap">
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block variables %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div id="wrap">
|
||||||
|
This is where the user gets to log in, or see information about their account if logged in.
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block elements %}
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user