Exemplo n.º 1
0
  def after_commit(self, session):
    """
    Any db updates go through here. We check if any of these models have
    ``__searchable__`` fields, indicating they need to be indexed. With these
    we update the whoosh index for the model. If no index exists, it will be
    created here; this could impose a penalty on the initial commit of a model.
    """
    if not self.running or session is not db.session():
      return

    for cls_name, values in self.to_update.iteritems():
      model_class = values[0][1].__class__
      assert model_class.__name__ == cls_name

      if (model_class not in self.indexed_classes
          or not hasattr(model_class, '__searchable__')):
        # safeguard
        continue

      primary_field = model_class.search_query.primary
      values = [(op, getattr(model, primary_field))
                for op, model in values]
      index_update.apply_async(kwargs=dict(class_name=cls_name, items=values))

    self.to_update = {}
Exemplo n.º 2
0
  def after_flush(self, session, flush_context):
    if not self.running or session is not db.session():
      return

    get_queue_for = lambda cls_name: self.to_update.setdefault(cls_name, [])

    for model in session.new:
      model_class = model.__class__

      if hasattr(model_class, '__searchable__'):
        get_queue_for(model_class.__name__).append(("new", model))

    for model in session.deleted:
      model_class = model.__class__
      if hasattr(model_class, '__searchable__'):
        get_queue_for(model_class.__name__).append(("deleted", model))

    for model in session.dirty:
      model_class = model.__class__
      if hasattr(model_class, '__searchable__'):
        get_queue_for(model_class.__name__).append(("changed", model))