separate status, add filters

This commit is contained in:
Daniel Tsvetkov 2020-08-11 19:02:29 +02:00
parent 79b0556964
commit 0dbb20fdab
10 changed files with 42 additions and 11 deletions

View File

@ -1,10 +1,13 @@
import importlib import importlib
import json
from collections import defaultdict
from copy import copy from copy import copy
from functools import wraps from functools import wraps
import inflect import inflect
from flask import flash, render_template, redirect, request, url_for, jsonify from flask import flash, render_template, redirect, request, url_for, jsonify
from flask_security import login_required, roles_required 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.persistance import db, filter_m_n, update_m_ns
from oshipka.util.strings import camel_case_to_snake_case from oshipka.util.strings import camel_case_to_snake_case
@ -53,8 +56,10 @@ def default_get_func(vc):
model = vc.model_view.model model = vc.model_view.model
uuid = vc.url_args.get('uuid') uuid = vc.url_args.get('uuid')
if uuid and vc.url_args.get('uuid').isdigit(): 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() vc.instances = model.query.filter_by(id=uuid).all()
else: else:
vc.instance = model.query.filter_by(uuid=uuid).first()
vc.instances = model.query.filter_by(uuid=uuid).all() vc.instances = model.query.filter_by(uuid=uuid).all()
if not vc.instances: if not vc.instances:
flash("No {}:{}".format(vc.model_view.model_name, uuid)) 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() 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): def default_search_func(vc):
q = vc.serialized_args.get('q') q = vc.serialized_args.get('q')
if hasattr(vc.model_view.model, 'search_query'): 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): 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 view_context.redirect_func(view_context)
return return_json_or_template(view_context) 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)

View File

@ -31,6 +31,7 @@ pytz==2019.3
pyyaml==5.3.1 pyyaml==5.3.1
six==1.14.0 six==1.14.0
speaklater==1.3 speaklater==1.3
sqlalchemy-filters==0.12.0
SQLAlchemy==1.3.15 SQLAlchemy==1.3.15
SQLAlchemy-Utils==0.36.3 SQLAlchemy-Utils==0.36.3
watchdog==0.10.2 watchdog==0.10.2

View File

@ -20,3 +20,14 @@ class [[ name ]](db.Model, ModelController[% for inherit in interits %], [[ inhe
[[ extra_code ]] [[ extra_code ]]
[%- endif %] [%- 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 %]

View File

@ -0,0 +1 @@
<a href="{{ url_for('delete_[[ name|camel_to_snake ]]', uuid=instance.id, _next=request.path) }}">x</a>

View File

@ -0,0 +1 @@
<a href="{{ url_for('update_[[ name|camel_to_snake ]]', uuid=instance.id, _next=request.path) }}">e</a>

View File

@ -0,0 +1,4 @@
[
{% include "[[ name|camel_to_snake ]]/_action_edit.html" %} |
{% include "[[ name|camel_to_snake ]]/_action_delete.html" %}
]

View File

@ -1,11 +1,3 @@
{% for instance in instances %} {% for instance in instances %}
<li> {% include "[[ name|camel_to_snake ]]/_list_item.html" %}
<a href="{{ url_for('get_[[ name|camel_to_snake ]]', uuid=instance.id) }}">
{% include "[[ name|camel_to_snake ]]/_title.html" %}</a>
|
[
<a href="{{ url_for('update_[[ name|camel_to_snake ]]', uuid=instance.id, _next=request.path) }}">e</a> |
<a href="{{ url_for('delete_[[ name|camel_to_snake ]]', uuid=instance.id, _next=request.path) }}">x</a>
]
</li>
{% endfor %} {% endfor %}

View File

@ -0,0 +1,5 @@
<li>
<a href="{{ url_for('get_[[ name|camel_to_snake ]]', uuid=instance.id) }}">
{% include "[[ name|camel_to_snake ]]/_title.html" %}</a>
{% include "[[ name|camel_to_snake ]]/_actions.html" %}
</li>

View File

@ -5,6 +5,9 @@
[%- if display.secondary %] [%- if display.secondary %]
- {{ instance.[[ display.secondary ]] }} - {{ instance.[[ display.secondary ]] }}
[%- endif %] [%- endif %]
[%- if display.tertiary %]
- {{ instance.[[ display.tertiary ]] }}
[%- endif %]
[%- else %] [%- else %]
{{ _("[[ name ]]") }} {{ instance.id }} {{ _("[[ name ]]") }} {{ instance.id }}
[%- endif %] [%- endif %]

View File

@ -78,7 +78,7 @@ def enrich_view_model(view_model):
_column_type = 'db.UnicodeText' _column_type = 'db.UnicodeText'
elif column_type in ['number', 'int', 'integer', ]: elif column_type in ['number', 'int', 'integer', ]:
_column_type = 'db.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' _column_type = 'LiberalBoolean'
elif column_type in ['datetime', ] or column_name.endswith('_dt'): elif column_type in ['datetime', ] or column_name.endswith('_dt'):
_column_type = 'db.UnicodeText' _column_type = 'db.UnicodeText'