added more hooks

This commit is contained in:
Daniel Tsvetkov 2020-04-26 19:00:27 +02:00
parent ad7ceefddf
commit 53aa2b6527

View File

@ -6,15 +6,16 @@ from flask import flash, render_template, redirect, request, url_for
from oshipka.persistance import db from oshipka.persistance import db
def list_view(model_view, template): def list_view(model_view, template, template_ctx_func=None):
def inner(): def inner():
instances = model_view.model.query.all() instances = model_view.model.query.all()
return render_template(template, instances=instances) template_ctx = template_ctx_func(instances) if template_ctx_func else {}
return render_template(template, instances=instances, **template_ctx)
return inner return inner
def get_view(model_view, template, template_ctx_func): def get_view(model_view, template, template_ctx_func=None):
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()
@ -113,18 +114,18 @@ class ModelView(object):
endpoint='create_{}'.format(self.model_name), endpoint='create_{}'.format(self.model_name),
view_func=create_view(self, list_template, **kwargs)) view_func=create_view(self, list_template, **kwargs))
def register_list(self, list_template): def register_list(self, list_template, **kwargs):
url = '/{}'.format(self.model_name_pl) url = '/{}'.format(self.model_name_pl)
self.app.add_url_rule(url, self.app.add_url_rule(url,
'list_{}'.format(self.model_name), 'list_{}'.format(self.model_name),
list_view(self, list_template)) list_view(self, list_template, **kwargs))
def register_get(self, retrieve_template, template_ctx_func=None): def register_get(self, retrieve_template, **kwargs):
url = '/{}/<uuid>'.format(self.model_name_pl) url = '/{}/<uuid>'.format(self.model_name_pl)
self.app.add_url_rule(url, self.app.add_url_rule(url,
'get_{}'.format(self.model_name), 'get_{}'.format(self.model_name),
get_view(self, retrieve_template, template_ctx_func)) get_view(self, retrieve_template, **kwargs))
def register_update(self, update_template): def register_update(self, update_template):
url = '/{}/<uuid>/edit'.format(self.model_name_pl) url = '/{}/<uuid>/edit'.format(self.model_name_pl)