-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.py
76 lines (63 loc) · 2.35 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from copy import deepcopy
from django import forms
from urllib.parse import urlencode
# from home.helper import get_domain_list
def get_domain_choices():
"""Make API call to obtain domain choices"""
# TODO: pull in the domains from the catalogue client
# facets = client.search_facets()
# domain_list = facets.options("domains")
return [
("urn:li:domain:HMCTS", "HMCTS"),
("urn:li:domain:HMPPS", "HMPPS"),
("urn:li:domain:OPG", "OPG"),
("urn:li:domain:HQ", "HQ"),
]
def get_sort_choices():
return [
("relevance", "Relevance"),
("ascending", "Ascending"),
("descending", "Descending"),
]
class SearchForm(forms.Form):
"""Django form to represent data product search page inputs"""
query = forms.CharField(
max_length=100,
strip=False,
required=False,
widget=forms.TextInput(attrs={"class": "govuk-input search-input"}),
)
domains = forms.MultipleChoiceField(
choices=get_domain_choices,
required=False,
widget=forms.CheckboxSelectMultiple(
attrs={"class": "govuk-checkboxes__input", "form": "searchform"}
),
)
sort = forms.ChoiceField(
choices=get_sort_choices,
widget=forms.RadioSelect(
attrs={
"class": "govuk-radios__input",
"form": "searchform",
"onchange": "document.getElementById('searchform').submit();",
}
),
required=False,
)
clear_filter = forms.BooleanField(initial=False, required=False)
clear_label = forms.BooleanField(initial=False, required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.initial["sort"] = "relevance"
def clean_query(self):
"""Example clean method to apply custom validation to input fields"""
return str(self.cleaned_data["query"]).capitalize()
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")
return f"?{urlencode(query_params, doseq=True)}"