ext.py 2.28 KB
Newer Older
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
5
6
#
# This file is part of CERN Search.
# Copyright (C) 2018-2019 CERN.
#
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
11
12
13
14
15
16
17
"""Cern Search module."""
from celery import current_app as current_celery
from cern_search_rest_api.modules.cernsearch.celery import DeclareDeadletter
from cern_search_rest_api.modules.cernsearch.indexer import index_file_content
from cern_search_rest_api.modules.cernsearch.receivers import (file_deleted_listener, file_processed_listener,
                                                               file_uploaded_listener, record_deleted_listener)
from cern_search_rest_api.modules.cernsearch.views import build_blueprint, build_blueprint_record_files_content
from invenio_files_processor.signals import file_processed
from invenio_files_rest.signals import file_deleted, file_uploaded
Carina Antunes's avatar
Carina Antunes committed
18
from invenio_indexer.signals import before_record_index
19
from invenio_records.signals import after_record_delete
20
21
22
23
24
25
26
27
28
29
30
31
32


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)
33

34
35
36
        blueprint = build_blueprint(app)
        app.register_blueprint(blueprint)

37
38
39
40
41
42
43
44
        blueprint_record_files_content = build_blueprint_record_files_content(app)
        app.register_blueprint(blueprint_record_files_content)

        current_celery.steps['worker'].add(DeclareDeadletter)

        self.register_signals()

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

46
47
48
49
50
    def init_config(self, app):
        """Initialize configuration."""
        # Set up API endpoints for records.
        for k in dir(app.config):
            if k.startswith('CERN_SEARCH'):
Carina Antunes's avatar
Carina Antunes committed
51
                app.config.setdefault(k, getattr(app.config, k))
52
53
54
55
56
57
58
59

    def register_signals(self):
        """Register signals."""
        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)