Esempio n. 1
0
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        view = request.args.get('view')

        if results['aggregations'].get('year'):
            results['aggregations']['year']['type'] = 'range'
            results['aggregations']['year']['config'] = {
                'min': 1950,
                'max': int(datetime.now().year),
                'step': 1
            }

        # Add organisation name
        for org_term in results.get('aggregations',
                                    {}).get('organisation',
                                            {}).get('buckets', []):
            organisation = OrganisationRecord.get_record_by_pid(
                org_term['key'])
            if organisation:
                org_term['name'] = organisation['name']

        # Add collection name
        for org_term in results.get('aggregations',
                                    {}).get('collection',
                                            {}).get('buckets', []):
            collection = CollectionRecord.get_record_by_pid(org_term['key'])
            if collection:
                org_term['name'] = get_language_value(collection['name'])

        return super(JSONSerializer,
                     self).post_process_serialize_search(results, pid_fetcher)
Esempio n. 2
0
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        # Add subdivision name
        for org_term in results.get('aggregations',
                                    {}).get('subdivision',
                                            {}).get('buckets', []):
            subdivision = SubdivisionRecord.get_record_by_pid(org_term['key'])
            if subdivision:
                org_term['name'] = get_language_value(subdivision['name'])

        return results
Esempio n. 3
0
def get_custom_field_label(record, custom_field_index):
    """Get the label of a custom field.

    :param record: Record object.
    :param custom_field_index: Index position of the custom field.
    :returns: The label found or None.
    """
    if record.get('organisation') and record['organisation'][0].get(
            'documentsCustomField' + str(custom_field_index), {}).get('label'):
        return get_language_value(
            record['organisation'][0]['documentsCustomField' +
                                      str(custom_field_index)]['label'])

    return None
Esempio n. 4
0
        def language_value(values,
                           locale=None,
                           value_field='value',
                           language_field='language'):
            """Get the value of a field corresponding to the current language.

            :params values: List of values with the language.
            :params locale: Two digit locale to find.
            :params value_field: Name of the property containing the value.
            :params language_field: Name of the property containing the language.
            :returns: The value corresponding to the current language.
            """
            return get_language_value(values, locale, value_field,
                                      language_field)
Esempio n. 5
0
 def get_label(self, obj):
     """Get label."""
     return get_language_value(obj['name'])
Esempio n. 6
0
def aggregations():
    """Get aggregations list."""
    view = request.args.get('view')
    collection = request.args.get('collection')

    customFields = [
        'customField1',
        'customField2',
        'customField3',
    ]

    aggregations_list = [
        'document_type',
        'controlled_affiliation',
        'year',
        'collection',
        'language',
        'author',
        'subject',
        'organisation',
        'subdivision',
    ] + customFields

    if view and view != current_app.config.get(
            'SONAR_APP_DEFAULT_ORGANISATION'):
        organisation = OrganisationRecord.get_record_by_pid(view)
        if organisation and organisation.get('isDedicated') \
        and organisation.get('publicDocumentFacets'):
            aggregations_list = organisation.get('publicDocumentFacets')\
                + customFields
    else:
        organisation = current_organisation

    # Remove organisation in dedicated view
    if 'organisation' in aggregations_list:
        if view and view != current_app.config.get(
                'SONAR_APP_DEFAULT_ORGANISATION'):
            aggregations_list.remove('organisation')
        elif current_user_record and not current_user_record.is_superuser:
            aggregations_list.remove('organisation')

    # Remove collection in collection context
    if collection and 'collection' in aggregations_list:
        aggregations_list.remove('collection')

    # Custom fields
    for i in range(1, 4):
        # Remove custom fields if we are in global view, or the fields is not
        # configured in organisation.
        if view == current_app.config.get(
                'SONAR_APP_DEFAULT_ORGANISATION'
        ) or not organisation or not organisation.get(
                f'documentsCustomField{i}', {}).get('includeInFacets'):
            aggregations_list.remove(f'customField{i}')
        elif organisation[f'documentsCustomField{i}'].get('label'):
            aggregations_list[aggregations_list.index(f'customField{i}')] = {
                'key':
                f'customField{i}',
                'name':
                get_language_value(
                    organisation[f'documentsCustomField{i}']['label'])
            }

    # Don't display subdivision in global context
    if view and 'subdivision' in aggregations_list and \
        view == current_app.config.get('SONAR_APP_DEFAULT_ORGANISATION'):
        aggregations_list.remove('subdivision')

    return jsonify(aggregations_list)