API for javascript user interface
Project description
OARepo API for UI
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
- date histogram facet: correspond with date histogram aggragation in ES (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html)
- group_by_terms_filter: Filter that map values to other values based on dictionary mapping
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
Built Distribution
Hashes for oarepo_ui-2.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4200b623894d5791dd263548cbe2029208f44aa190e218d49bf756eb6ac5de1 |
|
MD5 | 589cf59b824256e93b40ec0cbaf6df2a |
|
BLAKE2b-256 | 2a384ac98a1740c0f21aaf27cef02cde6993a97cf19026e2b7be30ebdf06064d |