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

This version

2.3.0

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.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oarepo_ui-2.3.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file oarepo_ui-2.3.0.tar.gz.

File metadata

  • Download URL: oarepo_ui-2.3.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for oarepo_ui-2.3.0.tar.gz
Algorithm Hash digest
SHA256 d493769ecdf0afd72b14dff9f62e7561fd13455c1876a3755de161c815a374aa
MD5 af395ffe5047dd157c9e2f8f43330d11
BLAKE2b-256 7a56f732be6131521478550b8881788ff4aba9b3e83ad57db4025f515f29e5cb

See more details on using hashes here.

File details

Details for the file oarepo_ui-2.3.0-py3-none-any.whl.

File metadata

  • Download URL: oarepo_ui-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for oarepo_ui-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dcf183de97b0b9b41812fdc141b64e257796dda5387945ed6fe68e7e3b1f0203
MD5 f3c1266a96510a0641b64808912ea403
BLAKE2b-256 5cb676d523861650681252d16108ea38e6c353c9f4ff28df07e456b63294ed75

See more details on using hashes here.

Supported by

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