Skip to content

Commit

Permalink
public ui: fix missing library facet on org view
Browse files Browse the repository at this point in the history
* Forces to compute the `organisation` aggregation values when the view
  is an organisation view and the `library` aggregation is enable.
* Closes #2215.

Co-Authored-by: Johnny Mariéthoz <johnny.mariethoz@rero.ch>
  • Loading branch information
jma and jma committed Jul 21, 2021
1 parent 50dae7a commit b6e87cd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion rero_ils/modules/documents/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def post_process_serialize_search(self, results, pid_fetcher):
if lib_buckets:
org_term['library']['buckets'] = lib_buckets

# TODO: Move this logic in the front end (needs backend adaptation)
# TODO: this should be done in the facet factory as we compute
# unused aggs values
if (viewcode is not None) and (viewcode != global_view_code):
org = Organisation.get_record_by_viewcode(viewcode)
org_buckets = results.get('aggregations', {}).get(
Expand Down
10 changes: 10 additions & 0 deletions rero_ils/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from flask import current_app, request
from invenio_i18n.ext import current_i18n
from invenio_records_rest.errors import InvalidQueryRESTError
from werkzeug.datastructures import ImmutableMultiDict

from .facets import i18n_facets_factory
from .modules.organisations.api import Organisation
Expand Down Expand Up @@ -133,6 +134,15 @@ def inner(values):
def documents_search_factory(self, search, query_parser=None):
"""Search factory with view code parameter."""
view = request.args.get('view')
# force to have organisation aggs if library is set
facets = request.args.get('facets', [])
if facets:
facets = facets.split(',')
if 'library' in facets and not 'organisation' in facets:
args = request.args.to_dict()
facets.append('organisation')
args['facets'] = ','.join(facets)
request.args = ImmutableMultiDict(args)
search, urlkwargs = search_factory(self, search)
# public interface
if view:
Expand Down
38 changes: 38 additions & 0 deletions tests/api/documents/test_documents_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,50 @@ def test_documents_facets(
res = client.get(list_url, headers=rero_json_header)
data = get_json(res)
aggs = data['aggregations']

# check all facets are present
for facet in [
'document_type', 'author', 'language', 'subject', 'status'
]:
assert aggs[facet]

list_url = url_for(
'invenio_records_rest.doc_list', view='global', facets='')
res = client.get(list_url, headers=rero_json_header)
data = get_json(res)
aggs = data['aggregations']
assert not aggs

list_url = url_for(
'invenio_records_rest.doc_list', view='global', facets='document_type')
res = client.get(list_url, headers=rero_json_header)
data = get_json(res)
aggs = data['aggregations']
assert list(aggs.keys()) == ['document_type']

list_url = url_for(
'invenio_records_rest.doc_list', view='global', facets='document_type')
res = client.get(list_url, headers=rero_json_header)
data = get_json(res)
aggs = data['aggregations']
assert list(aggs.keys()) == ['document_type']

list_url = url_for(
'invenio_records_rest.doc_list', view='org1', facets='document_type')
res = client.get(list_url, headers=rero_json_header)
data = get_json(res)
aggs = data['aggregations']
assert list(aggs.keys()) == ['document_type']

# test the patch that the library facet is computed by the serializer
list_url = url_for(
'invenio_records_rest.doc_list', view='org1',
facets='document_type,library,author')
res = client.get(list_url, headers=rero_json_header)
data = get_json(res)
aggs = data['aggregations']
assert set(aggs.keys()) == set(['document_type', 'library', 'author'])

# FILTERS
# contribution
list_url = url_for('invenio_records_rest.doc_list', view='global',
Expand Down

0 comments on commit b6e87cd

Please sign in to comment.