Skip to content

Commit

Permalink
task/WP-7: Admin submissions filtering and date-sorting (#151)
Browse files Browse the repository at this point in the history
* Added url patterns for filter and sort on this page

* Added filter and sort data modification to page's view

* Added filter and sort dropdowns and coinciding scripts to handle changes on those elements to reload page with selected options implemented

* Add date sort to view and grab selected filter + sort from url in view
  • Loading branch information
edmondsgarrett committed May 8, 2023
1 parent cac9083 commit bfb8d67
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<link rel="stylesheet" href="{% static 'submissions/css/table.css' %}">
<link rel="stylesheet" href="{% static 'submissions/css/modal.css' %}">
<link rel="stylesheet" href="{% static 'admin_submissions/css/admin_table.css' %}">
<link rel="stylesheet" href="{% static 'admin_regis_table/css/table.css' %}">
<link rel="stylesheet" href="{% static 'admin_regis_table/css/core-styles.c-form.css' %}">

<div class="container">
Expand All @@ -15,6 +16,27 @@ <h1>View Submissions</h1>
<hr />
<p style="margin-bottom: 30px">All completed submissions by organizations</p>
<hr />
<div class="filter-container">
<div class="filter-content">
<span><b>Filter by Status: </b></span>
<select id="statusFilter" class="status-filter" onchange="filterTableByStatus()">
{% for option in filter_options %}
<option class="dropdown-text" {% if option == selected_filter %}selected{% endif %}>{{ option }}</option>
{% endfor %}
</select>
<span><b>Sort by: </b></span>
<select id="dateSort" class="status-filter" onchange="sortByDate()">
<option class="dropdown-text" {% if not selected_sort %}selected{% else %}hidden{% endif %} disabled> </option>
{% for option, display_text in sort_options.items %}
<option class="dropdown-text" value={{option}} {% if option == selected_sort %}selected{% endif %}>{{ display_text }}</option>
{% endfor %}
</select>
{% if selected_filter or selected_sort %}
<button onclick="clearSelections()">Clear Options</button>
{% endif %}
</div>
</div>

<table id="submissionTable" class="submission-table">
<thead>
<tr>
Expand Down Expand Up @@ -43,4 +65,44 @@ <h1>View Submissions</h1>

{% include 'paginator.html' %}
</div>
<script>
function filterTableByStatus() {
var filterDropdown, filterValue, url_params, url, xhr;
filterDropdown = document.getElementById("statusFilter");
filterValue = filterDropdown.value;
url_params = `?filter=${filterValue}`;
{% if selected_sort %}
url_params = `?filter=${filterValue}&sort={{selected_sort}}`;
{% endif %}
url = `/administration/list-submissions/${url_params}`;
xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
window.location.href = url;
window.location.load();
}
function sortByDate() {
var sortDropdown, sortValue, url_params, url, xhr;
sortDropdown = document.getElementById('dateSort');
sortValue = sortDropdown.value;
url_params = `?sort=${sortValue}`;
{% if selected_filter %}
url_params = `?filter={{selected_filter}}&sort=${sortValue}`;
{% endif %}
url = `/administration/list-submissions/${url_params}`;
xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
window.location.href = url;
window.location.load();
}
function clearSelections() {
var xhr;
xhr = new XMLHttpRequest();
xhr.open('GET', '/administration/list-submissions/')
xhr.send()
window.location.href = '/administration/list-submissions/';
window.location.load();
}
</script>
{% endblock %}
3 changes: 3 additions & 0 deletions apcd-cms/src/apps/admin_submissions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
app_name = 'administration'
urlpatterns = [
path('list-submissions/', AdminSubmissionsTable.as_view(), name="admin_submissions"),
path(r'list-submissions/?filter=(?P<filter>)/', AdminSubmissionsTable.as_view(), name="admin_submissions"),
path(r'list-submissions/?sort=(?P<sort>)/', AdminSubmissionsTable.as_view(), name="admin_submissions"),
path(r'list-submissions/?filter=(?P<filter>)&sort=(?P<sort>)/', AdminSubmissionsTable.as_view(), name="admin_submissions"),
]
26 changes: 24 additions & 2 deletions apcd-cms/src/apps/admin_submissions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.views.generic.base import TemplateView
from apps.utils.apcd_database import get_all_submissions_and_logs
from apps.utils.apcd_groups import is_apcd_admin
from apps.utils.utils import title_case
from apps.utils.utils import title_case, table_filter
from apps.components.paginator.paginator import paginator
import logging
from dateutil import parser
Expand All @@ -24,11 +24,27 @@ def get_context_data(self, *args, **kwargs):

submission_content = get_all_submissions_and_logs()

filter = self.request.GET.get('filter')
dateSort = self.request.GET.get('sort')

def getDate(row):
date = row['received_timestamp']
return parser.parse(date) if date is not None else parser.parse('1-1-3005') # put 'None' date entries all together at top/bottom depending on direction of sort

if dateSort is not None:
context['selected_sort'] = dateSort
submission_content = sorted(submission_content, key=lambda row:getDate(row), reverse=(dateSort == 'newDate'))

try:
page_num = int(self.request.GET.get('page'))
except:
page_num = 1

context['selected_filter'] = None
if filter is not None and filter != 'All':
context['selected_filter'] = filter
submission_content = table_filter(filter, submission_content, 'status')

limit = 50
offset = limit * (page_num - 1)

Expand All @@ -45,7 +61,13 @@ def get_context_data(self, *args, **kwargs):
} for t in (s['view_modal_content'] or [])]

context['header'] = ['Received', 'Organization', 'File Name', ' ', 'Outcome', 'Status', 'Last Updated', 'Actions']
context['filter_options'] = ['All', 'In Process', 'Complete']
context['sort_options'] = {'newDate': 'Newest Received', 'oldDate': 'Oldest Received'}

context.update(paginator(self.request, submission_content))
queryStr = '?'
if len(self.request.META['QUERY_STRING']) > 0:
queryStr = queryStr + self.request.META['QUERY_STRING'].replace(f'page={page_num}', '') + ('&' if self.request.GET.get('page') is None else '')
context['query_str'] = queryStr
context.update(paginator(self.request, submission_content, limit))
context['pagination_url_namespaces'] = 'admin_submission:admin_submissions'
return context

0 comments on commit bfb8d67

Please sign in to comment.