Add feed control item to edit filters inline

This commit is contained in:
Tim Van Baak 2019-06-19 18:45:20 -07:00
parent 0906fe9b06
commit d62c3a9001
3 changed files with 67 additions and 17 deletions

View File

@ -1,6 +1,7 @@
# Standard library imports # Standard library imports
import datetime import datetime
import logging import logging
import traceback
# Third party imports # Third party imports
from flask import Flask, render_template, request, jsonify from flask import Flask, render_template, request, jsonify
@ -21,12 +22,12 @@ app = Flask(__name__)
dungeon = dungeonlib.Dungeon("dungeon") dungeon = dungeonlib.Dungeon("dungeon")
def list_filter(whitelist=None, blacklist=None): def make_query_link(text, wl, bl):
if whitelist is not None: wlp = "only=" + ",".join(wl)
return lambda item: any([tag in whitelist for tag in item['tags']]) blp = "not=" + ",".join(bl)
if blacklist is not None: params = [p for p in (wlp, blp) if not p.endswith("=")]
return lambda item: not any([tag in blacklist for tag in item['tags']]) query = "?{}".format("&".join(params))
return lambda s: True return '<a href="{1}">{0}</a>'.format(text, query)
@app.template_filter("datetimeformat") @app.template_filter("datetimeformat")
def datetimeformat(value, formatstr="%Y-%m-%d %H:%M:%S"): def datetimeformat(value, formatstr="%Y-%m-%d %H:%M:%S"):
@ -38,21 +39,61 @@ def datetimeformat(value, formatstr="%Y-%m-%d %H:%M:%S"):
@app.route("/") @app.route("/")
def root(): def root():
dungeon = dungeonlib.Dungeon("dungeon") dungeon = dungeonlib.Dungeon("dungeon")
filter_lambda = list_filter(
whitelist=request.args.get('only'), # Determine exclusion filters
blacklist=request.args.get('not')) filters = []
wl_param = request.args.get('only')
wl = wl_param.split(",") if wl_param else []
bl_param = request.args.get('not')
bl = bl_param.split(",") if bl_param else []
if wl:
filters.append(lambda item: not any([tag in wl for tag in item['tags']]))
if bl:
filters.append(lambda item: any([tag in bl for tag in item['tags']]))
# Get all active+filtered items and all active tags
total = 0
active_items = [] active_items = []
for cell_name in dungeon: active_tags = set()
cell = dungeon[cell_name] item_read_exceptions = []
for item_id in cell: for cell in dungeon:
for item_id in dungeon[cell]:
try: try:
item = cell[item_id] item = dungeon[cell][item_id]
except: except:
logger.error("Exception reading {}/{}".format(cell_name, item_id)) msg = "Exception reading {}/{}".format(cell, item_id)
logger.error(msg)
item_read_exceptions.append(msg)
item_read_exceptions.append(traceback.format_exc())
continue continue
if item['active'] and filter_lambda(item): if item['active']:
active_tags |= set(item['tags'])
total += 1
if not any(map(lambda f: f(item), filters)):
active_items.append(item) active_items.append(item)
logger.info("Found {} active items".format(len(active_items))) logger.info("Returning {} of {} items".format(len(active_items), total))
if item_read_exceptions:
read_ex = {
'title': 'Read errors',
'body': "<pre>{}</pre>".format("\n\n".join(item_read_exceptions)),
'created': None,
}
active_items = [read_ex] + active_items
# Create the feed control item
wl_minus = [make_query_link("only - {}".format(tag), [t for t in wl if t != tag], bl) for tag in wl]
wl_plus = [make_query_link("only + {}".format(tag), wl + [tag], bl) for tag in active_tags if tag not in wl]
bl_minus = [make_query_link("not - {}".format(tag), wl, [t for t in bl if t != tag]) for tag in bl]
bl_plus = [make_query_link("not + {}".format(tag), wl, bl + [tag]) for tag in active_tags if tag not in bl]
body = "<pre>{}</pre>".format("\n".join(wl_minus + wl_plus + bl_minus + bl_plus))
feed_control = {
'title': 'Feed Control',
'body': body,
'created': None,
}
active_items = [feed_control] + active_items
return render_template("feed.html", items=active_items[:100]) return render_template("feed.html", items=active_items[:100])

View File

@ -37,3 +37,6 @@ summary:focus {
text-decoration: line-through; text-decoration: line-through;
color: rgba(0, 0, 0, 0.2); color: rgba(0, 0, 0, 0.2);
} }
pre {
white-space: pre-wrap;
}

View File

@ -26,7 +26,9 @@
{% if items %} {% if items %}
{% for item in items %} {% for item in items %}
<div class="readable-item" id="{{item.source}}-{{item.id}}"> <div class="readable-item" id="{{item.source}}-{{item.id}}">
{% if item.id %}
<button onclick="javascript:deactivate('{{item.source}}', '{{item.id}}')">&#10005;</button> <button onclick="javascript:deactivate('{{item.source}}', '{{item.id}}')">&#10005;</button>
{% endif %}
{% if item.link %} {% if item.link %}
<a class="item-link" href="{{item.link}}" target="_blank">&#8663;</a> <a class="item-link" href="{{item.link}}" target="_blank">&#8663;</a>
{% endif %} {% endif %}
@ -49,7 +51,11 @@
</span><br> </span><br>
{% endif %} {% endif %}
<span class="item-info" title="{{ 'Tags: {}'.format(', '.join(item.tags)) }}"> <span class="item-info" title="{{ 'Tags: {}'.format(', '.join(item.tags)) }}">
{{item.source}} {{item.id}} {{item.created|datetimeformat}} {{item.source}}
{% if item.id %}
{{item.id}}
{% endif %}
{{item.created|datetimeformat}}
</span> </span>
</div> </div>
{% endfor %} {% endfor %}