added hooks to update
This commit is contained in:
parent
c493146322
commit
c665454045
@ -33,7 +33,7 @@ def serialize_form():
|
|||||||
return dict(filter(lambda k: not k[0].startswith("__"), dict(request.form).items()))
|
return dict(filter(lambda k: not k[0].startswith("__"), dict(request.form).items()))
|
||||||
|
|
||||||
|
|
||||||
def update_view(model_view, template):
|
def update_view(model_view, template, pre_process_func=None, post_process_func=None, **kwargs):
|
||||||
def inner(uuid):
|
def inner(uuid):
|
||||||
model = model_view.model
|
model = model_view.model
|
||||||
instance = model.query.filter_by(uuid=uuid).first()
|
instance = model.query.filter_by(uuid=uuid).first()
|
||||||
@ -45,10 +45,12 @@ def update_view(model_view, template):
|
|||||||
serialized_form = serialize_form()
|
serialized_form = serialize_form()
|
||||||
|
|
||||||
_next = serialized_form.pop('_next') if '_next' in serialized_form else None
|
_next = serialized_form.pop('_next') if '_next' in serialized_form else None
|
||||||
|
serialized_form = pre_process_func(serialized_form, instance) if pre_process_func else serialized_form
|
||||||
for k, v in serialized_form.items():
|
for k, v in serialized_form.items():
|
||||||
setattr(instance, k, v)
|
setattr(instance, k, v)
|
||||||
db.session.add(instance)
|
db.session.add(instance)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
post_process_func(instance) if post_process_func else instance
|
||||||
flash("Updated {}:{}".format(model_view.model_name, uuid))
|
flash("Updated {}:{}".format(model_view.model_name, uuid))
|
||||||
return redirect(_next or request.referrer or url_for('home'))
|
return redirect(_next or request.referrer or url_for('home'))
|
||||||
|
|
||||||
@ -129,11 +131,11 @@ class ModelView(object):
|
|||||||
'get_{}'.format(self.model_name),
|
'get_{}'.format(self.model_name),
|
||||||
get_view(self, retrieve_template, **kwargs))
|
get_view(self, retrieve_template, **kwargs))
|
||||||
|
|
||||||
def register_update(self, update_template):
|
def register_update(self, update_template, **kwargs):
|
||||||
url = '/{}/<uuid>/edit'.format(self.model_name_pl)
|
url = '/{}/<uuid>/edit'.format(self.model_name_pl)
|
||||||
self.app.add_url_rule(url, methods=["GET", "POST"],
|
self.app.add_url_rule(url, methods=["GET", "POST"],
|
||||||
endpoint='update_{}'.format(self.model_name),
|
endpoint='update_{}'.format(self.model_name),
|
||||||
view_func=update_view(self, update_template))
|
view_func=update_view(self, update_template, **kwargs))
|
||||||
|
|
||||||
def register_delete(self):
|
def register_delete(self):
|
||||||
url = '/{}/<uuid>/delete'.format(self.model_name_pl)
|
url = '/{}/<uuid>/delete'.format(self.model_name_pl)
|
||||||
|
Loading…
Reference in New Issue
Block a user