Pretty print data from Query tab (#257)

This commit is contained in:
Raphaël Pinson
2016-06-21 03:26:15 +02:00
committed by Corey Hammerton
parent fac55edc98
commit 4887588662
4 changed files with 44 additions and 2 deletions

View File

@@ -21,7 +21,7 @@ from pypuppetdb import connect
from puppetboard.forms import (CatalogForm, QueryForm) from puppetboard.forms import (CatalogForm, QueryForm)
from puppetboard.utils import ( from puppetboard.utils import (
get_or_abort, yield_or_stop, get_or_abort, yield_or_stop,
jsonprint, Pagination jsonprint, prettyprint, Pagination
) )
@@ -34,6 +34,7 @@ graph_facts += app.config['GRAPH_FACTS']
app.secret_key = app.config['SECRET_KEY'] app.secret_key = app.config['SECRET_KEY']
app.jinja_env.filters['jsonprint'] = jsonprint app.jinja_env.filters['jsonprint'] = jsonprint
app.jinja_env.filters['prettyprint'] = prettyprint
puppetdb = connect( puppetdb = connect(
host=app.config['PUPPETDB_HOST'], host=app.config['PUPPETDB_HOST'],

View File

@@ -4,7 +4,7 @@ from __future__ import absolute_import
from flask.ext.wtf import Form from flask.ext.wtf import Form
from wtforms import ( from wtforms import (
HiddenField, RadioField, SelectField, HiddenField, RadioField, SelectField,
TextAreaField, validators TextAreaField, BooleanField, validators
) )
@@ -26,6 +26,7 @@ class QueryForm(Form):
('edges', 'Edges'), ('edges', 'Edges'),
('environments', 'Environments'), ('environments', 'Environments'),
]) ])
rawjson = BooleanField('Raw JSON')
class CatalogForm(Form): class CatalogForm(Form):
"""The form used to compare the catalogs of different nodes.""" """The form used to compare the catalogs of different nodes."""

View File

@@ -26,6 +26,11 @@
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<div class="inline fields">
<div class="field">
{{ form.rawjson(active=True) }} Raw JSON
</div>
</div>
<input type=submit class="ui submit button" value='Submit'> <input type=submit class="ui submit button" value='Submit'>
<input type=reset class="ui red submit button" value='Cancel'> <input type=reset class="ui red submit button" value='Cancel'>
</form> </form>
@@ -35,7 +40,13 @@
<div class="row"> <div class="row">
<div class="span12"> <div class="span12">
<h2>Result</h2> <h2>Result</h2>
{% if form.rawjson.data %}
<pre><code>{{ result|jsonprint }}</code></pre> <pre><code>{{ result|jsonprint }}</code></pre>
{% else %}
{% autoescape false %}
{{ result|prettyprint}}
{% endautoescape %}
{% endif %}
</div> </div>
</div> </div>
{% endif %} {% endif %}

View File

@@ -22,6 +22,35 @@ log = logging.getLogger(__name__)
def jsonprint(value): def jsonprint(value):
return json.dumps(value, indent=2, separators=(',', ': ')) return json.dumps(value, indent=2, separators=(',', ': '))
def formatvalue(value):
if isinstance(value, str):
return value
elif isinstance(value, list):
return ", ".join(value)
elif isinstance(value, dict):
ret = ""
for k in value:
ret += k+" => "+formatvalue(value[k])+",<br/>"
return ret
else:
return str(value)
def prettyprint(value):
html = '<table class="ui basic fixed table"><tr>'
# Get keys
for k in value[0]:
html += "<th>"+k+"</th>"
for e in value:
html += "<tr>"
for k in e:
html += "<td>"+formatvalue(e[k])+"</td>"
html += "</tr>"
html += "</tr>"
html += "</table>"
return(html)
def get_or_abort(func, *args, **kwargs): def get_or_abort(func, *args, **kwargs):
"""Execute the function with its arguments and handle the possible """Execute the function with its arguments and handle the possible