Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing bad json from creating problems #8

Merged
merged 1 commit into from
Jul 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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