diff --git a/oshipka/webapp/views.py b/oshipka/webapp/views.py index 1c9089c..6d0d74e 100644 --- a/oshipka/webapp/views.py +++ b/oshipka/webapp/views.py @@ -1,10 +1,13 @@ import importlib +import json +from collections import defaultdict from copy import copy from functools import wraps import inflect from flask import flash, render_template, redirect, request, url_for, jsonify from flask_security import login_required, roles_required +from sqlalchemy_filters import apply_filters from oshipka.persistance import db, filter_m_n, update_m_ns from oshipka.util.strings import camel_case_to_snake_case @@ -53,8 +56,10 @@ def default_get_func(vc): model = vc.model_view.model uuid = vc.url_args.get('uuid') if uuid and vc.url_args.get('uuid').isdigit(): + vc.instance = model.query.filter_by(id=uuid).first() vc.instances = model.query.filter_by(id=uuid).all() else: + vc.instance = model.query.filter_by(uuid=uuid).first() vc.instances = model.query.filter_by(uuid=uuid).all() if not vc.instances: flash("No {}:{}".format(vc.model_view.model_name, uuid)) @@ -68,10 +73,17 @@ def default_list_func(vc): vc.instances = vc.model_view.model.query.all() +def get_filters(serialized_args): + return json.loads(serialized_args.get("_filters", "[]")) + + def default_search_func(vc): q = vc.serialized_args.get('q') if hasattr(vc.model_view.model, 'search_query'): - vc.instances = vc.model_view.model.search_query("{q}".format(q=q)).all() + query = vc.model_view.model.search_query("*{q}*".format(q=q)) + filters = get_filters(vc.serialized_args) + filtered_query = apply_filters(query, filters) + vc.instances = filtered_query.all() def default_update_func(vc): @@ -193,6 +205,7 @@ def create_view(model_view, view_context_kwargs, is_login_required=False, the_ro return view_context.redirect_func(view_context) return return_json_or_template(view_context) + if is_login_required: if the_roles_required: inner = roles_required(*the_roles_required)(inner) diff --git a/requirements.txt b/requirements.txt index f61728f..8d0e860 100644 --- a/requirements.txt +++ b/requirements.txt @@ -31,6 +31,7 @@ pytz==2019.3 pyyaml==5.3.1 six==1.14.0 speaklater==1.3 +sqlalchemy-filters==0.12.0 SQLAlchemy==1.3.15 SQLAlchemy-Utils==0.36.3 watchdog==0.10.2 diff --git a/vm_gen/templates/_model_py b/vm_gen/templates/_model_py index 41b5806..80ff757 100644 --- a/vm_gen/templates/_model_py +++ b/vm_gen/templates/_model_py @@ -20,3 +20,14 @@ class [[ name ]](db.Model, ModelController[% for inherit in interits %], [[ inhe [[ extra_code ]] [%- endif %] + + [%- if display %] + def __repr__(self): + [%- if display.tertiary %] + return "{} ({} - {})".format(self.[[ display.primary ]], self.[[ display.secondary ]], self.[[ display.tertiary]]) + [%- elif display.secondary %] + return "{} ({})".format(self.[[ display.primary ]], self.[[ display.secondary ]]) + [%- else %] + return "{}".format(self.[[ display.primary ]]) + [%- endif %] + [%- endif %] diff --git a/vm_gen/templates/html/_action_delete.html b/vm_gen/templates/html/_action_delete.html new file mode 100644 index 0000000..7592e8c --- /dev/null +++ b/vm_gen/templates/html/_action_delete.html @@ -0,0 +1 @@ +x \ No newline at end of file diff --git a/vm_gen/templates/html/_action_edit.html b/vm_gen/templates/html/_action_edit.html new file mode 100644 index 0000000..65f945f --- /dev/null +++ b/vm_gen/templates/html/_action_edit.html @@ -0,0 +1 @@ +e \ No newline at end of file diff --git a/vm_gen/templates/html/_actions.html b/vm_gen/templates/html/_actions.html new file mode 100644 index 0000000..bcf77ea --- /dev/null +++ b/vm_gen/templates/html/_actions.html @@ -0,0 +1,4 @@ +[ +{% include "[[ name|camel_to_snake ]]/_action_edit.html" %} | +{% include "[[ name|camel_to_snake ]]/_action_delete.html" %} +] \ No newline at end of file diff --git a/vm_gen/templates/html/_list.html b/vm_gen/templates/html/_list.html index e3bb2e3..5dd71eb 100644 --- a/vm_gen/templates/html/_list.html +++ b/vm_gen/templates/html/_list.html @@ -1,11 +1,3 @@ {% for instance in instances %} -
  • - - {% include "[[ name|camel_to_snake ]]/_title.html" %} - | - [ - e | - x - ] -
  • +{% include "[[ name|camel_to_snake ]]/_list_item.html" %} {% endfor %} \ No newline at end of file diff --git a/vm_gen/templates/html/_list_item.html b/vm_gen/templates/html/_list_item.html new file mode 100644 index 0000000..738a70c --- /dev/null +++ b/vm_gen/templates/html/_list_item.html @@ -0,0 +1,5 @@ +
  • + + {% include "[[ name|camel_to_snake ]]/_title.html" %} + {% include "[[ name|camel_to_snake ]]/_actions.html" %} +
  • \ No newline at end of file diff --git a/vm_gen/templates/html/_title.html b/vm_gen/templates/html/_title.html index 16b4b6e..26e7f26 100644 --- a/vm_gen/templates/html/_title.html +++ b/vm_gen/templates/html/_title.html @@ -5,6 +5,9 @@ [%- if display.secondary %] - {{ instance.[[ display.secondary ]] }} [%- endif %] + [%- if display.tertiary %] + - {{ instance.[[ display.tertiary ]] }} + [%- endif %] [%- else %] {{ _("[[ name ]]") }} {{ instance.id }} [%- endif %] \ No newline at end of file diff --git a/vm_gen/vm_gen.py b/vm_gen/vm_gen.py index c13e9f9..0231181 100644 --- a/vm_gen/vm_gen.py +++ b/vm_gen/vm_gen.py @@ -78,7 +78,7 @@ def enrich_view_model(view_model): _column_type = 'db.UnicodeText' elif column_type in ['number', 'int', 'integer', ]: _column_type = 'db.Integer' - elif column_type in ['bool', ] or column_name.startswith('is_'): + elif column_type in ['bool', 'boolean', ] or column_name.startswith('is_'): _column_type = 'LiberalBoolean' elif column_type in ['datetime', ] or column_name.endswith('_dt'): _column_type = 'db.UnicodeText'