Skip to content

Commit

Permalink
REST JSON create
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar committed Mar 12, 2015
1 parent 50ed3ae commit 64510d8
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions flask_appbuilder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,6 @@ def api(self):
@expose('/api/read', methods=['GET'])
def api_read(self):
"""
Parameters are passed as JSON with the following structure
{ 'order_colum':'<COLUMN_NAME>',
'order_direction':'<asc|desc>',
'page':'<PAGE NUMBER>',
'page_size':'<PAGE SIZE>',
'filter' : [['<COLUMN_NAME>','<FILTER TYPE>','<VALUE>']...]
}
"""
# Get arguments for ordering
if get_order_args().get(self.__class__.__name__):
Expand Down Expand Up @@ -333,7 +325,32 @@ def api_read(self):
@has_access_api
@permission_name('add')
def api_create(self):
pass
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()
form.populate_obj(item)
self.pre_add(item)
if self.datamodel.add(item):
self.post_add(item)
http_return_code = 200
else:
http_return_code = 500
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


@expose('/api/update/<pk>', methods=['PUT'])
@has_access_api
Expand Down Expand Up @@ -380,9 +397,12 @@ def api_column(self, col_name):

class ModelView(RestCRUDView):
"""
This is the CRUD generic view. If you want to automatically implement create, edit, delete, show, and list from your database tables, inherit your views from this class.
This is the CRUD generic view.
If you want to automatically implement create, edit,
delete, show, and list from your database tables, inherit your views from this class.
Notice that this class inherits from BaseCRUDView and BaseModelView so all properties from the parent class can be overriden.
Notice that this class inherits from BaseCRUDView and BaseModelView
so all properties from the parent class can be overriden.
"""

def __init__(self, **kwargs):
Expand Down

0 comments on commit 64510d8

Please sign in to comment.