fix model_view

This commit is contained in:
Daniel Tsvetkov 2020-06-29 09:26:11 +02:00
parent 1e9776ee82
commit 03a747f115
2 changed files with 19 additions and 17 deletions

View File

@ -205,9 +205,7 @@
<script src="{{ url_for('oshipka_bp.static', filename='js/fuse.js') }}"></script> <script src="{{ url_for('oshipka_bp.static', filename='js/fuse.js') }}"></script>
<script src="{{ url_for('oshipka_bp.static', filename='js/draggable.js') }}"></script> <script src="{{ url_for('oshipka_bp.static', filename='js/draggable.js') }}"></script>
<script src="{{ url_for('oshipka_bp.static', filename='js/oshipka.js') }}"></script> <script src="{{ url_for('oshipka_bp.static', filename='js/oshipka.js') }}"></script>
{% block script %}
<script> <script>
$("select").chosen({ $("select").chosen({
inherit_select_classes: true, inherit_select_classes: true,
}); });
@ -220,6 +218,6 @@ lightbox.option({
$('.data-table').DataTable(); $('.data-table').DataTable();
</script> </script>
{% endblock %} {% block script %}{% endblock %}
</body> </body>
</html> </html>

View File

@ -144,7 +144,7 @@ class ViewContext(object):
jsonify_func=None, render_func=None, template_func=None, template_ctx_func=None, jsonify_func=None, render_func=None, template_func=None, template_ctx_func=None,
should_redirect_no_instances_func=None, should_redirect_no_instances_func=None,
should_redirect_at_end_func=None, should_redirect_at_end_func=None,
is_json=False, **kwargs): is_json=False, model_view=None, **kwargs):
self.args_get_func = args_get_func or default_get_args_func self.args_get_func = args_get_func or default_get_args_func
self.args_process_func = args_process_func or default_none_func self.args_process_func = args_process_func or default_none_func
self.filter_func = filter_func or default_none_func self.filter_func = filter_func or default_none_func
@ -161,6 +161,7 @@ class ViewContext(object):
self.template_ctx_func = template_ctx_func or default_none_func self.template_ctx_func = template_ctx_func or default_none_func
self.should_redirect_at_end_func = should_redirect_at_end_func or default_none_func self.should_redirect_at_end_func = should_redirect_at_end_func or default_none_func
self.is_json = is_json self.is_json = is_json
self.model_view = model_view
self.serialized_args = {} self.serialized_args = {}
self.url_args = {} self.url_args = {}
@ -169,22 +170,23 @@ class ViewContext(object):
self.should_redirect_at_end = not is_json self.should_redirect_at_end = not is_json
self.template = None self.template = None
self.template_ctx = {} self.template_ctx = {}
self.model_view = None
self.redirect_next = None self.redirect_next = None
def create_view(model_view, view_context, is_login_required=False, the_roles_required=None, **kwargs): def return_json_or_template(view_context):
view_context.model_view = model_view
the_roles_required = [] if not the_roles_required else the_roles_required
def return_json_or_template():
if view_context.is_json: if view_context.is_json:
return view_context.jsonify_func(view_context) return view_context.jsonify_func(view_context)
view_context.template_func(view_context) view_context.template_func(view_context)
view_context.template_ctx_func(view_context) view_context.template_ctx_func(view_context)
return view_context.render_func(view_context) return view_context.render_func(view_context)
def create_view(model_view, view_context_kwargs, is_login_required=False, the_roles_required=None, **kwargs):
view_context_kwargs['model_view'] = model_view
the_roles_required = [] if not the_roles_required else the_roles_required
def inner(**kwargs): def inner(**kwargs):
view_context = ViewContext(**view_context_kwargs)
view_context.url_args = kwargs view_context.url_args = kwargs
view_context.args_get_func(view_context) view_context.args_get_func(view_context)
view_context.args_process_func(view_context) view_context.args_process_func(view_context)
@ -195,7 +197,7 @@ def create_view(model_view, view_context, is_login_required=False, the_roles_req
return view_context.redirect_func(view_context) return view_context.redirect_func(view_context)
if request.method == "GET": if request.method == "GET":
return return_json_or_template() return return_json_or_template(view_context)
view_context.should_execute_func(view_context) view_context.should_execute_func(view_context)
if not view_context.should_execute: if not view_context.should_execute:
@ -213,7 +215,7 @@ def create_view(model_view, view_context, is_login_required=False, the_roles_req
if view_context.should_redirect_at_end: if view_context.should_redirect_at_end:
return view_context.redirect_func(view_context) return view_context.redirect_func(view_context)
return return_json_or_template() return return_json_or_template(view_context)
if is_login_required: if is_login_required:
if the_roles_required: if the_roles_required:
inner = roles_required(*the_roles_required)(inner) inner = roles_required(*the_roles_required)(inner)
@ -243,12 +245,14 @@ class ModelView(object):
endpoint = url_args.pop('endpoint') endpoint = url_args.pop('endpoint')
api_endpoint = 'api_{}'.format(endpoint) api_endpoint = 'api_{}'.format(endpoint)
view_func = url_args.pop('view_func') view_func = url_args.pop('view_func')
view_context_kwargs = copy(kwargs['view_context'].__dict__)
kwargs['view_context_kwargs'] = view_context_kwargs
self.app.add_url_rule(rule=url, endpoint=endpoint, self.app.add_url_rule(rule=url, endpoint=endpoint,
view_func=view_func(self, **kwargs), **url_args) view_func=view_func(self, **kwargs), **url_args)
view_context_kwargs = copy(kwargs['view_context'].__dict__) view_context_kwargs = copy(kwargs['view_context'].__dict__)
view_context_kwargs['is_json'] = True view_context_kwargs['is_json'] = True
kwargs['view_context'] = ViewContext(**view_context_kwargs) kwargs['view_context_kwargs'] = view_context_kwargs
self.app.add_url_rule(rule=api_url, endpoint=api_endpoint, self.app.add_url_rule(rule=api_url, endpoint=api_endpoint,
view_func=view_func(self, **kwargs), **url_args) view_func=view_func(self, **kwargs), **url_args)