Skip to content

Commit

Permalink
Form factory refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Sep 9, 2015
1 parent 6dd81a3 commit 1283bc0
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 152 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
* Save / bookmark / url shortener
* SQL: Find a way to manage granularity
* Create ~/.panoramix/ to host DB and config, generate default config there
* Add a per-datasource permission

90 changes: 90 additions & 0 deletions panoramix/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from wtforms import Field, Form, SelectMultipleField, SelectField, TextField
from flask_appbuilder.fieldwidgets import Select2Widget, Select2ManyWidget




class OmgWtForm(Form):
field_order = tuple()
css_classes = dict()
@property
def fields(self):
fields = []
for field in self.field_order:
if hasattr(self, field):
obj = getattr(self, field)
if isinstance(obj, Field):
fields.append(getattr(self, field))
return fields

def get_field(self, fieldname):
return getattr(self, fieldname)

def field_css_classes(self, fieldname):
if fieldname in self.css_classes:
return " ".join(self.css_classes[fieldname])
return ""


def form_factory(datasource, viz, form_args=None):
from panoramix.viz import viz_types
row_limits = [10, 50, 100, 500, 1000, 5000, 10000]
series_limits = [0, 5, 10, 25, 50, 100, 500]
group_by_choices = [(s, s) for s in datasource.groupby_column_names]
# Pool of all the fields that can be used in Panoramix
px_form_fields = {
'viz_type': SelectField(
'Viz',
choices=[(k, v.verbose_name) for k, v in viz_types.items()]),
'metrics': SelectMultipleField(
'Metrics', choices=datasource.metrics_combo),
'groupby': SelectMultipleField(
'Group by',
choices=[(s, s) for s in datasource.groupby_column_names]),
'granularity': TextField('Time Granularity', default="one day"),
'since': TextField('Since', default="one day ago"),
'until': TextField('Until', default="now"),
'row_limit':
SelectField(
'Row limit', choices=[(s, s) for s in row_limits]),
'limit':
SelectField(
'Series limit', choices=[(s, s) for s in series_limits]),
'rolling_type': SelectField(
'Rolling',
choices=[(s, s) for s in ['mean', 'sum', 'std']]),
'rolling_periods': TextField('Periods',),
'series': SelectField('Series', choices=group_by_choices),
'entity': SelectField('Entity', choices=group_by_choices),
'x': SelectField('X Axis', choices=datasource.metrics_combo),
'y': SelectField('Y Axis', choices=datasource.metrics_combo),
'size': SelectField('Bubble Size', choices=datasource.metrics_combo),
}
field_css_classes = {k: ['form-control'] for k in px_form_fields.keys()}
select2 = [
'viz_type', 'metrics', 'groupby',
'row_limit', 'rolling_type', 'series',
'entity', 'x', 'y', 'size',]
field_css_classes['since'] += ['select2_free_since']
field_css_classes['until'] += ['select2_free_until']
field_css_classes['granularity'] += ['select2_free_granularity']
for field in select2:
field_css_classes[field] += ['select2']


class QueryForm(OmgWtForm):
field_order = viz.form_fields
css_classes = field_css_classes

for i in range(10):
setattr(QueryForm, 'flt_col_' + str(i), SelectField(
'Filter 1', choices=[(s, s) for s in datasource.filterable_column_names]))
setattr(QueryForm, 'flt_op_' + str(i), SelectField(
'Filter 1', choices=[(m, m) for m in ['in', 'not in']]))
setattr(QueryForm, 'flt_eq_' + str(i), TextField("Super"))
for ff in viz.form_fields:
if isinstance(ff, basestring):
ff = [ff]
for s in ff:
setattr(QueryForm, s, px_form_fields[s])
return QueryForm
47 changes: 21 additions & 26 deletions panoramix/templates/panoramix/datasource.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,27 @@ <h3>

<hr>
<form id="query" method="GET" style="display: none;">
<div>{{ form.viz_type.label }}: {{ form.viz_type(class_="form-control select2") }}</div>
{% if 'metrics' not in viz.hidden_fields %}
<div>{{ form.metrics.label }}: {{ form.metrics(class_="form-control select2") }}</div>
{% endif %}
{% if 'granularity' not in viz.hidden_fields %}
<div>{{ form.granularity.label }}
<i class="fa fa-info-circle" data-toggle="tooltip" data-placement="right"
title="Supports natural language time as in '10 seconds', '1 day' or '1 week'"
id="blah"></i>
{{ form.granularity(class_="form-control select2_free_granularity") }}</div>
{% endif %}
<div class="row">
<div class="form-group">
<div class="col-xs-6">{{ form.since.label }}
<i class="fa fa-info-circle" data-toggle="tooltip" data-placement="right"
title="Supports natural language time as in '1 day ago', '28 days' or '3 years'"
id="blah"></i>
{{ form.since(class_="form-control select2_free_since") }}</div>
<div class="col-xs-6">{{ form.until.label }}
{{ form.until(class_="form-control select2_free_until") }}</div>
</div>
</div>
{% if 'groupby' not in viz.hidden_fields %}
<div>{{ form.groupby.label }}: {{ form.groupby(class_="form-control select2") }}</div>
{% endif %}
{% block extra_fields %}{% endblock %}
{% for fieldname in form.field_order %}
{% if not fieldname.__iter__ %}
<div>
{% set field = form.get_field(fieldname)%}
{{ field.label }}:
{{ field(class_=form.field_css_classes(field.name)) }}
</div>
{% else %}
<div class="row">
<div class="form-group">
{% for name in fieldname %}
<div class="col-xs-{{ (12 / fieldname|length) | int }}">
{% set field = form.get_field(name)%}
{{ field.label }}:
{{ field(class_=form.field_css_classes(field.name)) }}
</div>
{% endfor %}
</div>
</div>
{% endif %}
{% endfor %}
<hr>
<h4>Filters</h4>
<div id="flt0" style="display: none;">
Expand Down
30 changes: 0 additions & 30 deletions panoramix/templates/panoramix/viz_highcharts.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,6 @@
<div id="chart"></div>
{% endblock %}

{% block extra_fields %}
{% if form.compare %}
<div>{{ form.compare.label }}: {{ form.compare(class_="form-control") }}</div>
{% endif %}
{% if form.rolling_type %}
<div class="row">
<span class="col col-sm-5">{{ form.rolling_type.label }}: {{ form.rolling_type(class_="form-control select2") }}</span>
<span class="col col-sm-4">{{ form.rolling_periods.label }}: {{ form.rolling_periods(class_="form-control") }}</span>
</div>
{% endif %}
{% if form.limit %}
<div>{{ form.limit.label }}: {{ form.limit(class_="form-control select2") }}</div>
{% endif %}
{% if form.series %}
<div>{{ form.series.label }}: {{ form.series(class_="form-control select2") }}</div>
{% endif %}
{% if form.entity %}
<div>{{ form.entity.label }}: {{ form.entity(class_="form-control select2") }}</div>
{% endif %}
{% if form.size %}
<div>{{ form.size.label }}: {{ form.size(class_="form-control select2") }}</div>
{% endif %}
{% if form.x %}
<div>{{ form.x.label }}: {{ form.x(class_="form-control select2") }}</div>
{% endif %}
{% if form.y %}
<div>{{ form.y.label }}: {{ form.y(class_="form-control select2") }}</div>
{% endif %}
{% endblock %}

{% block tail %}
{{ super() }}
{% if viz.stockchart %}
Expand Down
4 changes: 0 additions & 4 deletions panoramix/templates/panoramix/viz_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
{% endif %}
{% endblock %}

{% block extra_fields %}
<div>{{ form.row_limit.label }}: {{ form.row_limit(class_="form-control select2") }}</div>
{% endblock %}

{% block tail %}
{{ super() }}
<script src="{{ url_for('static', filename='jquery.dataTables.min.js') }}"></script>
Expand Down
Loading

0 comments on commit 1283bc0

Please sign in to comment.