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

[rest api] improve error messages, and return item object upon create/update #443

Merged
merged 2 commits into from
May 7, 2017
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
Binary file removed examples/employees/config.pyc
Binary file not shown.
1 change: 0 additions & 1 deletion flask_appbuilder/models/datamodel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# for Retro compatibility purposes
from .sqla.interface import SQLAInterface as SQLAModel

63 changes: 35 additions & 28 deletions flask_appbuilder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<pk>', methods=['GET'])
@has_access_api
@permission_name('show')
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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/<pk>', 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()

Expand All @@ -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/<pk>', methods=['DELETE'])
Expand Down