From db895c86f8337b222814340775a2d13c6bee2ee4 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Sun, 7 Jun 2020 13:13:14 +0200 Subject: [PATCH] relationship create update --- bootstrap/webapp/templates/layout.html | 2 +- oshipka/webapp/static/js/oshipka.js | 6 +++++- oshipka/webapp/views.py | 4 ++++ vm_gen/templates/html/_create.html | 10 +++++++--- vm_gen/templates/html/_get.html | 7 +++++++ vm_gen/templates/html/_update.html | 10 ++++++++++ vm_gen/templates/routes_py | 13 +++++++------ vm_gen/vm_gen.py | 11 +++++++---- 8 files changed, 48 insertions(+), 15 deletions(-) diff --git a/bootstrap/webapp/templates/layout.html b/bootstrap/webapp/templates/layout.html index dbae59e..9ebe1f0 100644 --- a/bootstrap/webapp/templates/layout.html +++ b/bootstrap/webapp/templates/layout.html @@ -145,6 +145,7 @@ height: 80px; } + {% block style %}{% endblock %} @@ -176,7 +177,6 @@ - {% block script %}{% endblock %} diff --git a/oshipka/webapp/static/js/oshipka.js b/oshipka/webapp/static/js/oshipka.js index 59d8342..82a556f 100644 --- a/oshipka/webapp/static/js/oshipka.js +++ b/oshipka/webapp/static/js/oshipka.js @@ -12,4 +12,8 @@ function start_async_task(task_name, data, ondata) { } } }) -} \ No newline at end of file +} + +$("select").chosen({ + inherit_select_classes: true, +}); \ No newline at end of file diff --git a/oshipka/webapp/views.py b/oshipka/webapp/views.py index 551303a..6d71576 100644 --- a/oshipka/webapp/views.py +++ b/oshipka/webapp/views.py @@ -6,6 +6,7 @@ from flask import flash, render_template, redirect, request, url_for, jsonify from oshipka.persistance import db from oshipka.util.strings import camel_case_to_snake_case +MODEL_VIEWS = dict() def default_get_args_func(view_context): view_context.serialized_args = request.args @@ -83,6 +84,7 @@ def default_render_func(vc): vc.template_ctx['instance'] = vc.instances[0] vc.template_ctx['instances'] = vc.instances vc.template_ctx['model_view'] = vc.model_view + vc.template_ctx['model_views'] = MODEL_VIEWS return render_template(vc.template, **vc.template_ctx) @@ -182,6 +184,8 @@ class ModelView(object): self.model_name = camel_case_to_snake_case(model.__name__) self.model_name_pl = p.plural(self.model_name) + MODEL_VIEWS[self.model_name] = self + def _register_rule(self, url_args, **kwargs): url = url_args.pop('rule') api_url = '/api{}'.format(url) diff --git a/vm_gen/templates/html/_create.html b/vm_gen/templates/html/_create.html index 9df14f0..9ba7a07 100644 --- a/vm_gen/templates/html/_create.html +++ b/vm_gen/templates/html/_create.html @@ -3,9 +3,13 @@ [%- for column in columns %] : [%- if column.type in ['relationship'] %] - + [%- else %] [[ column.name ]]: {{ instance.[[ column.name ]] }} + [%- else %] +
  • [[ column.name ]]: {{ instance.[[ column.name ]] }}
  • + [%- endif %] +[%- endfor %] \ No newline at end of file diff --git a/vm_gen/templates/html/_update.html b/vm_gen/templates/html/_update.html index d167665..f1cfe38 100644 --- a/vm_gen/templates/html/_update.html +++ b/vm_gen/templates/html/_update.html @@ -2,10 +2,20 @@ [%- for column in columns %] : + [%- if column.type in ['relationship'] %] + + [%- else %] + [%- endif %] [%- endfor %] \ No newline at end of file diff --git a/vm_gen/templates/routes_py b/vm_gen/templates/routes_py index e8bddcd..06609f3 100644 --- a/vm_gen/templates/routes_py +++ b/vm_gen/templates/routes_py @@ -9,9 +9,10 @@ from oshipka.webapp.views import ModelView from webapp.models import [[ name ]] from webapp.routes.[[ name|camel_to_snake ]]_hooks import * -ModelView(app, [[name]]).register_get(view_context=get_view_context) -ModelView(app, [[name]]).register_list(view_context=list_view_context) -ModelView(app, [[name]]).register_search(view_context=search_view_context) -ModelView(app, [[name]]).register_create(view_context=create_view_context) -ModelView(app, [[name]]).register_update(view_context=update_view_context) -ModelView(app, [[name]]).register_delete(view_context=delete_view_context) +[[ name|camel_to_snake ]] = ModelView(app, [[name]]) +[[ name|camel_to_snake ]].register_get(view_context=get_view_context) +[[ name|camel_to_snake ]].register_list(view_context=list_view_context) +[[ name|camel_to_snake ]].register_search(view_context=search_view_context) +[[ name|camel_to_snake ]].register_create(view_context=create_view_context) +[[ name|camel_to_snake ]].register_update(view_context=update_view_context) +[[ name|camel_to_snake ]].register_delete(view_context=delete_view_context) diff --git a/vm_gen/vm_gen.py b/vm_gen/vm_gen.py index d60efd4..cefd6c1 100644 --- a/vm_gen/vm_gen.py +++ b/vm_gen/vm_gen.py @@ -78,14 +78,17 @@ def process_routes(view_model): def process_html_templates(view_model): _model_name_snake = camel_case_to_snake_case(view_model.get('name')) - model_dir = os.path.join(HTML_TEMPLATES_PATH, _model_name_snake) - if not os.path.exists(model_dir): - os.makedirs(model_dir) + templates_dir = os.path.join(HTML_TEMPLATES_PATH, _model_name_snake) + if not os.path.exists(templates_dir): + os.makedirs(templates_dir) for filename in os.listdir(os.path.join(VM_TEMPLATES_PATH, "html")): + filepath = os.path.join(templates_dir, filename) + if not filename.startswith("_") and os.path.exists(filepath): + continue template = env.get_template(os.path.join('html', filename)) rv = template.render(**view_model) - with open(os.path.join(model_dir, filename), 'w+') as f: + with open(filepath, 'w+') as f: f.write(rv)