source: main/waeup.kofa/trunk/src/waeup/kofa/scripts.py @ 15819

Last change on this file since 15819 was 15754, checked in by uli, 5 years ago

Add a collection of helper scripts.

These will not work through-the-web. Use them from the debug shell.

File size: 1.3 KB
Line 
1"""
2This is a collection of arbitrary scripts for mainly maintenance purposes.
3
4The scripts are not callable via web interface.
5
6Use them (for instance) like so::
7
8    $ ./bin/kofa-debug
9    >>> from waeup.kofa.scripts import add_field_index
10    >>> add_field_index(root['aaue'], 'things_catalog', 'new_id')
11
12Use at your own risk.
13
14"""
15from zope.catalog.field import FieldIndex
16from zope.catalog.interfaces import ICatalog
17from zope.component import getUtility
18from waeup.kofa.utils.helpers import reindex_cat
19import transaction
20
21
22def get_catalog(app, catalog_name):
23    return getUtility(ICatalog, name=catalog_name, context=app)
24
25
26def add_field_index(app, catalog_name, field_name):
27    """Add a catalog FieldIndex.
28
29    This can become cumbersome through the web API as _all_ objects stored in
30    ZODB will then be indexed and pulled into memory - might become a problem
31    with large databases. This CLI runs faster. You should make sure, that not
32    much traffic is on the site when running the script.
33    """
34    cat = get_catalog(app, catalog_name)
35    if field_name in cat.keys():
36        # index exists already
37        return False
38    cat[field_name] = FieldIndex(field_name=field_name)
39    transaction.commit()
40    reindex_cat(cat)
41    transaction.commit()
42    return True
Note: See TracBrowser for help on using the repository browser.