Skip to main content

API for javascript user interface

Project description

OARepo API for UI

image image image image

API for UI in OAREPO

Instalation

    pip install oarepo-ui

Usage

This library provides a set of APIs for writing oarepo javascript UI client.

OPTIONS https://localhost:5000/api/<record list endpoint>/?ln=<lang>

Returns known facets of all mappings registered in RECORDS_REST_FACETS

Response:

{
    "facets": {
        "facet-key": {
            "label": "translated facet label",
        }
    },
    "filters": {
        "filter-key": {
            "label": "translated filter label",
            "type": "one of string, number, date, time, datetime"
        }
    }
}

Configuration

OAREPO_UI_TRANSLATOR

Unless configured otherwise, oarepo-ui uses flask-babelex to perform translation of labels/values. This setting can be changed by specifying config option OAREPO_UI_TRANSLATOR. The translator accepts key (and bunch of kwargs according to the context) and returns translated value.

Translation keys

The default keys being fed into babel are:

  • oarepo.facets...label
  • facet values are not translated
  • oarepo.filters...label

This can be changed on index-level or facet-level.

Index-level key override

If label, value and translator are unfilled, the default will be used

from oarepo_ui import translate_facets, translate_filters

FACETS = {
    'category': {
        'terms': {
            'field': 'category',
        },
    }
}
FILTERS = ...

RECORDS_REST_FACETS = {
    'records-record-v1.0.0': {
        'aggs': translate_facets(FACETS, 
                                 label='my.own.{facet_key}', 
                                 value='my.own.{facet_key}.{value_key}', 
                                 translator=lambda key, **kwargs: ...),
        'filters': translate_filters(FILTERS, label='my.own.{filter_key}', 
                                     translator=lambda key, **kwargs: ...)
    },
}

If you do not want to translate the label, use oarepo_ui.no_translation in label or use keep_facets, keep_facet, keep_filters, keep_filter instead.

Facet-level key override

from oarepo_ui import translate_facet

RECORDS_REST_FACETS = {
    'records-record-v1.0.0': {
        'aggs': {
            'category': translate_facet(
                {
                    'terms': {
                        'field': 'category',
                    },
                }, 
                label='my.own.key', 
                value='my.own.{value_key}', 
                translator=lambda key, **kwargs: ...)
        }
   }
}

Filter-level key override

from invenio_records_rest.facets import terms_filter
from oarepo_ui import translate_filter

RECORDS_REST_FACETS = {
    'records-record-v1.0.0': {
        'filters': {
            'category': translate_filter(
                terms_filter('category'), 
                label='my.own.key', 
                translator=lambda key, **kwargs: ...)
        }
   }
}

Permissions

translate_facets, translate_facet can receive additional parameter permissions. Pass a permission factory function perm(index, facet, **kwargs) -> that returns an object with a .can() method. If it returns True, the facet will be returned in https://localhost:5000/api/oarepo/indices/<index-name>?ln=<lang> call.

Note that this does not prevent client to use any filters he/she wants - no permissions are enforced on filter level.

from oarepo_ui import translate_facets, translate_filters

FACETS = ...

def perms(index, facet, **kwargs):
    class Perm():
        def can(self):
            return True
    return Perm()

RECORDS_REST_FACETS = {
    'records-record-v1.0.0': {
        'aggs': translate_facets(FACETS, permissions=perms)
    },
}

Facets and filters library

Filters

exclude_filter: Takes one argument, which is facet function and invert search query using bool must_not query.

f = exclude_filter(terms_filter('test'))
res = f(['a', 'b']).to_dict()
res == {
  "bool": {
    "must_not": [
      {
        "terms": {
          "test": ["a", "b"]
        }
      }
    ]
  }
}

Changes

Version [Unreleased]

Added

Version 1.0.0

  • Initial public release.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oarepo_ui-2.3.1.tar.gz (12.8 kB view hashes)

Uploaded Source

Built Distribution

oarepo_ui-2.3.1-py3-none-any.whl (11.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page