Skip to content

Commit

Permalink
Preventing bad json from creating problems
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jul 28, 2015
1 parent fb365cd commit 6be2224
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
24 changes: 24 additions & 0 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)


Expand All @@ -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)


Expand All @@ -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,
Expand Down

0 comments on commit 6be2224

Please sign in to comment.