Add basic db explorer

This commit is contained in:
Michal Kunc 2021-11-17 11:12:31 +01:00
parent dd89826a8d
commit d9b9e10d56
Signed by: michal
GPG Key ID: 4CA5FB6559E0BDF8
13 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,58 @@
import urllib.parse
import os
import re
from flask import Flask, render_template
from flask_pymongo import PyMongo
app = Flask(__name__)
app.config["MONGO_URI"] = os.environ["MONGO_URI"]
mongo = PyMongo(app)
@app.template_filter('filter_vtt')
def filter_vtt(text):
compendium_re = re.compile(r"@Compendium\[([^\]]+)\]\{([^\}]+)\}")
def compendium_return(m):
return f"<a>{m.group(2)}</a>"
macro_re = re.compile(r"\[\[\/\S?r\s(?:(?P<sdice>[^\{\] ]+)[^\{\]]*|(?:\{(?P<ldice>[^\}]+)\}\[[^\]]+\])?)\]\](?:\{(?P<alt>[^\}]+)\})?")
def macro_return(m):
if m.group('alt') is not None:
return m.group('alt')
return m.group('sdice')
text = compendium_re.sub(compendium_return, text)
text = macro_re.sub(macro_return, text)
return text
@app.route("/")
def list_types():
types = [ {"name": "Actions", "href": "action"} ]
return render_template("list.html", header="All types", items=types)
@app.route("/action")
def list_actions():
items = list(map(lambda x: {"name": x["name"], "href": "./action/" + urllib.parse.quote(x["name"], safe='')}, mongo.db.action.find({}, {"name": 1}).sort("name", 1)))
return render_template("list.html", header="All actions", items=items)
@app.route("/action/<name>")
def show_action(name):
item = mongo.db.action.find_one_or_404({"name": name})
print(item)
if item["data"]["actions"]["value"] == None:
if item["data"]["actionType"]["value"] == 'passive':
item['_action_icon'] = None
elif item["data"]["actionType"]["value"] == 'reaction':
item['_action_icon'] = 'r'
elif item["data"]["actionType"]["value"] == 'free':
item['_action_icon'] = 0
else:
raise ValueError(f'Unknown action type: {item["data"]["actionType"]["value"]}')
elif item["data"]["actions"]["value"] in (1,2,3):
item['_action_icon'] = item["data"]["actions"]["value"]
else:
raise ValueError(f'Unknown action: {item["data"]["actionType"]["value"]} - {item["data"]["actions"]["value"]}')
return render_template("action.html", action=item)
if __name__ == "__main__":
app.run()

View File

@ -0,0 +1,2 @@
from flask import Blueprint

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1000 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -0,0 +1,10 @@
.action-icon {
height: 26px;
margin-left: 5px;
}
.sourcebook {
text-align: right;
font-size: 0.8em;
}

View File

@ -0,0 +1,15 @@
{% import "_const.html" as c -%}
<!DOCTYPE html>
<html>
<head>
{% block head %}
<title>{% block title %}PF2 DB{% endblock %}</title>
{% endblock %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/base.css') }}"
</head>
<body>
{% block body %}
<i>This page is unintentionally left empty</i>
{% endblock %}
</body>
</html>

View File

@ -0,0 +1,16 @@
{% macro _abase(url) -%}
<img class="action-icon" src="{{ url }}">
{%- endmacro %}
{% set afree = _abase(url_for("static", filename="activity/free.webp")) -%}
{% set aone = _abase(url_for("static", filename="activity/one.webp")) -%}
{% set atwo = _abase(url_for("static", filename="activity/two.webp")) -%}
{% set athree = _abase(url_for("static", filename="activity/three.webp")) -%}
{% set areaction = _abase(url_for("static", filename="activity/reaction.webp")) -%}
{% set actions = {
None: "",
0: afree,
1: aone,
2: atwo,
3: athree,
'r': areaction
} -%}

View File

@ -0,0 +1,7 @@
{% extends "_base.html" %}
{% block title %}{{action.name}} - PF2 DB{% endblock %}
{% block body %}
<h1>{{action["name"]}}{{c.actions[action["_action_icon"]]|safe}}</h1>
<p>{{action["data"]["description"]["value"]|filter_vtt|safe}}</p>
<p class="sourcebook">{{action["data"]["source"]["value"]}}</p>
{% endblock %}

View File

@ -0,0 +1,10 @@
{% extends "_base.html" %}
{% block title %}{{header}} - PF2 DB {% endblock %}
{% block body %}
<h1>{{ header }}</h1>
<ul>
{% for item in items -%}
<li><a href="{{item.href}}">{{item.name}}</a></li>
{%- endfor %}
</ul>
{% endblock %}

View File

@ -0,0 +1,17 @@
[tool.poetry]
name = "db-explorer"
version = "0.1.0"
description = ""
authors = ["Michal Kunc <michkunc@gmail.com>"]
readme = "README.md"
packages = [{include = "db_explorer"}]
[tool.poetry.dependencies]
python = "^3.8"
Flask = "^2.0.2"
Flask-PyMongo = "^2.3.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"