From 7aeed0c52d5155d3740b3963ac5b08b92d2ee2c2 Mon Sep 17 00:00:00 2001 From: Flancian <0@flancia.org> Date: Sun, 22 Nov 2020 18:54:08 +0100 Subject: [PATCH] Implement [[agora go links integration]]. --- app/agora.py | 31 ++++++++++++++++++++++++++++++- app/db.py | 23 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/app/agora.py b/app/agora.py index 3202b5c..e74a95a 100644 --- a/app/agora.py +++ b/app/agora.py @@ -56,6 +56,35 @@ def today(): today = datetime.datetime.now().date() return redirect("https://anagora.org/node/%s" % today.strftime("%Y-%m-%d")) +@bp.route('/go/') +def go(node): + """Redirects to the URL in the given node in a block that starts with [[go]], if there is one.""" + # TODO(flancian): all node-scoped stuff should move to actually use node objects. + try: + n = db.nodes_by_wikilink(node) + except KeyError: + return redirect("https://anagora.org/node/%s" % node) + + if len(n) > 1: + current_app.logger.warning('nodes_by_wikilink returned more than one node, should not happen.') + + if len(n) == 0: + # No nodes with this name -- redirect to node 404. + return redirect("https://anagora.org/node/%s" % node) + + links = n[0].go() + if len(links) == 0: + # No go links detected in this node -- just redirect to the node. + # TODO(flancian): flash an explanation :) + return redirect("https://anagora.org/node/%s" % node) + + if len(links) > 1: + # TODO(flancian): to be implemented. + # Likely default to one of the links, show all of them but redirect to default within n seconds. + current_app.logger.warning('Code to manage nodes with more than one go link is not Not implemented.') + + return redirect(links[0]) + @bp.route('/u/') @bp.route('/user/') @bp.route('/@') @@ -76,7 +105,7 @@ def wikilink(node): def subnode(subnode): return render_template('subnode_rendered.html', subnode=db.subnode_by_uri(subnode), backlinks=db.subnodes_by_outlink(subnode)) -# Searching with GET: potentially useful but probably not a good idea. + # Searching with GET: potentially useful but probably not a good idea. # @bp.route('/search/') # def search(query): # return render_template('subnodes.html', subnodes=db.search_subnodes(query)) diff --git a/app/db.py b/app/db.py index aea34c1..e4c8055 100644 --- a/app/db.py +++ b/app/db.py @@ -63,6 +63,14 @@ class Node: def size(self): return len(self.subnodes) + def go(self): + # There's surely a much better way to do this. Alas :) + go = [] + for subnode in self.subnodes: + go.extend(subnode.go()) + return go + + class Subnode: """A subnode is a note or media resource volunteered by a user of the Agora. It maps to a particular file in the Agora repository, stored (relative to @@ -98,6 +106,21 @@ class Subnode: # hack hack return 100-fuzz.ratio(self.wikilink, other.wikilink) + def go(self): + # returns a set of go links contained in this node + return subnode_to_actions(self, 'go') + + +def subnode_to_actions(subnode, action): + # hack hack. + action_regex ='\[\[' + action + '\]\] (.*?)$' + content = subnode.content + actions = [] + for line in content.splitlines(): + m = re.search(action_regex, line) + if m: + actions.append(m.group(1)) + return actions class User: def __init__(self, user):