From 1e8fbdcf775e4843295760a7192b54e0cff95dbc Mon Sep 17 00:00:00 2001 From: Flancian <0@flancia.org> Date: Mon, 16 Nov 2020 15:51:34 +0100 Subject: [PATCH] Add very basic GET-based /search. --- app/agora.py | 7 +++++++ app/db.py | 4 ++++ app/templates/subnodes.html | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/app/agora.py b/app/agora.py index f734120..ccf5678 100644 --- a/app/agora.py +++ b/app/agora.py @@ -71,14 +71,20 @@ def wikilink(node): def subnode(subnode): return render_template('subnode_rendered.html', subnode=db.subnode_by_uri(subnode), backlinks=db.subnodes_by_outlink(subnode)) +@bp.route('/search/') +def search(query): + return render_template('subnodes.html', subnodes=db.search_subnodes(query)) + @bp.route('/asset//') def asset(user, asset): # An asset is a binary in someone's garden//assets directory. + # Currently unused. path = '/'.join(["garden", user, 'assets', asset]) return current_app.send_static_file(path) @bp.route('/raw/') def raw(node): + # Currently unused. # hack hack # outlinks return Response("\n\n".join([str(n.outlinks) for n in db.nodes_by_wikilink(node)]), mimetype="text/plain") @@ -87,4 +93,5 @@ def raw(node): @bp.route('/backlinks/') def backlinks(node): + # Currently unused. return render_template('nodes.html', nodes=db.nodes_by_outlink(node)) diff --git a/app/db.py b/app/db.py index 9026342..4e532a5 100644 --- a/app/db.py +++ b/app/db.py @@ -173,6 +173,10 @@ def subnodes_by_wikilink(wikilink, fuzzy_matching=True): subnodes = [subnode for subnode in all_subnodes() if subnode.wikilink == wikilink] return subnodes +def search_subnodes(query): + subnodes = [subnode for subnode in all_subnodes() if re.search(query, subnode.content)] + return subnodes + def subnodes_by_user(user): subnodes = [subnode for subnode in all_subnodes() if subnode.user == user] return subnodes diff --git a/app/templates/subnodes.html b/app/templates/subnodes.html index f639ef5..20a9758 100644 --- a/app/templates/subnodes.html +++ b/app/templates/subnodes.html @@ -17,6 +17,12 @@ {% extends "base.html" %}

Subnodes

{% block content %} +{% if not subnodes %} +No subnodes found for the given term or path. +

+Go back to /nodes? +{% endif %} + {% for subnode in subnodes %} (user: {{subnode.user}}) {{ subnode.uri }}: {{subnode.wikilink}}
{% endfor %}