Skip to content

Commit

Permalink
CONCD-996 Implement filtering for Completed Campaigns Page (#2636)
Browse files Browse the repository at this point in the history
* CONCD-996 dropdowns for filtering

* CONCD-996 Retired Campaigns filter

* CONCD-996 click handler for Campaign Type dropdown

* CONCD-996 Research Center filter (WiP)

* CONCD-996 refactoring to allow all options to appear in dropdown, even when one is currently selected

* CONCD-996 fixed a bug where the correct option was not being shown as selected

* CONCD-996 updated unit test
  • Loading branch information
rasarkar authored Dec 11, 2024
1 parent e485c43 commit a05f697
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
48 changes: 45 additions & 3 deletions concordia/templates/transcriptions/campaign_list_small_blocks.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,30 @@ <h1>Completed Campaigns</h1>
<option value="grid"{% if request.GET.view != 'list' %} selected{% endif %}>Grid</option>
<option value="list"{% if request.GET.view == 'list' %} selected{% endif %}>List</option>
</select>
<a class="btn btn-primary" onclick="filterCampaigns();">Go</a>
<a class="btn btn-primary" onclick="toggleCampaignView();">Go</a>
</div>
<div class="col-5 d-flex align-items-center">
<label for="campaign-type" class="pe-1">Campaign Type</label>
<select id="campaign-type">
<option value="completed"{% if request.GET.type != 'retired' %} selected{% endif %}>Completed Campaigns</option>
<option value="retired"{% if request.GET.type == 'retired' %} selected{% endif %}>Retired Campaigns</option>
</select>
<a class="btn btn-primary" onclick="toggleCampaignType();" type="submit">Go</a>
</div>
{% if research_centers %}
<div class="col-5 d-flex align-items-center">
<label for="research-center" class="pe-1">Research Center</label>
<select id="research-center">
<option value="all"{% if 'research_center' not in request.GET %} selected{% endif %}>All Research Centers</option>
{% for research_center in research_centers %}
<option value="{{ research_center.pk }}"{% if request.GET.research_center|add:"0" == research_center.pk %} selected{% endif %}>
{{ research_center.title }}
</option>
{% endfor %}
</select>
<a class="btn btn-primary" onclick="toggleResearchCenter();" type="submit">Go</a>
</div>
{% endif %}
</div>
{% if request.GET.view == 'list' %}
<ul id="campaign-list" class="list-unstyled list-view">
Expand Down Expand Up @@ -83,9 +105,11 @@ <h1>Completed Campaigns</h1>

{% block body_scripts %}
<script>
var filterCampaigns = function(form) {
var toggleCampaignView = function(form) {
let url = new URL(window.location.href);
let viewValue = document.getElementById('view-options').value;
window.location.href = "?view=" + encodeURIComponent(viewValue);
url.searchParams.set("view", encodeURIComponent(viewValue));
window.location.href = url;
};
let showMoreButton = document.getElementById("show-more");
let campaignList = document.getElementById("campaign-list");
Expand All @@ -99,5 +123,23 @@ <h1>Completed Campaigns</h1>
event.preventDefault();
});
}

var toggleCampaignType = function(form) {
let url = new URL(window.location.href);
let typeValue = document.getElementById('campaign-type').value;
url.searchParams.set("type", encodeURIComponent(typeValue));
window.location.href = url;
}

var toggleResearchCenter = function(form) {
let url = new URL(window.location.href);
let researchCenter = document.getElementById('research-center').value;
if (researchCenter == "all") {
url.searchParams.delete("research_center");
} else {
url.searchParams.set("research_center", encodeURIComponent(researchCenter));
}
window.location.href = url;
}
</script>
{% endblock body_scripts %}
4 changes: 3 additions & 1 deletion concordia/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ def setUp(self):

def test_queryset(self):
view = CompletedCampaignListView()
view.request = RequestFactory().get("/campaigns/completed/")
queryset = view.get_queryset()
self.assertGreater(
view.queryset.first().completed_date, view.queryset.last().completed_date
queryset.first().completed_date, queryset.last().completed_date
)


Expand Down
29 changes: 23 additions & 6 deletions concordia/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
Guide,
Item,
Project,
ResearchCenter,
SimplePage,
SiteReport,
Tag,
Expand Down Expand Up @@ -910,12 +911,28 @@ def serialize_context(self, context):
class CompletedCampaignListView(APIListView):
template_name = "transcriptions/campaign_list_small_blocks.html"

queryset = (
Campaign.objects.published()
.listed()
.filter(status__in=[Campaign.Status.COMPLETED, Campaign.Status.RETIRED])
.order_by("-completed_date")
)
def _get_all_campaigns(self):
if self.request.GET.get("type", "completed") == "retired":
status = Campaign.Status.RETIRED
else:
status = Campaign.Status.COMPLETED
return Campaign.objects.published().listed().filter(status=status)

def get_queryset(self):
campaigns = self._get_all_campaigns()
research_center = self.request.GET.get("research_center", None)
if research_center is not None:
campaigns = campaigns.filter(research_centers=research_center)
return campaigns.order_by("-completed_date")

def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
data["research_centers"] = ResearchCenter.objects.filter(
campaign__in=self._get_all_campaigns()
).distinct()

return data

context_object_name = "campaigns"


Expand Down

0 comments on commit a05f697

Please sign in to comment.