added hooks to update

This commit is contained in:
Daniel Tsvetkov 2020-06-03 12:31:59 +02:00
parent c493146322
commit c665454045

View File

@ -33,7 +33,7 @@ def serialize_form():
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):
model = model_view.model
instance = model.query.filter_by(uuid=uuid).first()
@ -45,10 +45,12 @@ def update_view(model_view, template):
serialized_form = serialize_form()
_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():
setattr(instance, k, v)
db.session.add(instance)
db.session.commit()
post_process_func(instance) if post_process_func else instance
flash("Updated {}:{}".format(model_view.model_name, uuid))
return redirect(_next or request.referrer or url_for('home'))
@ -129,11 +131,11 @@ class ModelView(object):
'get_{}'.format(self.model_name),
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)
self.app.add_url_rule(url, methods=["GET", "POST"],
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):
url = '/{}/<uuid>/delete'.format(self.model_name_pl)