@@ -30,6 +30,16 @@ def set_excluded_facets(url, available_facets):
3030 exclude_str += 'excluded_facets=citation_type:' + facet_tuple [0 ] + "&"
3131 return (url + '&' + exclude_str ).replace ("&&" , "&" )
3232
33+ @register .filter
34+ def remove_selected_facet (url , arg ):
35+ path , qs = _get_path_and_qs (url )
36+ for para in qs :
37+ if para [0 ] == "selected_facets" and para [1 ] == arg :
38+ qs .remove (para )
39+
40+ return _build_final_url (path , qs )
41+
42+
3343@register .filter
3444def remove_url_part (url , arg ):
3545 qs = urllib .parse .parse_qsl (url )
@@ -58,18 +68,13 @@ def add_selected_facet(url, facet):
5868
5969@register .filter
6070def remove_query (url ):
61- # we will probably have the path prefix before the query string
62- parsed_url = urllib .parse .urlparse (url )
63- query_string = parsed_url .query
64- path = parsed_url .path
65-
66- qs = urllib .parse .parse_qsl (query_string )
71+ path , qs = _get_path_and_qs (url )
72+
6773 for para in qs :
6874 if para [0 ] == "q" :
6975 qs .remove (para )
7076
71- full_qs = "&" .join ([f"{ para [0 ]} ={ para [1 ]} " for para in qs ])
72- return path + "?" + full_qs if path else full_qs
77+ return _build_final_url (path , qs )
7378
7479
7580@register .filter
@@ -109,20 +114,43 @@ def are_reviews_excluded(url):
109114
110115@register .filter
111116def is_limited_to_tech_culture (url ):
112- parsed_url = urllib .parse .urlparse (url )
113- query_string = parsed_url .query
114- path = parsed_url .path
115-
116- qs = urllib .parse .parse_qsl (query_string )
117+ path , qs = _get_path_and_qs (url )
117118 for para in qs :
118119 if para [0 ] == "selected_facets" and para [1 ]== "citation_dataset_typed_names:Technology & Culture Bibliography" :
119120 return True
120121 return False
121122
122123@register .filter
123124def limit_to_tech_culture_facet (url ):
124- return (url + "&selected_facets=citation_dataset_typed_names:Technology%20%26%20Culture%20Bibliography" )
125+ path , qs = _get_path_and_qs (url )
126+ if not any (para [0 ] == "selected_facets" and \
127+ para [1 ]== "citation_dataset_typed_names:Technology & Culture Bibliography" \
128+ for para in qs
129+ ):
130+ qs .append (("selected_facets" ,"citation_dataset_typed_names:Technology & Culture Bibliography" ))
131+ return _build_final_url (path , qs )
125132
133+ @register .filter
134+ def remove_tech_culture_facet (url ):
135+ path , qs = _get_path_and_qs (url )
136+ for para in qs :
137+ if para [0 ] == "selected_facets" and para [1 ]== "citation_dataset_typed_names:Technology & Culture Bibliography" :
138+ qs .remove (para )
139+ return _build_final_url (path , qs )
140+
141+ @register .filter
142+ def set_status_retain (url ):
143+ """
144+ If this parameter is set, the search should maintain all panel statuses (which filters are open
145+ and selected and which ones are not).
146+ """
147+ path , qs = _get_path_and_qs (url )
148+ for para in qs :
149+ if para [0 ] == 'state' :
150+ qs .remove (para )
151+ qs .append (('state' , 'retain' ))
152+
153+ return _build_final_url (path , qs )
126154
127155@register .filter
128156def are_stubs_excluded (url ):
@@ -135,3 +163,24 @@ def add_excluded_stub_record_status_facet(url, facet):
135163 url = url .replace ('selected_facets=' + facet_str , '' )
136164 url = url + '&excluded_facets=' + facet_str
137165 return url .replace ('&&' , '&' )
166+
167+ def _get_path_and_qs (url ):
168+ # we will probably have the path prefix before the query string
169+ parsed_url = urllib .parse .urlparse (url )
170+ query_string = parsed_url .query
171+ path = parsed_url .path
172+
173+ qs = urllib .parse .parse_qsl (query_string )
174+ return path , qs
175+
176+ def _build_final_url (path , qs ):
177+ """
178+ Build a full url from a path prefix and a query string list.
179+ E.g.:
180+ path: /p/isiscb
181+ qs: [('state', 'retain'), ('selected_facets', 'Review')]
182+ becomes
183+ /p/isiscb?state=retain&selected_facets=Review
184+ """
185+ full_qs = "&" .join ([f"{ para [0 ]} ={ urllib .parse .quote (para [1 ])} " for para in qs ])
186+ return path + "?" + full_qs if path else full_qs
0 commit comments