From ad7ceefddfcc1fd59e6448839db7f13898174e00 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Sun, 26 Apr 2020 16:51:40 +0200 Subject: [PATCH] hooks to create view --- oshipka/webapp/views.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/oshipka/webapp/views.py b/oshipka/webapp/views.py index 893f588..9fd423b 100644 --- a/oshipka/webapp/views.py +++ b/oshipka/webapp/views.py @@ -53,7 +53,7 @@ def update_view(model_view, template): return inner -def create_view(model_view, template): +def create_view(model_view, template, post_add=None, post_create=None): def inner(): if request.method == "GET": return render_template(template) @@ -64,7 +64,12 @@ def create_view(model_view, template): serialized_form['uuid'] = str(uuid4()) instance = model_view.model(**serialized_form) db.session.add(instance) + if post_add is not None: + post_add(instance) + db.session.commit() + if post_create is not None: + post_create(instance) flash("Created {}".format(model_view.model_name)) return redirect(_next or request.referrer or url_for('home')) @@ -102,11 +107,11 @@ class ModelView(object): self.model_name = model.__name__.lower() self.model_name_pl = p.plural(self.model_name) - def register_create(self, list_template): + def register_create(self, list_template, **kwargs): url = '/{}/create'.format(self.model_name_pl) self.app.add_url_rule(url, methods=["GET", "POST"], endpoint='create_{}'.format(self.model_name), - view_func=create_view(self, list_template)) + view_func=create_view(self, list_template, **kwargs)) def register_list(self, list_template): url = '/{}'.format(self.model_name_pl)