From e7a0d9f6bd1dd6cc2dc4ad80c2eb7383cdd574fe Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Sat, 8 May 2021 16:56:42 +0200 Subject: [PATCH] pagination --- oshipka/persistance/__init__.py | 2 ++ oshipka/webapp/views.py | 13 +++++++++++-- vm_gen/templates/html/_list.html | 5 ++++- vm_gen/templates/html/_table.html | 2 +- vm_gen/templates/html/list.html | 3 ++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/oshipka/persistance/__init__.py b/oshipka/persistance/__init__.py index 956570f..63b5191 100644 --- a/oshipka/persistance/__init__.py +++ b/oshipka/persistance/__init__.py @@ -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): diff --git a/oshipka/webapp/views.py b/oshipka/webapp/views.py index 206e381..a76da8b 100644 --- a/oshipka/webapp/views.py +++ b/oshipka/webapp/views.py @@ -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): diff --git a/vm_gen/templates/html/_list.html b/vm_gen/templates/html/_list.html index 5dd71eb..0df4d5e 100644 --- a/vm_gen/templates/html/_list.html +++ b/vm_gen/templates/html/_list.html @@ -1,3 +1,6 @@ {% for instance in instances %} {% include "[[ name|camel_to_snake ]]/_list_item.html" %} -{% endfor %} \ No newline at end of file +{% endfor %} +
+{% import "_macros.html" as m %} +{{ m.render_pagination(pagination, 'list_[[ name|camel_to_snake ]]') }} \ No newline at end of file diff --git a/vm_gen/templates/html/_table.html b/vm_gen/templates/html/_table.html index 78f06d6..de84dd8 100644 --- a/vm_gen/templates/html/_table.html +++ b/vm_gen/templates/html/_table.html @@ -28,7 +28,7 @@ {% endif %} [%- endfor %] - {% include "[[ name|camel_to_snake ]]/_action.html" %} | + {% include "[[ name|camel_to_snake ]]/_actions.html" %} {% endfor %} diff --git a/vm_gen/templates/html/list.html b/vm_gen/templates/html/list.html index b16bf6d..7ef3fc8 100644 --- a/vm_gen/templates/html/list.html +++ b/vm_gen/templates/html/list.html @@ -2,8 +2,9 @@ {% block content %}

{{ _("[[ name|pluralize ]]") }}

+ {% 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" %}
{% include "[[ name|camel_to_snake ]]/_list.html" %} {% endblock %} \ No newline at end of file