ext.py 2.5 KB
Newer Older
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
#
# This file is part of CERN Search.
Carina Antunes's avatar
Carina Antunes committed
5
# Copyright (C) 2018-2021 CERN.
6
#
7
# Citadel Search is free software; you can redistribute it and/or modify it
8
# under the terms of the MIT License; see LICENSE file for more details.
9
10
"""Cern Search module."""
from celery import current_app as current_celery
Carina Antunes's avatar
Carina Antunes committed
11

12
13
from cern_search_rest_api.modules.cernsearch.celery import DeclareDeadletter
from cern_search_rest_api.modules.cernsearch.views import build_blueprint, build_blueprint_record_files_content
14
15
16
17
18
19
20
21
22
23
24
25
26


class CERNSearch(object):
    """CERN Search extension."""

    def __init__(self, app=None):
        """Extension initialization."""
        if app:
            self.init_app(app)

    def init_app(self, app):
        """Flask application initialization."""
        self.init_config(app)
27

28
29
30
        blueprint = build_blueprint(app)
        app.register_blueprint(blueprint)

31
32
33
        blueprint_record_files_content = build_blueprint_record_files_content(app)
        app.register_blueprint(blueprint_record_files_content)

Carina Antunes's avatar
Carina Antunes committed
34
        current_celery.steps["worker"].add(DeclareDeadletter)
Carina Antunes's avatar
Carina Antunes committed
35
        current_celery.conf.update(app.config["CELERYCONF_V6"])
36
        self.register_signals(app)
37
38

        app.extensions["cern-search"] = self
Carina Antunes's avatar
Carina Antunes committed
39

40
41
42
43
    def init_config(self, app):
        """Initialize configuration."""
        # Set up API endpoints for records.
        for k in dir(app.config):
Carina Antunes's avatar
Carina Antunes committed
44
            if k.startswith("CERN_SEARCH"):
Carina Antunes's avatar
Carina Antunes committed
45
                app.config.setdefault(k, getattr(app.config, k))
46

47
    def register_signals(self, app):
48
        """Register signals."""
Carina Antunes's avatar
Carina Antunes committed
49
        if app.config["SEARCH_FILE_INDEXER"]:
Carina Antunes's avatar
Carina Antunes committed
50
51
52
53
54
55
            from invenio_files_processor.signals import file_processed
            from invenio_files_rest.signals import file_deleted, file_uploaded
            from invenio_indexer.signals import before_record_index
            from invenio_records.signals import after_record_delete

            from cern_search_rest_api.modules.cernsearch.indexer import index_file_content
Carina Antunes's avatar
Carina Antunes committed
56
57
58
59
60
61
            from cern_search_rest_api.modules.cernsearch.receivers import (
                file_deleted_listener,
                file_processed_listener,
                file_uploaded_listener,
                record_deleted_listener,
            )
62
63
64
65
66
67

            file_uploaded.connect(file_uploaded_listener)
            file_processed.connect(file_processed_listener)
            file_deleted.connect(file_deleted_listener)
            after_record_delete.connect(record_deleted_listener)
            before_record_index.connect(index_file_content)