Add frontend infrastructure for item callbacks

This commit is contained in:
Tim Van Baak 2020-06-08 22:15:43 -07:00
parent 38c81c4c3f
commit c2569cc321
4 changed files with 46 additions and 3 deletions

View File

@ -140,3 +140,10 @@ def mass_deactivate():
logger.debug(f"Deactivating {info['source']}/{info['itemid']}") logger.debug(f"Deactivating {info['source']}/{info['itemid']}")
item['active'] = False item['active'] = False
return jsonify({}) 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({})

View File

@ -155,7 +155,7 @@ def populate_new(source_name, item):
if 'title' not in item: item['title'] = item['id'] if 'title' not in item: item['title'] = item['id']
# tags is auto-populated if missing (not if empty!) # tags is auto-populated if missing (not if empty!)
if 'tags' not in item: item['tags'] = [source_name] 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): def populate_old(prior, new):
# Not updated: id, source, active, created # Not updated: id, source, active, created
@ -168,3 +168,4 @@ def populate_old(prior, new):
if 'ttl' in new: prior['ttl'] = new['ttl'] if 'ttl' in new: prior['ttl'] = new['ttl']
if 'ttd' in new: prior['ttd'] = new['ttd'] if 'ttd' in new: prior['ttd'] = new['ttd']
if 'tts' in new: prior['tts'] = new['tts'] if 'tts' in new: prior['tts'] = new['tts']
if 'callback' in new: prior['callback'] = new['callback']

View File

@ -73,11 +73,22 @@
}, },
body: JSON.stringify({items: items}), body: JSON.stringify({items: items}),
}) })
.then(response => response.json())
.then(function () { .then(function () {
location.reload() 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()
});
};
</script> </script>
</head> </head>
<body> <body>
@ -88,9 +99,14 @@
{% if item.id %}<button class="item-button" onclick="javascript:deactivate('{{item.source}}', '{{item.id}}')" title="Deactivate">&#10005;</button>{% endif %} {% if item.id %}<button class="item-button" onclick="javascript:deactivate('{{item.source}}', '{{item.id}}')" title="Deactivate">&#10005;</button>{% endif %}
{% if item.id %}<button class="item-button" onclick="javascript:punt('{{item.source}}', '{{item.id}}')" title="Punt to tomorrow">&#8631;</button>{% endif %} {% if item.id %}<button class="item-button" onclick="javascript:punt('{{item.source}}', '{{item.id}}')" title="Punt to tomorrow">&#8631;</button>{% endif %}
{% if item.link %}<a class="item-link" href="{{item.link}}" target="_blank">&#8663;</a>{% endif %} {% if item.link %}<a class="item-link" href="{{item.link}}" target="_blank">&#8663;</a>{% endif %}
{% if item.body %}<details> {% if item.body or item.callback %}<details>
<summary><span class="item-title">{{item.title}}</span></summary> <summary><span class="item-title">{{item.title}}</span></summary>
{% if item.body %}
<p>{{item.body|safe}}</p> <p>{{item.body|safe}}</p>
{% endif %}
{% if item.callback %}
<p><button onclick="javascript:callback('{{item.source}}', '{{item.id}}')">Callback</button></p>
{% endif %}
</details> </details>
{% else %}<span class="item-title">{{item.title}}</span><br> {% else %}<span class="item-title">{{item.title}}</span><br>
{% endif %} {% endif %}

19
sources/callbackdemo.py Normal file
View File

@ -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)