示例#1
0
  def _my_work_count(self, **kwargs):  # pylint: disable=unused-argument
    """Get object counts for my work page."""
    with benchmark("Make response"):
      aliased = my_objects.get_myobjects_query(
          types=self.MY_WORK_OBJECTS.keys(),
          contact_id=login.get_current_user_id()
      )
      all_ = db.session.query(
          aliased.c.type,
          aliased.c.id,
      )

      all_ids = collections.defaultdict(set)
      for type_, id_ in all_:
        all_ids[type_].add(id_)

      response_object = self.MY_WORK_OBJECTS.copy()
      for type_, ids in all_ids.items():
        model = models.get_model(type_)
        # pylint: disable=protected-access
        # We must move the type permissions query to a proper utility function
        # but we will not do that for a patch release
        permission_filter = builder.QueryHelper._get_type_query(model, "read")
        if permission_filter is not None:
          count = model.query.filter(
              model.id.in_(ids),
              permission_filter,
          ).count()
        else:
          count = model.query.filter(model.id.in_(ids)).count()
        response_object[type_] = count

      return self.json_success_response(response_object, )
示例#2
0
  def _my_work_count(self, **kwargs):  # pylint: disable=unused-argument
    """Get object counts for my work page."""
    with benchmark("Make response"):
      aliased = my_objects.get_myobjects_query(
          types=self.MY_WORK_OBJECTS.keys(),
          contact_id=login.get_current_user_id()
      )
      all_ = db.session.query(
          aliased.c.type,
          aliased.c.id,
      )

      all_ids = collections.defaultdict(set)
      for type_, id_ in all_:
        all_ids[type_].add(id_)

      response_object = self.MY_WORK_OBJECTS.copy()
      for type_, ids in all_ids.items():
        model = models.get_model(type_)
        # pylint: disable=protected-access
        # We must move the type permissions query to a proper utility function
        # but we will not do that for a patch release
        permission_filter = builder.QueryHelper._get_type_query(model, "read")
        if permission_filter is not None:
          count = model.query.filter(
              model.id.in_(ids),
              permission_filter,
          ).count()
        else:
          count = model.query.filter(model.id.in_(ids)).count()
        response_object[type_] = count

      return self.json_success_response(response_object, )
示例#3
0
    def search_get_owner_query(query, types=None, contact_id=None):
        """Prepare the search query based on the contact_id to return my
    objects. This method is used only for dashboard and returns objects
    the user is the owner.
    """
        if not contact_id:
            return query

        union_query = my_objects.get_myobjects_query(types=types,
                                                     contact_id=contact_id)

        return query.join(
            union_query,
            and_(union_query.c.id == MysqlRecordProperty.key,
                 union_query.c.type == MysqlRecordProperty.type),
        )
示例#4
0
文件: mysql.py 项目: egorhm/ggrc-core
  def search_get_owner_query(query, types=None, contact_id=None):
    """Prepare the search query based on the contact_id to return my
    objects. This method is used only for dashboard and returns objects
    the user is the owner.
    """
    if not contact_id:
      return query

    union_query = my_objects.get_myobjects_query(
        types=types,
        contact_id=contact_id)

    return query.join(
        union_query,
        sa.and_(
            union_query.c.id == MysqlRecordProperty.key,
            union_query.c.type == MysqlRecordProperty.type),
    )
示例#5
0
def owned(exp, object_class, target_class, query):
    """Get objects for which the user is owner.

  Note: only the first id from the list of ids is used.

  Args:
    ids: the ids of owners.

  Returns:
    sqlalchemy.sql.elements.BinaryExpression if an object of `object_class`
    is owned by one of the given users.
  """
    res = db.session.query(
        my_objects.get_myobjects_query(types=[object_class.__name__],
                                       contact_id=exp['ids'][0]).alias().c.id)
    res = res.all()
    if res:
        return object_class.id.in_([obj.id for obj in res])
    return sqlalchemy.sql.false()
示例#6
0
def owned(exp, object_class, target_class, query):
  """Get objects for which the user is owner.

  Note: only the first id from the list of ids is used.

  Args:
    ids: the ids of owners.

  Returns:
    sqlalchemy.sql.elements.BinaryExpression if an object of `object_class`
    is owned by one of the given users.
  """
  res = db.session.query(
      my_objects.get_myobjects_query(
          types=[object_class.__name__],
          contact_id=exp['ids'][0]
      ).alias().c.id
  )
  res = res.all()
  if res:
    return object_class.id.in_([obj.id for obj in res])
  return sqlalchemy.sql.false()