Skip to content

Commit

Permalink
public ui: better document type facet
Browse files Browse the repository at this point in the history
* The functionality of the document main, sub type facet for documents with multiple types was not clear.
Now we display only the sub types accossiated with the main type in the facet.

Co-Authored-by: Peter Weber <peter.weber@rero.ch>
  • Loading branch information
rerowep committed Feb 9, 2021
1 parent fccfa68 commit 6dc58d3
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
9 changes: 8 additions & 1 deletion rero_ils/modules/documents/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from invenio_records_rest.serializers.response import record_responsify, \
search_responsify

from .utils import create_contributions
from .utils import create_contributions, filter_document_type_buckets
from ..documents.api import Document
from ..documents.utils import title_format_text_head
from ..documents.views import create_title_alternate_graphic, \
Expand Down Expand Up @@ -133,6 +133,13 @@ def post_process_serialize_search(self, results, pid_fetcher):
results['aggregations']['library'] = lib_agg
del results['aggregations']['organisation']

# Correct document type buckets
new_type_buckets = []
type_buckets = results['aggregations']['document_type']['buckets']
results['aggregations']['document_type']['buckets'] = \
filter_document_type_buckets(type_buckets)


return super(
DocumentJSONSerializer, self).post_process_serialize_search(
results, pid_fetcher)
Expand Down
85 changes: 85 additions & 0 deletions rero_ils/modules/documents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,91 @@
from .dojson.contrib.marc21tojson.model import remove_trailing_punctuation
from ...utils import get_i18n_supported_languages

DOCUMENT_TYPES = {
'docmaintype_archives': {},
'docmaintype_article': {},
'docmaintype_audio': {
'docsubtype_music': True,
'docsubtype_sound': True,
'docsubtype_audio_book': True,
'docsubtype_recorded_words': True
},
'docmaintype_book': {
'docsubtype_other_book': True,
'docsubtype_large_print_book': True,
'docsubtype_braille_book': True,
'docsubtype_manuscript': True,
'docsubtype_thesis': True,
'docsubtype_e-book': True,
'docsubtype_microform': True
},
'docmaintype_children': {},
'docmaintype_comic': {
'docsubtype_comic': True,
'docsubtype_manga': True
},
'docmaintype_electronic': {
'docsubtype_cdrom_dvdrom': True,
'docsubtype_other_electronic_carrier': True
},
'docmaintype_game': {
'docsubtype_other_game': True,
'docsubtype_video_game': True,
'docsubtype_role-playing_game': True,
'docsubtype_board_game': True
},
'docmaintype_image': {
'docsubtype_poster': True,
'docsubtype_postcard': True,
'docsubtype_photography': True,
'docsubtype_print_engraving': True,
'docsubtype_calendar': True,
'docsubtype_other_image': True
},
'docmaintype_language_method': {},
'docmaintype_leaf': {},
'docmaintype_map': {
'docsubtype_map': True,
'docsubtype_atlas': True
},
'docmaintype_movie_series': {
'docsubtype_movie': True,
'docsubtype_series': True,
'docsubtype_documentary': True
},
'docmaintype_object': {
'docsubtype_kamishibai': True,
'docsubtype_tablet': True,
'docsubtype_other_object': True
},
'docmaintype_other': {},
'docmaintype_score': {
'docsubtype_printed_score': True,
'docsubtype_music_method': True,
'docsubtype_manuscript_score': True
},
'docmaintype_serial': {},
'docmaintype_series': {}
}


def filter_document_type_buckets(buckets):
"""Removes unwanted sub types from buckets."""
new_type_buckets = []
for type_bucket in buckets:
new_type_bucket = type_bucket
main_type = type_bucket['key']
new_subtype_buckets = []
subtype_buckets = type_bucket['document_subtype']['buckets']
for subtype_bucket in subtype_buckets:
if DOCUMENT_TYPES.get(main_type, {}).get(subtype_bucket['key']):
new_subtype_buckets.append(subtype_bucket)
new_type_bucket[
'document_subtype'
]['buckets'] = new_subtype_buckets
new_type_buckets.append(new_type_bucket)
return new_type_buckets


def clean_text(data):
"""Delete all _text from data."""
Expand Down
2 changes: 2 additions & 0 deletions rero_ils/modules/imports/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def serialize_search(self, pid_fetcher, search_result, links=None,
),
aggregations=search_result.get('aggregations', dict()),
)
# TODO: If we have multiple types for a document we have to Correct
# the document type buckets here.
return json.dumps(results, **self._format_args())

def post_process(self, metadata):
Expand Down
8 changes: 7 additions & 1 deletion rero_ils/modules/items/serializers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"""Item serializers."""

from rero_ils.modules.documents.api import search_document_by_pid
from rero_ils.modules.documents.utils import title_format_text_head
from rero_ils.modules.documents.utils import filter_document_type_buckets, \
title_format_text_head
from rero_ils.modules.item_types.api import ItemType
from rero_ils.modules.items.api import Item
from rero_ils.modules.items.models import ItemStatus
Expand Down Expand Up @@ -111,3 +112,8 @@ def post_process_serialize_search(self, results, pid_fetcher):
vendor_term['name'] = vendor.get('name')

return super().post_process_serialize_search(results, pid_fetcher)

# Correct document type buckets
buckets = results['aggregations']['document_type']['buckets']
results['aggregations']['document_type']['buckets'] = \
filter_document_type_buckets(buckets)

0 comments on commit 6dc58d3

Please sign in to comment.