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,10 +49,11 @@ def main():
args = parse_args(commands)
# Configure logging.
loglevel = getattr(logging, args.log.upper())
if not isinstance(loglevel, int):
raise ValueError("Invalid log level: {}".format(args.log))
logging.basicConfig(format='[%(levelname)s:%(filename)s:%(lineno)d] %(message)s', level=loglevel)
if args.command != 'run':
loglevel = getattr(logging, args.log.upper())
if not isinstance(loglevel, int):
raise ValueError("Invalid log level: {}".format(args.log))
logging.basicConfig(format='[%(levelname)s:%(filename)s:%(lineno)d] %(message)s', level=loglevel)
# Execute command.
if args.command:

View File

@ -1,5 +1,5 @@
# Standard library imports
from datetime import datetime
import datetime
import logging
# Third party imports
@ -18,27 +18,46 @@ console.setFormatter(formatter)
logger.addHandler(console)
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("/")
def root():
dungeon = dungeonlib.Dungeon("dungeon")
filter_lambda = list_filter(
whitelist=request.args.get('wl'),
blacklist=request.args.get('bl'))
active_items = []
for cell in dungeon:
for item_id in dungeon[cell]:
for cell_name in dungeon:
cell = dungeon[cell_name]
for item_id in cell:
try:
item = dungeon[cell][item_id]
if item['active']:
active_items.append(item)
item = cell[item_id]
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)))
return render_template("feed.html", items=active_items[:100])
@app.route("/deactivate/", methods=['POST'])
def deactivate():
dungeon = dungeonlib.Dungeon("dungeon")
params = request.get_json()
if 'source' not in params and 'itemid' not in params:
logger.error("Bad request params: {}".format(params))

View File

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

View File

@ -7,7 +7,7 @@ import logging
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
item = {
'id': item_id,
@ -15,15 +15,12 @@ def create_item(source, item_id, title, link=None, ts=None, author=None, body=No
'active': True,
'created': time.time(),
'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
import builtins

View File

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