diff --git a/examples/employees/config.pyc b/examples/employees/config.pyc deleted file mode 100644 index 9862e2bd12..0000000000 Binary files a/examples/employees/config.pyc and /dev/null differ diff --git a/flask_appbuilder/models/datamodel.py b/flask_appbuilder/models/datamodel.py index b8563fa5fb..ac64475a75 100644 --- a/flask_appbuilder/models/datamodel.py +++ b/flask_appbuilder/models/datamodel.py @@ -1,4 +1,3 @@ # for Retro compatibility purposes from .sqla.interface import SQLAInterface as SQLAModel - diff --git a/flask_appbuilder/views.py b/flask_appbuilder/views.py index 1677345a8c..566e79e3c2 100644 --- a/flask_appbuilder/views.py +++ b/flask_appbuilder/views.py @@ -231,6 +231,16 @@ def api_read(self): response.headers['Content-Type'] = "application/json" return response + def show_item_dict(self, item): + """Returns a json-able dict for show""" + d = {} + for col in self.show_columns: + v = getattr(item, col) + if not isinstance(v, (int, float, unicode, str)): + v = str(v) + d[col] = v + return d + @expose_api(name='get', url='/api/get/', methods=['GET']) @has_access_api @permission_name('show') @@ -241,15 +251,11 @@ def api_get(self, pk): item = self.datamodel.get(pk, self._base_filters) if not item: abort(404) - _item = dict() - for col in self.show_columns: - _item[col] = str(getattr(item, col)) - ret_json = jsonify(pk=pk, label_columns=self._label_columns_json(), include_columns=self.show_columns, modelview_name=self.__class__.__name__, - result=_item) + result=self.show_item_dict(item)) response = make_response(ret_json, 200) response.headers['Content-Type'] = "application/json" return response @@ -258,11 +264,9 @@ def api_get(self, pk): @has_access_api @permission_name('add') def api_create(self): - is_valid_form = True get_filter_args(self._filters) exclude_cols = self._filters.get_relation_cols() form = self.add_form.refresh() - self._fill_form_exclude_cols(exclude_cols, form) if form.validate(): item = self.datamodel.obj() @@ -273,22 +277,23 @@ def api_create(self): http_return_code = 200 else: http_return_code = 500 + payload = { + 'message': self.datamodel.message[0], + 'item': self.show_item_dict(item), + 'severity': self.datamodel.message[1], + } else: - is_valid_form = False - if is_valid_form: - response = make_response(jsonify({'message': self.datamodel.message[0], - 'severity': self.datamodel.message[1]}), http_return_code) - else: - # TODO return dict with errors - response = make_response(jsonify({'message': 'Invalid form', - 'severity': 'warning'}), 500) - return response + payload = { + 'message': 'Validation error', + 'error_details': form.errors, + } + http_return_code = 500 + return make_response(jsonify(payload), http_return_code) @expose_api(name='update', url='/api/update/', methods=['PUT']) @has_access_api @permission_name('edit') def api_update(self, pk): - is_valid_form = True get_filter_args(self._filters) exclude_cols = self._filters.get_relation_cols() @@ -303,24 +308,26 @@ def api_update(self, pk): self._fill_form_exclude_cols(exclude_cols, form) # trick to pass unique validation form._id = pk + http_return_code = 500 if form.validate(): form.populate_obj(item) self.pre_update(item) if self.datamodel.edit(item): self.post_update(item) http_return_code = 200 - else: - http_return_code = 500 + payload = { + 'message': self.datamodel.message[0], + 'severity': self.datamodel.message[1], + 'item': self.show_item_dict(item), + } else: - is_valid_form = False - if is_valid_form: - response = make_response(jsonify({'message': self.datamodel.message[0], - 'severity': self.datamodel.message[1]}), http_return_code) - else: - # TODO return dict with from errors validation - response = make_response(jsonify({'message': 'Invalid form', - 'severity': 'warning'}), 500) - return response + payload = { + 'message': 'Validation error', + 'error_details': form.errors, + 'severity': 'warning', + } + return make_response(jsonify(payload), http_return_code) + @expose_api(name='delete', url='/api/delete/', methods=['DELETE'])