hooks to create view

This commit is contained in:
Daniel Tsvetkov 2020-04-26 16:51:40 +02:00
parent b678d45224
commit ad7ceefddf

View File

@ -53,7 +53,7 @@ def update_view(model_view, template):
return inner return inner
def create_view(model_view, template): def create_view(model_view, template, post_add=None, post_create=None):
def inner(): def inner():
if request.method == "GET": if request.method == "GET":
return render_template(template) return render_template(template)
@ -64,7 +64,12 @@ def create_view(model_view, template):
serialized_form['uuid'] = str(uuid4()) serialized_form['uuid'] = str(uuid4())
instance = model_view.model(**serialized_form) instance = model_view.model(**serialized_form)
db.session.add(instance) db.session.add(instance)
if post_add is not None:
post_add(instance)
db.session.commit() db.session.commit()
if post_create is not None:
post_create(instance)
flash("Created {}".format(model_view.model_name)) flash("Created {}".format(model_view.model_name))
return redirect(_next or request.referrer or url_for('home')) 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 = model.__name__.lower()
self.model_name_pl = p.plural(self.model_name) 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) url = '/{}/create'.format(self.model_name_pl)
self.app.add_url_rule(url, methods=["GET", "POST"], self.app.add_url_rule(url, methods=["GET", "POST"],
endpoint='create_{}'.format(self.model_name), 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): def register_list(self, list_template):
url = '/{}'.format(self.model_name_pl) url = '/{}'.format(self.model_name_pl)