Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WV-861 add next page UI [TEAM REVIEW] #2821

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 27 additions & 2 deletions templates/politician/politician_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ <h1>Politicians</h1>
{% endif %}
<input type="text" name="politician_search" id="politician_search_id" value="{{ politician_search }}" />
<input id="politician_search_submit" type="submit" value="Search for Politician" />
<!-- Pagination Section -->
<form method="get" action="" class="pagination-form">
<div class="float-right">
{% if previous_page_url %}
<a href="{{ previous_page_url }}" onclick="displayLoadingBanner()">Previous page</a> |
{% endif %}
Page {{ current_page_number|add:1 }}
{% if next_page_url %}
| <a href="{{ next_page_url }}" onclick="displayLoadingBanner()">Next page</a>
{% endif %}
</div>
</form>
<!-- End of Pagination Section -->
&nbsp;&nbsp;
<label for="show_politicians_with_email_id">
<input type="checkbox" name="show_politicians_with_email" id="show_politicians_with_email_id" value="1"
Expand Down Expand Up @@ -142,7 +155,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 +261,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 }}" onclick="displayLoadingBanner()">Previous page</a> |
{% endif %}
Page {{ current_page_number|add:1 }}
{% if next_page_url %}
| <a href="{{ next_page_url }}" onclick="displayLoadingBanner()">Next page</a>
{% endif %}
</span>
{% endif %}

<p></p>
{% else %}
Expand All @@ -264,7 +289,7 @@ <h1>Politicians</h1>

function clearSearch() {
displayLoadingBanner();
$('#politician_search_id').value = '';
$('#politician_search_id').val('');
$('form[name="state_code_form"]').submit();
}

Expand Down