From 6be22246e967f6095e0de4fd1c011ce1e3091605 Mon Sep 17 00:00:00 2001 From: Maxime Date: Tue, 28 Jul 2015 23:06:06 +0000 Subject: [PATCH] Preventing bad json from creating problems --- app/models.py | 7 ++++++- app/views.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/models.py b/app/models.py index 2838813cae421..0c5b266fc826b 100644 --- a/app/models.py +++ b/app/models.py @@ -115,7 +115,12 @@ class Metric(Model): @property def json_obj(self): - return json.loads(self.json) + try: + obj = json.loads(self.json) + except Exception as e: + obj = {} + return obj + class Column(Model, AuditMixin): __tablename__ = 'columns' diff --git a/app/views.py b/app/views.py index c55d9cdbc5bf9..280cc14a5d72a 100644 --- a/app/views.py +++ b/app/views.py @@ -9,6 +9,14 @@ from flask.ext.appbuilder.security.decorators import has_access, permission_name import config from pydruid.client import doublesum +from wtforms.validators import ValidationError + + +def validate_json(form, field): + try: + json.loads(field.data) + except Exception as e: + raise ValidationError("Json isn't valid") class ColumnInlineView(CompactCRUDMixin, ModelView): @@ -21,6 +29,13 @@ class ColumnInlineView(CompactCRUDMixin, ModelView): 'sum', 'min', 'max'] can_delete = False page_size = 100 + + def post_update(self, col): + col.generate_metrics() + + def post_update(self, col): + col.generate_metrics() + appbuilder.add_view_no_menu(ColumnInlineView) @@ -33,6 +48,9 @@ class MetricInlineView(CompactCRUDMixin, ModelView): add_columns = [ 'metric_name', 'verbose_name', 'metric_type', 'datasource', 'json'] page_size = 100 + validators_columns = { + 'json': [validate_json], + } appbuilder.add_view_no_menu(MetricInlineView) @@ -46,6 +64,12 @@ class DatasourceModelView(ModelView): page_size = 100 base_order = ('datasource_name', 'asc') + def post_insert(self, datasource): + datasource.generate_metrics() + + def post_update(self, datasource): + datasource.generate_metrics() + appbuilder.add_view( DatasourceModelView,