Beispiel #1
0
    def setup_mappings(cls, force=False):
        """ Setup ES mappings for all existing models.

        This method is meant to be run once at application lauch.
        ES._mappings_setup flag is set to not run make mapping creation
        calls on subsequent runs.

        Use `force=True` to make subsequent calls perform mapping
        creation calls to ES.
        """
        if getattr(cls, '_mappings_setup', False) and not force:
            log.debug('ES mappings have been already set up for currently '
                      'running application. Call `setup_mappings` with '
                      '`force=True` to perform mappings set up again.')
            return
        log.info('Setting up ES mappings for all existing models')
        models = engine.get_document_classes()
        try:
            for model_name, model_cls in models.items():
                if getattr(model_cls, '_index_enabled', False):
                    es = cls(model_cls.__name__)
                    es.put_mapping(body=model_cls.get_es_mapping())
        except JHTTPBadRequest as ex:
            raise Exception(ex.json['extra']['data'])
        cls._mappings_setup = True
def update_ace(from_ace, to_ace, models=None):
    """ Update documents that contain ``from_ace`` with ``to_ace``.
    In fact ``from_ace`` is replaced with ``to_ace`` in matching
    documents.

    Look into ACLEncoderMixin.stringify_acl for details on ace format.

    **NOTE**: When using this util with SQLA outside of request cycle
    transaction management should be done explicitly for changes
    to be saved.

    :param from_ace: Stringified ACL entry (ACE) to match agains.
    :param to_ace: Stringified ACL entry (ACE) ``from_ace`` should be
        replaced with. Value is validated.
    :param models: List of document classes objects of which should
        be found and updated.
    :raises ValueError: If no es-based documents passed.
    """
    ACLEncoderMixin().validate_acl([to_ace])
    if models is None:
        models = list(engine.get_document_classes().values())

    documents = find_by_ace(from_ace, models)
    documents = _group_by_type(documents, models)
    document_ids = _extract_ids(documents)
    for model, doc_ids in document_ids.items():
        items = model.get_by_ids(doc_ids)
        _replace_docs_ace(items, from_ace, to_ace)
Beispiel #3
0
    def setup_mappings(cls, force=False):
        """ Setup ES mappings for all existing models.

        This method is meant to be run once at application lauch.
        ES._mappings_setup flag is set to not run make mapping creation
        calls on subsequent runs.

        Use `force=True` to make subsequent calls perform mapping
        creation calls to ES.
        """
        if getattr(cls, '_mappings_setup', False) and not force:
            log.debug('ES mappings have been already set up for currently '
                      'running application. Call `setup_mappings` with '
                      '`force=True` to perform mappings set up again.')
            return
        log.info('Setting up ES mappings for all existing models')
        models = engine.get_document_classes()
        try:
            for model_name, model_cls in models.items():
                if getattr(model_cls, '_index_enabled', False):
                    es = cls(model_cls.__name__)
                    es.put_mapping(body=model_cls.get_es_mapping())
        except JHTTPBadRequest as ex:
            raise Exception(ex.json['extra']['data'])
        cls._mappings_setup = True
Beispiel #4
0
def update_ace(from_ace, to_ace, models=None):
    """ Update documents that contain ``from_ace`` with ``to_ace``.
    In fact ``from_ace`` is replaced with ``to_ace`` in matching
    documents.

    Look into ACLEncoderMixin.stringify_acl for details on ace format.

    **NOTE**: When using this util with SQLA outside of request cycle
    transaction management should be done explicitly for changes
    to be saved.

    :param from_ace: Stringified ACL entry (ACE) to match agains.
    :param to_ace: Stringified ACL entry (ACE) ``from_ace`` should be
        replaced with. Value is validated.
    :param models: List of document classes objects of which should
        be found and updated.
    :raises ValueError: If no es-based documents passed.
    """
    ACLEncoderMixin().validate_acl([to_ace])
    if models is None:
        models = list(engine.get_document_classes().values())

    documents = find_by_ace(from_ace, models)
    documents = _group_by_type(documents, models)
    document_ids = _extract_ids(documents)
    for model, doc_ids in document_ids.items():
        items = model.get_by_ids(doc_ids)
        _replace_docs_ace(items, from_ace, to_ace)
Beispiel #5
0
 def run(self):
     ES.setup(self.settings)
     if self.options.recreate:
         self.recreate_index()
         models = engine.get_document_classes()
         model_names = [
             name for name, model in models.items()
             if getattr(model, '_index_enabled', False)]
     else:
         model_names = split_strip(self.options.models)
     self.index_models(model_names)
Beispiel #6
0
 def run(self):
     ES.setup(self.settings)
     if self.options.recreate:
         self.recreate_index()
         models = engine.get_document_classes()
         model_names = [
             name for name, model in models.items()
             if getattr(model, '_index_enabled', False)
         ]
     else:
         model_names = split_strip(self.options.models)
     self.index_models(model_names)
def count_ace(ace, models=None):
    """ Count number of given models items with given ace.

    Look into ACLEncoderMixin.stringify_acl for details on ace format.

    :param ace: Stringified ACL entry (ACE).
    :param models: List of document classes objects of which should
        be found and counted.
    :returns: Dict of format {Model: number_of_matching_docs, ...}.
        Number of matching documents is None if model is not Es-based.
    """
    counts = {}
    if models is None:
        models = list(engine.get_document_classes().values())

    for model in models:
        try:
            counts[model] = find_by_ace(ace, [model], count=True)
        except ValueError:
            counts[model] = None

    return counts
Beispiel #8
0
def count_ace(ace, models=None):
    """ Count number of given models items with given ace.

    Look into ACLEncoderMixin.stringify_acl for details on ace format.

    :param ace: Stringified ACL entry (ACE).
    :param models: List of document classes objects of which should
        be found and counted.
    :returns: Dict of format {Model: number_of_matching_docs, ...}.
        Number of matching documents is None if model is not Es-based.
    """
    counts = {}
    if models is None:
        models = list(engine.get_document_classes().values())

    for model in models:
        try:
            counts[model] = find_by_ace(ace, [model], count=True)
        except ValueError:
            counts[model] = None

    return counts
Beispiel #9
0
def available_models():
    models = engine.get_document_classes()
    return [
        name for name, model in models.items()
        if getattr(model, '_index_enabled', False)
    ]