Commit 474f7b88 authored by felix.herrmann's avatar felix.herrmann
Browse files

Merge branch '426-add-sorting-to-dataset-search-result' into 'master'

Resolve "Add sorting to dataset search result"

Closes #426

See merge request !397
parents 53685739 ccd75776
...@@ -18,13 +18,13 @@ repos: ...@@ -18,13 +18,13 @@ repos:
types: [python] types: [python]
#language_version: python3.6 #language_version: python3.6
- repo: local #- repo: local
hooks: # hooks:
- id: flake8 # - id: flake8
name: flake8 # name: flake8
entry: flake8 # entry: flake8
language: python # language: python
types: [python] # types: [python]
# - repo: local # - repo: local
# hooks: # hooks:
......
...@@ -138,12 +138,18 @@ def core_search_view(request, search_index, objects_on_page): ...@@ -138,12 +138,18 @@ def core_search_view(request, search_index, objects_on_page):
methods_of_data_collection=methods_of_data_collection, methods_of_data_collection=methods_of_data_collection,
disciplines=disciplines, disciplines=disciplines,
) )
# search result ordering
order_by_filter = request.GET.get("orderby")
if order_by_filter == "alpha":
order_by = "title"
else:
order_by = "-publication_date"
# only the dataset with the highest published version per dsmo will be listed in search results # only the dataset with the highest published version per dsmo will be listed in search results
# generate SQL join using Django F() expressions # generate SQL join using Django F() expressions
# https://docs.djangoproject.com/en/3.0/topics/db/queries/#filters-can-reference-fields-on-the-model # https://docs.djangoproject.com/en/3.0/topics/db/queries/#filters-can-reference-fields-on-the-model
queryset = queryset.filter( queryset = queryset.filter(
dataset_management_object__main_published_ds_id=F("id") dataset_management_object__main_published_ds_id=F("id")
) ).order_by(order_by)
elif search_index == "user_index": elif search_index == "user_index":
# default search index is user_index # default search index is user_index
...@@ -199,6 +205,10 @@ def core_search_view(request, search_index, objects_on_page): ...@@ -199,6 +205,10 @@ def core_search_view(request, search_index, objects_on_page):
search_params["disciplines"] = disciplines search_params["disciplines"] = disciplines
search_params["methods_of_data_analysis"] = methods_of_data_analysis search_params["methods_of_data_analysis"] = methods_of_data_analysis
search_params["methods_of_data_collection"] = methods_of_data_collection search_params["methods_of_data_collection"] = methods_of_data_collection
try:
search_params["orderby"] = [order_by_filter]
except UnboundLocalError:
pass
return render( return render(
request, request,
......
...@@ -19,7 +19,21 @@ ...@@ -19,7 +19,21 @@
{# search object list #} {# search object list #}
{% if object_list %} {% if object_list %}
<p>{% if object_list.paginator %}{{ object_list.paginator.count }}{% else %}{{ object_list.count }}{% endif %} {% trans "Datasets found" %}</p> <div class="row smallskip">
<div class="col-md-7">
{% if object_list.paginator %}{{ object_list.paginator.count }}{% else %}{{ object_list.count }}{% endif %} {% trans "Datasets found" %}
</div>
<div class="col-md-5 text-right">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary">
<input type="radio" value="pubdate" name="orderby" id="pubdate" ic-get-from="{% url 'dddatasets:search' %}{{ search_params|add_url_search_params }}" ic-push-url="true" ic-include=".search-middle" ic-target="#content" {% if search_params.orderby.0 != 'alpha' %}checked{% endif %}>{% trans "sort by publication date" %}
</label>
<label class="btn btn-secondary">
<input type="radio" value="alpha" name="orderby" id="alpha" ic-get-from="{% url 'dddatasets:search' %}{{ search_params|add_url_search_params }}" ic-push-url="true" ic-include=".search-middle" ic-target="#content" {% if search_params.orderby.0 == 'alpha' %}checked{% endif %}>{% trans "sort alphabetically" %}
</label>
</div>
</div>
</div>
{% for object in object_list %} {% for object in object_list %}
{% include "dddatasets/_dataset_card.html" with dataset=object prep=False %} {% include "dddatasets/_dataset_card.html" with dataset=object prep=False %}
{% endfor %} {% endfor %}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment