Implement [[agora go links integration]].

This commit is contained in:
Flancian 2020-11-22 18:54:08 +01:00
parent d55f7dcdd9
commit 7aeed0c52d
2 changed files with 53 additions and 1 deletions

View file

@ -56,6 +56,35 @@ def today():
today = datetime.datetime.now().date() today = datetime.datetime.now().date()
return redirect("https://anagora.org/node/%s" % today.strftime("%Y-%m-%d")) return redirect("https://anagora.org/node/%s" % today.strftime("%Y-%m-%d"))
@bp.route('/go/<node>')
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/<user>') @bp.route('/u/<user>')
@bp.route('/user/<user>') @bp.route('/user/<user>')
@bp.route('/@<user>') @bp.route('/@<user>')
@ -76,7 +105,7 @@ def wikilink(node):
def subnode(subnode): def subnode(subnode):
return render_template('subnode_rendered.html', subnode=db.subnode_by_uri(subnode), backlinks=db.subnodes_by_outlink(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/<query>') # @bp.route('/search/<query>')
# def search(query): # def search(query):
# return render_template('subnodes.html', subnodes=db.search_subnodes(query)) # return render_template('subnodes.html', subnodes=db.search_subnodes(query))

View file

@ -63,6 +63,14 @@ class Node:
def size(self): def size(self):
return len(self.subnodes) 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: class Subnode:
"""A subnode is a note or media resource volunteered by a user of the Agora. """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 It maps to a particular file in the Agora repository, stored (relative to
@ -98,6 +106,21 @@ class Subnode:
# hack hack # hack hack
return 100-fuzz.ratio(self.wikilink, other.wikilink) 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: class User:
def __init__(self, user): def __init__(self, user):