From c66545404555b71b4e7c597814c3d72932d4b640 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Wed, 3 Jun 2020 12:31:59 +0200 Subject: [PATCH] added hooks to update --- oshipka/webapp/views.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/oshipka/webapp/views.py b/oshipka/webapp/views.py index 6a8f9ef..189f18c 100644 --- a/oshipka/webapp/views.py +++ b/oshipka/webapp/views.py @@ -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 = '/{}//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 = '/{}//delete'.format(self.model_name_pl)