pagination

This commit is contained in:
Daniel Tsvetkov 2021-05-08 16:56:42 +02:00
parent 9d85c95584
commit e7a0d9f6bd
5 changed files with 20 additions and 5 deletions

View File

@ -331,6 +331,7 @@ SENSITIVE_PREFIX = "__SENSITIVE__."
def populate_static(app):
print("populating...")
static_media_dir = os.path.join(STATIC_DATA_DIR, "media")
if os.path.exists(static_media_dir):
dir_util.copy_tree(static_media_dir, MEDIA_DIR)
@ -368,6 +369,7 @@ def populate_static(app):
instance = create_model(model, row)
db.session.add(instance)
db.session.commit()
print("Finished populating")
def filter_m_n(serialized_args):

View File

@ -80,7 +80,13 @@ def default_list_args_get_func(vc):
def default_list_func(vc):
vc.instances = vc.model_view.model.query.all()
per_page = request.args.get('per_page')
order_by = request.args.get('order_by')
order_by_dir = request.args.get('order_by_dir')
# .order_by(getattr(vc.model_view.model, order_by).desc())
pagination = vc.model_view.model.query.paginate(per_page=per_page, error_out=False)
vc.template_ctx['pagination'] = pagination
vc.instances = pagination.items
def get_filters(serialized_args):
@ -93,7 +99,10 @@ def default_search_func(vc):
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()
per_page = request.args.get('per_page')
pagination = filtered_query.paginate(per_page=per_page, error_out=False)
vc.template_ctx['pagination'] = pagination
vc.instances = pagination.items
def default_update_func(vc):

View File

@ -1,3 +1,6 @@
{% for instance in instances %}
{% include "[[ name|camel_to_snake ]]/_list_item.html" %}
{% endfor %}
{% endfor %}
<br>
{% import "_macros.html" as m %}
{{ m.render_pagination(pagination, 'list_[[ name|camel_to_snake ]]') }}

View File

@ -28,7 +28,7 @@
{% endif %}
[%- endfor %]
<td>
{% include "[[ name|camel_to_snake ]]/_action.html" %} |
{% include "[[ name|camel_to_snake ]]/_actions.html" %}
</td>
</tr>
{% endfor %}

View File

@ -2,8 +2,9 @@
{% block content %}
<h2>{{ _("[[ name|pluralize ]]") }}</h2>
{% include "[[ name|camel_to_snake ]]/_action_create.html" %} |
{% include "[[ name|camel_to_snake ]]/_action_table.html" %}
{% include "[[ name|camel_to_snake ]]/_action_search.html" %}
{% include "[[ name|camel_to_snake ]]/_action_create.html" %}
<br>
{% include "[[ name|camel_to_snake ]]/_list.html" %}
{% endblock %}