diff --git a/serve.py b/serve.py index 31d91d4..f69b12e 100644 --- a/serve.py +++ b/serve.py @@ -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 def get_tags(): + if g.user is None: + return {} if not hasattr(g, '_tags'): - user = 'root' # root for now, the only default user 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 return g._tags @@ -48,6 +49,11 @@ def get_metas(): g._mdb = get_metas_db() 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 def close_connection(error=None): # close any opened database connections @@ -154,6 +160,12 @@ def search_rank(q: str = ''): # ----------------------------------------------------------------------------- # 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']) def main(): @@ -208,11 +220,16 @@ def main(): for i, p in enumerate(papers): 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() - 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['tags'] = [{'name':t, 'n':len(pids)} for t, pids in tags.items()] + [{'name': 'all'}] + context['tags'] = rtags context['gvars'] = {} context['gvars']['rank'] = opt_rank context['gvars']['tags'] = opt_tags @@ -249,26 +266,34 @@ def inspect(): # package everything up and render paper = render_pid(pid) - context = dict( - paper = paper, - words = words, - ) + context = default_context() + context['paper'] = paper + context['words'] = words 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 @app.route('/add//') 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: # create the user if we don't know about them yet with an empty library - if not user in tags_db: - tags_db[user] = {} + if not g.user in tags_db: + tags_db[g.user] = {} # fetch the user library object - d = tags_db[user] + d = tags_db[g.user] # add the paper to the tag if tag not in d: @@ -276,22 +301,24 @@ def add(pid=None, tag=None): d[tag].add(pid) # 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 @app.route('/sub//') 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: # 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 ¯\_(ツ)_/¯" # fetch the user library object - d = tags_db[user] + d = tags_db[g.user] # add the paper to the tag if tag not in d: @@ -300,20 +327,22 @@ def sub(pid=None, tag=None): d[tag].remove(pid) # 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 @app.route('/del/') 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: - if user not in tags_db: + if g.user not in tags_db: return "user does not have a library" - d = tags_db[user] + d = tags_db[g.user] if tag not in d: return "user does not have this tag" @@ -322,7 +351,7 @@ def delete_tag(tag=None): del d[tag] # 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 diff --git a/static/paper_list.js b/static/paper_list.js index a06121e..69c91cd 100644 --- a/static/paper_list.js +++ b/static/paper_list.js @@ -80,5 +80,11 @@ const TagList = props => { ) } -ReactDOM.render(, document.getElementById('wrap')) -ReactDOM.render(, document.getElementById('tagwrap')) +// render papers into #wrap +ReactDOM.render(, document.getElementById('wrap')); + +// render tags into #tagwrap, if it exists +let tagwrap_elt = document.getElementById('tagwrap'); +if (tagwrap_elt) { + ReactDOM.render(, tagwrap_elt); +} diff --git a/static/style.css b/static/style.css index 82470be..e5e54cb 100644 --- a/static/style.css +++ b/static/style.css @@ -175,3 +175,9 @@ body { display: inline-block; margin-left: 5px; } +#login-link { + /* position on the right of the header */ + position: absolute; + right: 10px; + top: 10px; +} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index fd8d19c..c3f9a40 100644 --- a/templates/base.html +++ b/templates/base.html @@ -18,6 +18,9 @@ {% block content %} diff --git a/templates/index.html b/templates/index.html index 1ea1ffc..1ec1951 100644 --- a/templates/index.html +++ b/templates/index.html @@ -65,8 +65,10 @@ var gvars = {{ gvars | tojson }}; +{% if user %}
+{% endif %}
diff --git a/templates/profile.html b/templates/profile.html new file mode 100644 index 0000000..6b073c7 --- /dev/null +++ b/templates/profile.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block variables %} +{% endblock %} + +{% block content %} +
+ This is where the user gets to log in, or see information about their account if logged in. +
+{% endblock %} + +{% block elements %} +{% endblock %}