Skip to content

Commit

Permalink
[WV-861]Add 'Next Page' UI on Politician Admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
itcreativeusa committed Dec 14, 2024
1 parent 7756464 commit 72fbb8a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
30 changes: 30 additions & 0 deletions politician/views_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ def politicians_import_from_master_server_view(request):

@login_required
def politician_list_view(request):
# Pagination parameters
page = int(request.GET.get('page', 0)) # Default to page 0
items_per_page = 25 # Number of items per page
# admin, analytics_admin, partner_organization, political_data_manager, political_data_viewer, verified_volunteer
authority_required = {'partner_organization', 'political_data_viewer', 'verified_volunteer'}
if not voter_has_authority(request, authority_required):
Expand Down Expand Up @@ -1166,7 +1169,34 @@ def politician_list_view(request):
show_related_candidates=show_related_candidates,
was_candidate_recently=was_candidate_recently,
)

# Pagination logic
total_count = politician_query.count()
items_per_page = 25
page = int(request.GET.get("page", 1)) - 1 # Page is 0-indexed
start_index = page * items_per_page
end_index = start_index + items_per_page

# Fetch only the items for the current page
politician_list = politician_query.order_by("politician_name")[start_index:end_index]

# Determine if there are previous/next pages
has_previous_page = page > 0
has_next_page = end_index < total_count

# Update URLs for previous and next pages
previous_page_url = f"?page={page}&state_code={state_code}&politician_search={politician_search}" if has_previous_page else None
next_page_url = f"?page={page + 2}&state_code={state_code}&politician_search={politician_search}" if has_next_page else None

template_values = {
"start_index": start_index,
'politician_list': politician_list,
'state_code': state_code,
'politician_search': politician_search,
'current_page_number': page,
'previous_page_url': previous_page_url,
'next_page_url': next_page_url,
'hide_pagination': total_count <= items_per_page,
'checkbox_url_variables': checkbox_url_variables,
'election_list': election_list,
'exclude_politician_analysis_done': exclude_politician_analysis_done,
Expand Down
26 changes: 25 additions & 1 deletion templates/politician/politician_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ <h1>Politicians</h1>
{% if exclude_politician_analysis_done %}checked{% endif %} /> Exclude Analyzed
</label>
</form>
<!-- Pagination Section -->
{% if not hide_pagination %}
<span class="float-right">
{% if previous_page_url %}
<a href="{{ previous_page_url }}">Previous page</a> |
{% endif %}
Page {{ current_page_number|add:1 }}
{% if next_page_url %}
| <a href="{{ next_page_url }}">Next page</a>
{% endif %}
</span>
{% endif %}

{% if politician_list %}
<table class="table">
Expand All @@ -142,7 +154,7 @@ <h1>Politicians</h1>
</thead>
{% for politician in politician_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ forloop.counter|add:start_index }}</td>
<td{% if politician.profile_image_background_color %}
style="padding-left: 0; padding-right: 0;"
{% endif %}>
Expand Down Expand Up @@ -248,6 +260,18 @@ <h1>Politicians</h1>
</tr>
{% endfor %}
</table>
<!-- Pagination Section -->
{% if not hide_pagination %}
<span class="float-right">
{% if previous_page_url %}
<a href="{{ previous_page_url }}">Previous page</a> |
{% endif %}
Page {{ current_page_number|add:1 }}
{% if next_page_url %}
| <a href="{{ next_page_url }}">Next page</a>
{% endif %}
</span>
{% endif %}

<p></p>
{% else %}
Expand Down

0 comments on commit 72fbb8a

Please sign in to comment.