From c2569cc3219679dc83856e570bea7986cf24e77c Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 8 Jun 2020 22:15:43 -0700 Subject: [PATCH] Add frontend infrastructure for item callbacks --- inquisitor/app.py | 7 +++++++ inquisitor/importer.py | 3 ++- inquisitor/templates/feed.html | 20 ++++++++++++++++++-- sources/callbackdemo.py | 19 +++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 sources/callbackdemo.py diff --git a/inquisitor/app.py b/inquisitor/app.py index 39ff1af..57ab55a 100644 --- a/inquisitor/app.py +++ b/inquisitor/app.py @@ -140,3 +140,10 @@ def mass_deactivate(): logger.debug(f"Deactivating {info['source']}/{info['itemid']}") item['active'] = False return jsonify({}) + +@app.route("/callback/", methods=['POST']) +def callback(): + params = request.get_json() + if 'source' not in params and 'itemid' not in params: + logger.error("Bad request params: {}".format(params)) + return jsonify({}) diff --git a/inquisitor/importer.py b/inquisitor/importer.py index 2f21fd4..04adfc3 100644 --- a/inquisitor/importer.py +++ b/inquisitor/importer.py @@ -155,7 +155,7 @@ def populate_new(source_name, item): if 'title' not in item: item['title'] = item['id'] # tags is auto-populated if missing (not if empty!) if 'tags' not in item: item['tags'] = [source_name] - # link, time, author, body, ttl, ttd, and tts are optional + # link, time, author, body, ttl, ttd, tts, callback are optional def populate_old(prior, new): # Not updated: id, source, active, created @@ -168,3 +168,4 @@ def populate_old(prior, new): if 'ttl' in new: prior['ttl'] = new['ttl'] if 'ttd' in new: prior['ttd'] = new['ttd'] if 'tts' in new: prior['tts'] = new['tts'] + if 'callback' in new: prior['callback'] = new['callback'] diff --git a/inquisitor/templates/feed.html b/inquisitor/templates/feed.html index ac8fdc1..c9bbb3f 100644 --- a/inquisitor/templates/feed.html +++ b/inquisitor/templates/feed.html @@ -73,11 +73,22 @@ }, body: JSON.stringify({items: items}), }) - .then(response => response.json()) .then(function () { location.reload() }); }; + var callback = function (source, itemid) { + fetch('callback/', { + method: 'POST', + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + }, + body: JSON.stringify({source: source, itemid: itemid}), + }) + .then(function (data) { + location.reload() + }); + }; @@ -88,9 +99,14 @@ {% if item.id %}{% endif %} {% if item.id %}{% endif %} {% if item.link %}{% endif %} - {% if item.body %}
+ {% if item.body or item.callback %}
{{item.title}} + {% if item.body %}

{{item.body|safe}}

+ {% endif %} + {% if item.callback %} +

+ {% endif %}
{% else %}{{item.title}}
{% endif %} diff --git a/sources/callbackdemo.py b/sources/callbackdemo.py new file mode 100644 index 0000000..93e1b2f --- /dev/null +++ b/sources/callbackdemo.py @@ -0,0 +1,19 @@ +""" +Demonstrates the behavior of the callback field. +""" +# Standard library imports +import random + +def fetch_new(state): + itemid = '{:x}'.format(random.getrandbits(16 * 4)) + item = { + 'source': "callbackdemo", + 'id': itemid, + 'title': f"Callback demo", + 'body': 'No callbacks', + 'callback': { 'id': itemid } + } + return [item] + +def callback(state, item): + print(item)