relationship create update

This commit is contained in:
Daniel Tsvetkov 2020-06-07 13:13:14 +02:00
parent cf4ffc861a
commit db895c86f8
8 changed files with 48 additions and 15 deletions

View File

@ -145,6 +145,7 @@
height: 80px; height: 80px;
} }
</style> </style>
<link rel="stylesheet" type="text/css" href="{{ url_for('oshipka_bp.static', filename='css/chosen.css') }}">
{% block style %}{% endblock %} {% block style %}{% endblock %}
</head> </head>
<body> <body>
@ -176,7 +177,6 @@
</main> </main>
<script src="{{ url_for('oshipka_bp.static', filename='js/jquery.js') }}"></script> <script src="{{ url_for('oshipka_bp.static', filename='js/jquery.js') }}"></script>
<script src="{{ url_for('oshipka_bp.static', filename='js/chosen.jquery.js') }}"></script> <script src="{{ url_for('oshipka_bp.static', filename='js/chosen.jquery.js') }}"></script>
<script src="{{ url_for('oshipka_bp.static', filename='js/lightbox.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 %}{% endblock %} {% block script %}{% endblock %}
</body> </body>

View File

@ -13,3 +13,7 @@ function start_async_task(task_name, data, ondata) {
} }
}) })
} }
$("select").chosen({
inherit_select_classes: true,
});

View File

@ -6,6 +6,7 @@ from flask import flash, render_template, redirect, request, url_for, jsonify
from oshipka.persistance import db from oshipka.persistance import db
from oshipka.util.strings import camel_case_to_snake_case from oshipka.util.strings import camel_case_to_snake_case
MODEL_VIEWS = dict()
def default_get_args_func(view_context): def default_get_args_func(view_context):
view_context.serialized_args = request.args 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['instance'] = vc.instances[0]
vc.template_ctx['instances'] = vc.instances vc.template_ctx['instances'] = vc.instances
vc.template_ctx['model_view'] = vc.model_view vc.template_ctx['model_view'] = vc.model_view
vc.template_ctx['model_views'] = MODEL_VIEWS
return render_template(vc.template, **vc.template_ctx) 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 = camel_case_to_snake_case(model.__name__)
self.model_name_pl = p.plural(self.model_name) self.model_name_pl = p.plural(self.model_name)
MODEL_VIEWS[self.model_name] = self
def _register_rule(self, url_args, **kwargs): def _register_rule(self, url_args, **kwargs):
url = url_args.pop('rule') url = url_args.pop('rule')
api_url = '/api{}'.format(url) api_url = '/api{}'.format(url)

View File

@ -3,9 +3,13 @@
[%- for column in columns %] [%- for column in columns %]
<label for="input-[[ name|camel_to_snake ]]-[[ column.name ]]">[[ column.name ]]</label>: <label for="input-[[ name|camel_to_snake ]]-[[ column.name ]]">[[ column.name ]]</label>:
[%- if column.type in ['relationship'] %] [%- if column.type in ['relationship'] %]
<input id="input-[[ name|camel_to_snake ]]-[[ column.name ]]" <select id="input-[[ name|camel_to_snake ]]-[[ column.name ]]"
type="number" name="[[ column.name ]]_id" autocomplete="off" name="[[ column.name ]]_id">
/> <option selected="selected">Choose...</option>
{%- for sub_instance in model_views.[[ column.name ]].model.query.all() %}
<option value="{{ sub_instance.id }}">{{ sub_instance.name }}</option>
{%- endfor %}
</select>
[%- else %] [%- else %]
<input id="input-[[ name|camel_to_snake ]]-[[ column.name ]]" <input id="input-[[ name|camel_to_snake ]]-[[ column.name ]]"
type="text" name="[[ column.name ]]" autocomplete="off" type="text" name="[[ column.name ]]" autocomplete="off"

View File

@ -0,0 +1,7 @@
[%- for column in columns %]
[%- if column.type in ['relationship'] %]
<li><strong>[[ column.name ]]</strong>: {{ instance.[[ column.name ]] }}</li>
[%- else %]
<li><strong>[[ column.name ]]</strong>: {{ instance.[[ column.name ]] }}</li>
[%- endif %]
[%- endfor %]

View File

@ -2,10 +2,20 @@
<input type="hidden" name="_next" value="{{ url_for('get_[[ name|camel_to_snake ]]', uuid=instance.id) }}"/> <input type="hidden" name="_next" value="{{ url_for('get_[[ name|camel_to_snake ]]', uuid=instance.id) }}"/>
[%- for column in columns %] [%- for column in columns %]
<label for="input-[[ name|camel_to_snake ]]-[[ column.name ]]">[[ column.name ]]</label>: <label for="input-[[ name|camel_to_snake ]]-[[ column.name ]]">[[ column.name ]]</label>:
[%- if column.type in ['relationship'] %]
<select id="input-[[ name|camel_to_snake ]]-[[ column.name ]]"
name="[[ column.name ]]_id">
{%- for sub_instance in model_views.[[ column.name ]].model.query.all() %}
<option value="{{ sub_instance.id }}"
{% if instance.[[ column.name ]]_id == sub_instance.id %}selected="selected"{% endif %}>{{ sub_instance.name }}</option>
{%- endfor %}
</select>
[%- else %]
<input id="input-[[ name|camel_to_snake ]]-[[ column.name ]]" <input id="input-[[ name|camel_to_snake ]]-[[ column.name ]]"
value="{{ instance.[[ column.name ]] }}" value="{{ instance.[[ column.name ]] }}"
type="text" name="[[ column.name ]]" autocomplete="off" type="text" name="[[ column.name ]]" autocomplete="off"
/> />
[%- endif %]
[%- endfor %] [%- endfor %]
<input type="submit"> <input type="submit">
</form> </form>

View File

@ -9,9 +9,10 @@ from oshipka.webapp.views import ModelView
from webapp.models import [[ name ]] from webapp.models import [[ name ]]
from webapp.routes.[[ name|camel_to_snake ]]_hooks import * from webapp.routes.[[ name|camel_to_snake ]]_hooks import *
ModelView(app, [[name]]).register_get(view_context=get_view_context) [[ name|camel_to_snake ]] = ModelView(app, [[name]])
ModelView(app, [[name]]).register_list(view_context=list_view_context) [[ name|camel_to_snake ]].register_get(view_context=get_view_context)
ModelView(app, [[name]]).register_search(view_context=search_view_context) [[ name|camel_to_snake ]].register_list(view_context=list_view_context)
ModelView(app, [[name]]).register_create(view_context=create_view_context) [[ name|camel_to_snake ]].register_search(view_context=search_view_context)
ModelView(app, [[name]]).register_update(view_context=update_view_context) [[ name|camel_to_snake ]].register_create(view_context=create_view_context)
ModelView(app, [[name]]).register_delete(view_context=delete_view_context) [[ name|camel_to_snake ]].register_update(view_context=update_view_context)
[[ name|camel_to_snake ]].register_delete(view_context=delete_view_context)

View File

@ -78,14 +78,17 @@ def process_routes(view_model):
def process_html_templates(view_model): def process_html_templates(view_model):
_model_name_snake = camel_case_to_snake_case(view_model.get('name')) _model_name_snake = camel_case_to_snake_case(view_model.get('name'))
model_dir = os.path.join(HTML_TEMPLATES_PATH, _model_name_snake) templates_dir = os.path.join(HTML_TEMPLATES_PATH, _model_name_snake)
if not os.path.exists(model_dir): if not os.path.exists(templates_dir):
os.makedirs(model_dir) os.makedirs(templates_dir)
for filename in os.listdir(os.path.join(VM_TEMPLATES_PATH, "html")): 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)) template = env.get_template(os.path.join('html', filename))
rv = template.render(**view_model) 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) f.write(rv)