Add tag support to server

This commit is contained in:
Tim Van Baak 2019-06-19 12:52:28 -07:00
parent 7c29071b23
commit ee7927ec9e
5 changed files with 58 additions and 24 deletions

View File

@ -49,6 +49,7 @@ def main():
args = parse_args(commands) args = parse_args(commands)
# Configure logging. # Configure logging.
if args.command != 'run':
loglevel = getattr(logging, args.log.upper()) loglevel = getattr(logging, args.log.upper())
if not isinstance(loglevel, int): if not isinstance(loglevel, int):
raise ValueError("Invalid log level: {}".format(args.log)) raise ValueError("Invalid log level: {}".format(args.log))

View File

@ -1,5 +1,5 @@
# Standard library imports # Standard library imports
from datetime import datetime import datetime
import logging import logging
# Third party imports # Third party imports
@ -18,27 +18,46 @@ console.setFormatter(formatter)
logger.addHandler(console) logger.addHandler(console)
app = Flask(__name__) app = Flask(__name__)
dungeon = dungeonlib.Dungeon("dungeon")
def list_filter(whitelist=None, blacklist=None):
if whitelist is not None:
return lambda item: any([tag in whitelist for tag in item['tags']])
if blacklist is not None:
return lambda item: not any([tag in blacklist for tag in item['tags']])
return lambda s: True
@app.template_filter("datetimeformat")
def datetimeformat(value, formatstr="%Y-%m-%d %H:%M:%S"):
if value is None:
return ""
dt = datetime.datetime.fromtimestamp(value)
return dt.strftime(formatstr)
@app.route("/") @app.route("/")
def root(): def root():
dungeon = dungeonlib.Dungeon("dungeon") dungeon = dungeonlib.Dungeon("dungeon")
filter_lambda = list_filter(
whitelist=request.args.get('wl'),
blacklist=request.args.get('bl'))
active_items = [] active_items = []
for cell in dungeon: for cell_name in dungeon:
for item_id in dungeon[cell]: cell = dungeon[cell_name]
for item_id in cell:
try: try:
item = dungeon[cell][item_id] item = cell[item_id]
if item['active']:
active_items.append(item)
except: except:
logger.error("Exception reading {}/{}".format(cell, item_id)) logger.error("Exception reading {}/{}".format(cell_name, item_id))
continue
if item['active'] and filter_lambda(item):
active_items.append(item)
logger.info("Found {} active items".format(len(active_items))) logger.info("Found {} active items".format(len(active_items)))
return render_template("feed.html", items=active_items[:100]) return render_template("feed.html", items=active_items[:100])
@app.route("/deactivate/", methods=['POST']) @app.route("/deactivate/", methods=['POST'])
def deactivate(): def deactivate():
dungeon = dungeonlib.Dungeon("dungeon")
params = request.get_json() params = request.get_json()
if 'source' not in params and 'itemid' not in params: if 'source' not in params and 'itemid' not in params:
logger.error("Bad request params: {}".format(params)) logger.error("Bad request params: {}".format(params))

View File

@ -35,7 +35,13 @@ class ReadableItem():
self.item = ast.literal_eval(f.read()) self.item = ast.literal_eval(f.read())
def __getitem__(self, key): def __getitem__(self, key):
if key in self.item:
return self.item[key] return self.item[key]
else:
if key == 'tags':
return []
else:
return None
def __setitem__(self, key, value): def __setitem__(self, key, value):
raise TypeError("ReadableItem is not writable") raise TypeError("ReadableItem is not writable")

View File

@ -7,7 +7,7 @@ import logging
logger = logging.getLogger("inquisitor.item") logger = logging.getLogger("inquisitor.item")
def create_item(source, item_id, title, link=None, ts=None, author=None, body=None): def create_item(source, item_id, title, link=None, ts=None, author=None, body=None, tags=None):
import time import time
item = { item = {
'id': item_id, 'id': item_id,
@ -15,15 +15,12 @@ def create_item(source, item_id, title, link=None, ts=None, author=None, body=No
'active': True, 'active': True,
'created': time.time(), 'created': time.time(),
'title': title, 'title': title,
'link': link,
'time': ts,
'author': author,
'body': body,
'tags': [] if tags is None else tags,
} }
if link is not None:
item['link'] = link
if ts is not None:
item['time'] = ts
if author is not None:
item['author'] = author
if body is not None:
item['body'] = body
return item return item
import builtins import builtins

View File

@ -38,8 +38,19 @@
{% else %} {% else %}
<span class="item-title">{{item.title}}</span><br> <span class="item-title">{{item.title}}</span><br>
{% endif %} {% endif %}
<span class="item-info">{{item.time_readable}} {{item.author}}</span><br> {% if item.author or item.time %}
<span class="item-info">{{item.source}} {{item.id}} {{item.created}}</span> <span class="item-info">
{% if item.author %}
{{item.author}}
{% endif %}
{% if item.time %}
{{item.time|datetimeformat}}
{% endif %}
</span><br>
{% endif %}
<span class="item-info">
{{item.source}} {{item.id}} {{item.created|datetimeformat}}
</span>
</div> </div>
{% endfor %} {% endfor %}
{% else %} {% else %}