Skip to content

Commit

Permalink
Make domain a single choice field
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchdawson1982 committed Feb 22, 2024
1 parent a46b1c6 commit 389a609
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 28 deletions.
11 changes: 7 additions & 4 deletions home/forms/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class SearchForm(forms.Form):
required=False,
widget=forms.TextInput(attrs={"class": "govuk-input search-input"}),
)
domains = forms.MultipleChoiceField(
domain = forms.ChoiceField(
choices=get_domain_choices,
required=False,
widget=forms.Select(attrs={"form": "searchform", "class": "govuk-select"}),
Expand Down Expand Up @@ -168,7 +168,10 @@ def encode_without_filter(self, filter_to_remove):
"""Preformat hrefs to drop individual filters"""
# Deepcopy the cleaned data dict to avoid modifying it inplace
query_params = deepcopy(self.cleaned_data)
query_params["domains"].remove(filter_to_remove)
if len(query_params["domains"]) == 0:
query_params.pop("domains")
domain = query_params.get("domain")
classifications = query_params.get("classifications")
if domain == filter_to_remove:
query_params.pop("domain")
elif filter_to_remove in classifications:
query_params["classifications"].remove(filter_to_remove)
return f"?{urlencode(query_params, doseq=True)}"
38 changes: 25 additions & 13 deletions home/service/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
from .base import GenericService


def domains_with_their_subdomains(domains: list[str]) -> list[str]:
def domains_with_their_subdomains(domain: str) -> list[str]:
"""
When assets are tagged to subdomains, they are not included in search results if
we filter by domain alone. We need to include all possible subdomains in the filter.
"""
return [
subdomain
for domain in domains
for (subdomain, _) in get_subdomain_choices(domain)
] + domains
subdomains = get_subdomain_choices(domain)
return [domain, *subdomains] if subdomains else subdomains


class SearchService(GenericService):
Expand All @@ -37,8 +34,13 @@ def _get_search_results(self, page: str, items_per_page: int):

query = form_data.get("query", "")
sort = form_data.get("sort", "relevance")
domains = domains_with_their_subdomains(form_data.get("domains", []))
filter_value = [MultiSelectFilter("domains", domains)] if domains else []
domain = form_data.get("domain", "")
domains_and_subdomains = domains_with_their_subdomains(domain)
filter_value = (
[MultiSelectFilter("domains", domains_and_subdomains)]
if domains_and_subdomains
else []
)

classifications = form_data.get("classifications", [])
where_to_access = form_data.get("where_to_access", [])
Expand Down Expand Up @@ -79,13 +81,23 @@ def _get_context(self) -> dict[str, Any]:
page_title = "Search - Data catalogue"

if self.form.is_bound:
label_clear_href = {
filter.split(":")[-1]: self.form.encode_without_filter(filter)
for filter in self.form.cleaned_data.get("domains", [])
}
domain = self.form.cleaned_data.get("domain", "")
classifications = self.form.cleaned_data.get("classifications", [])
label_clear_href = {}
if domain:
label_clear_href["domain"] = {
domain.split(":")[-1]: (self.form.encode_without_filter(domain))
}
if classifications:
classifications_clear_href = {}
for classification in classifications:
classifications_clear_href[
classification.split("=")[1]
] = self.form.encode_without_filter(classification)
label_clear_href["classifications"] = classifications_clear_href
else:
label_clear_href = None
# print(f"label clear ref: {label_clear_href}")
print(f"label clear ref: {label_clear_href}")
context = {
"form": self.form,
"results": self.results.page_results,
Expand Down
15 changes: 15 additions & 0 deletions home/templatetags/clear_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django import template

register = template.Library()


@register.filter
def get_item(dictionary, key):
print(key)
print(dictionary)
return dictionary.get(key)


@register.filter
def get_items(dictionary, key):
return dictionary.get(key).items()
1 change: 0 additions & 1 deletion home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def details_view(request, id):


def search_view(request, page: str = "1"):
# print(request.GET)
new_search = request.GET.get("new", "")
if new_search:
form = SearchForm()
Expand Down
27 changes: 17 additions & 10 deletions templates/partial/filter.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load future %}
{% load clear_filter %}
<div class="govuk-grid-column-one-third">
<div class="moj-filter">
<div class="moj-filter__header">
Expand All @@ -18,18 +19,29 @@ <h2 class="govuk-heading-m">Selected filters</h2>
<div class="moj-filter__heading-action">
<p>
<a class="govuk-link govuk-link--no-visited-state"
href="{% url 'home:search' %}{% query_string domains=None subdomains=None classifications=None availabilities=None %}"
href="{% url 'home:search' %}{% query_string domain=None classifications=None where_to_access=None %}"
id="clear_filter">
Clear filter
</a>
</p>
</div>
</div>
{% if form.domains.value %}
{% if label_clear_href|get_item:"domain"|length > 0 %}
<h3 class="govuk-heading-s govuk-!-margin-bottom-0">Domain</h3>
<ul class="moj-filter-tags">
<input type="hidden" name="clear_label" value="">
{% for label, href in label_clear_href.items %}
{% for label, href in label_clear_href|get_items:"domain" %}
<li><a class="moj-filter__tag govuk-link" href="{{ href }}">
<span data-test-id="selected-domain-label">{{label}}</span> <span class="govuk-visually-hidden">Remove this filter</span>
</a></li>
{% endfor %}
</ul>
{% endif %}
{% if label_clear_href|get_item:"classifications"|length > 0 %}
<h3 class="govuk-heading-s govuk-!-margin-bottom-0">Security Classifications</h3>
<ul class="moj-filter-tags">
<input type="hidden" name="clear_label" value="">
{% for label, href in label_clear_href|get_items:"classifications" %}
<li><a class="moj-filter__tag govuk-link" href="{{ href }}">
<span data-test-id="selected-domain-label">{{label}}</span> <span class="govuk-visually-hidden">Remove this filter</span>
</a></li>
Expand All @@ -45,12 +57,7 @@ <h3 class="govuk-heading-s govuk-!-margin-bottom-0">Domain</h3>
<fieldset class="govuk-fieldset">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--m">Domain</legend>
<div class="govuk-checkboxes govuk-checkboxes--small" data-module="govuk-checkboxes">
{% for domain_option in form.domains %}
<div class="govuk-checkboxes__item">
{{ domain_option.tag}}
<label class="govuk-label govuk-checkboxes__label" for="{{ domain_option.id_for_label }}">{{domain_option.choice_label}}</label>
</div>
{% endfor %}
{{form.domain}}
</div>
</fieldset>
</div>
Expand All @@ -74,7 +81,7 @@ <h3 class="govuk-heading-s govuk-!-margin-bottom-0">Domain</h3>
{% for access_option in form.where_to_access %}
<div class="govuk-checkboxes__item">
{{ access_option.tag}}
<label class="govuk-label govuk-checkboxes__label" for="{{ availability_option.id_for_label }}">{{access_option.choice_label}}</label>
<label class="govuk-label govuk-checkboxes__label" for="{{ access_option.id_for_label }}">{{access_option.choice_label}}</label>
</div>
{% endfor %}
</div>
Expand Down

0 comments on commit 389a609

Please sign in to comment.