Skip to content

Commit

Permalink
refactored tobe used more general
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Feb 8, 2019
1 parent 6d223a2 commit 33d2901
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/z3c/relationfield/README.rst
Expand Up @@ -881,5 +881,7 @@ Setting up a releation catalog
This package provides a RelationCatalog initialized with a set of indexes commonly useful for queries on RelationValue objects.
The default indexes are `from_id`, `to_id`, `from_attribute`, `from_interfaces_flattened` and `to_interfaces_flattened`.

However, sometimes it is needed to define custom indexes.
The `zc.relationfield.index.RelationCatalog` class can be initialized with a list of dicts with keys `name` and `kwargs` to be passed to RelationCatalog to be passed to the catalogs `addValueIndex` method.
Sometimes it is needed to define custom indexes or use less than the default ones.
The `zc.relationfield.index.RelationCatalog` class can be initialized with a list of dicts with keys `element` and `kwargs` to be passed to RelationCatalog `addValueIndex` method.
As `element` in general the attribute on the `IRelationValue` like `IRelationValue['from_id']` is expected.
However, if theres a subclass of `IRelationValue` is used with additional fields, those fields can be added here as indexes.
15 changes: 6 additions & 9 deletions src/z3c/relationfield/index.py
Expand Up @@ -7,26 +7,26 @@

DEFAULT_INDEXES = [
{
'name': 'from_id',
'element': IRelationValue['from_id'],
},
{
'name': 'to_id',
'element': IRelationValue['to_id'],
},
{
'name': 'from_attribute',
'element': IRelationValue['from_attribute'],
'kwargs': {
'btree': BTrees.family32.OI,
},
},
{
'name': 'from_interfaces_flattened',
'element': IRelationValue['from_interfaces_flattened'],
'kwargs': {
'btree': BTrees.family32.OI,
'multiple': True,
},
},
{
'name': 'to_interfaces_flattened',
'element': IRelationValue['to_interfaces_flattened'],
'kwargs': {
'btree': BTrees.family32.OI,
'multiple': True,
Expand Down Expand Up @@ -58,7 +58,4 @@ def __init__(self, indexes=DEFAULT_INDEXES):
"""
Catalog.__init__(self, dump, load)
for index in indexes:
self.addValueIndex(
IRelationValue[index['name']],
**index.get('kwargs', {})
)
self.addValueIndex(index['element'], **index.get('kwargs', {}))

0 comments on commit 33d2901

Please sign in to comment.