Skip to content

Commit

Permalink
fix: admin person query & record list view more results per page
Browse files Browse the repository at this point in the history
  • Loading branch information
moogoo78 committed May 5, 2024
1 parent a52b3bf commit 182c035
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
16 changes: 10 additions & 6 deletions app/blueprints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,17 +697,18 @@ def record_list():
subquery = base_stmt.subquery()
count_stmt = select(func.count()).select_from(subquery)
total = session.execute(count_stmt).scalar()
per_page = 50

# order & limit
stmt = stmt.order_by(desc(Record.id))
if current_page > 1:
stmt = stmt.offset((current_page-1) * 20)
stmt = stmt.limit(20)
stmt = stmt.offset((current_page-1) * per_page)
stmt = stmt.limit(per_page)

result = session.execute(stmt)
rows = result.all()
# print(stmt, '==', flush=True)
last_page = math.ceil(total / 20)
last_page = math.ceil(total / per_page)
pagination = {
'current_page': current_page,
'last_page': last_page,
Expand Down Expand Up @@ -999,6 +1000,7 @@ def __init__(self, register):
self.template = 'admin/list-view.html'

def dispatch_request(self):

# login_requried
if not current_user.is_authenticated:
return redirect('/login')
Expand All @@ -1016,7 +1018,7 @@ def dispatch_request(self):

#print(query, flush=True)
if list_filter := self.register.get('list_filter'):
if q := request.args.get('q'):
if q := request.args.get('q'):
many_or = or_()
for x in list_filter:
attr = getattr(self.register['model'], x)
Expand All @@ -1031,9 +1033,11 @@ def dispatch_request(self):
elif field := collection_filter.get('field'):
query = query.filter(field==int(collection_id))

items = query.all()

return render_template(self.template, items=items, register=self.register)
total = query.count()
items = query.limit(50).all()

return render_template(self.template, items=items, register=self.register, total=total)


class FormView(View):
Expand Down
3 changes: 2 additions & 1 deletion app/blueprints/admin_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@
'list_display': ('full_name', 'full_name_en', 'is_collector', 'is_identifier',),
'list_collection_filter': {
'related': Collection.people,
}
},
'list_filter': ('full_name', 'full_name_en'),
},
'taxon': {
'name': 'taxon',
Expand Down
13 changes: 13 additions & 0 deletions app/templates/admin/list-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
</ul>
</nav>

{% if register.query %}
<div>
<form method="get" action="{{ url_for('admin.'+register.name+'-list')}}">
<div class="uk-inline uk-width-large">
<button id="" type="submit" class="uk-form-icon uk-form-icon-flip" uk-icon="icon: search"></button>
<input type="search" name="q" class="search-input uk-input" autocapitalize="none" autocorrect="off" autocomplete="off" value="{%if request.args.q %}{{ request.args.q }}{% endif%}"/>
</div>
<div class="uk-text-meta">keyword: 館號、物種、採集者、採集號</div>
</form>
<p>Results: {{ "{:,}".format(total) }}</p>
</div>
{% endif %}

{% if register.list_collection_filter %}
{% include "admin/inc_collection_query_buttons.html" %}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/admin/record-list-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</div>


<div class="uk-overflow-auto uk-height-large">
<div class="uk-overflow-auto">
<table class="uk-table uk-table-striped uk-table-condensed uk-table-hover uk-table-divider uk-text-nowrap">
<thead>
<tr>
Expand Down

0 comments on commit 182c035

Please sign in to comment.