Passed
Pull Request — 2.x (#1933)
by Ramon
05:34
created

senaite.core.scripts._reindex   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 47
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A run() 0 18 2
A recursive_reindex() 0 10 5
A reindex() 0 7 1
1
# -*- coding: utf-8 -*-
2
3
import argparse
4
5
import transaction
6
from AccessControl.SecurityManagement import newSecurityManager
7
from Acquisition import aq_base
8
from bika.lims import api
9
from senaite.core import logger
10
from senaite.core.scripts import parser
11
from senaite.core.scripts.utils import setup_site
12
from senaite.core.decorators import retriable
13
14
__doc__ = """Reindex objects in SENAITE
15
"""
16
17
parser.description = __doc__
18
parser.add_argument("-r", "--recursive", default=False,
19
                    dest="recursive", action="store_true",
20
                    help="Reindex objects recursively (default: %(default)s)")
21
parser.add_argument("-p", "--path",
22
                    dest="path", default=None, type=str,
23
                    help="Path to reindex (default: %(default)s)")
24
25
26
def recursive_reindex(obj, recursive=False):
27
    """Reindex object
28
    """
29
    if not api.is_object(obj):
30
        return
31
    logger.debug("Reindexing %s" % api.get_path(obj))
32
    obj.reindexObject()
33
    if recursive and hasattr(aq_base(obj), "objectItems"):
34
        for child in obj.objectValues():
35
            recursive_reindex(child, recursive=recursive)
36
37
38
@retriable(sync=True)
39
def reindex(path, recursive=False):
40
    """Reindex path
41
    """
42
    obj = api.get_object_by_path(path)
43
    recursive_reindex(obj, recursive=recursive)
44
    transaction.commit()
45
46
47
def run(app):
48
    args, _ = parser.parse_known_args()
49
50
    site = None
51
    sid = args.site_id or "senaite"
52
    site = app.get(sid)
53
    if site:
54
        setup_site(site)
55
    else:
56
        raise argparse.ArgumentError(
57
            "No SENAITE site with ID '%s' found" % sid)
58
59
    user = app.acl_users.getUser("admin")
60
    newSecurityManager(None, user.__of__(app.acl_users))
61
62
    path = args.path or "/%s" % sid
63
    recursive = args.recursive
64
    reindex(path, recursive=recursive)
65
66
67
if __name__ == "__main__":
68
    run(app)  # noqa
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable app does not seem to be defined.
Loading history...
69